- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Golang program to implement binary tree data structure

In the Go, a binary tree is a tree data structure where each node has at most two children, as the left child and the right child. Go's built-in data structures and operations, like structs and pointers, can be used to create a binary tree. The tree's nodes can be visualized as structs with fields for the value kept at each node and pointers to any left or right children. There are three types of tree traversals- preorder, inorder and postorder. We will use two methods to implement the binary tree data structure. In the first method we executed pre-order traversal and in the second method we executed in-order traversal.

## Method 1: Using pre-order traversal

This method generates a binary tree with two child nodes with values of 20 and 30 and a root node with a value of 10. The value of the right child of the node with value 20 is 50, while the value of the left child of the node with value 20 is 40. The binary tree's pre-order traversal is then printed. Let’s go through the code and algorithm to understand the concept.

### Algorithm

**Step 1**− Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.**Step 2**− Establish a structure Node is used to represent a node in a binary tree and has three fields − value, left_val, and right_val. Value stores the node's value, while left_val and right_val store pointers to its left and right child nodes, respectively.**Step 3**− Create a root node with a value of 10, two child nodes with values of 20 and 30, and designate them as the left and right children of the root node, respectively, in the main function.**Step 4**− To make the left and right children of the node with value 20, respectively, create two new nodes with values 40 and 50.**Step 5**− Create the function preorder to traverse the binary tree in pre-order. This function prints the value of a node as an argument.**Step 6**− If there are left and right child nodes of the current node, the preorder function then calls itself recursively on those nodes. In this approach, the function makes pre-order visits to each node in the binary tree.**Step 7**− In order to complete the pre-order traverse of the binary tree, execute the preorder function on the root node. Print the values of the nodes visited in the order they were visited.**Step 8**− The print statement is executed using fmt.Println() function where ln means new line.

### Example

In this example we will print the preorder traversal.

package main import "fmt" type Node struct { value int left_val *Node //create and left_val and right_val node right_val *Node } func main() { root := &Node{value: 10} //assign the linked list required elements root.left_val = &Node{value: 20} root.right_val = &Node{value: 30} root.left_val.left_val = &Node{value: 40} root.left_val.right_val = &Node{value: 50} fmt.Println("The pre-order traversal is given as:") preOrder(root) } func preOrder(node *Node) { if node == nil { return } fmt.Println(node.value) preOrder(node.left_val) preOrder(node.right_val) }

### Output

The pre-order traversal is given as: 10 20 40 50 30

## Method 2: Using in-order traversal

This method generates a binary tree with two child nodes that have the values 2 and 6 and a root node that has the value 40. The value of the right child of the node with value 20 is 30, and the value of the left child of the node with value 20 is 10. The value of the right child of the node with value 60 is 70, while the value of the left child of the node with value 60 is 50. The binary tree's in-order traversal is then printed.

### Algorithm

**Step 1**− Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.**Step 2**− Establish a structure Node is used to represent a node in a binary tree and has three fields − value, left_val, and right_val. Value stores the node's value, while left_val and right_val store pointers to its left and right child nodes, respectively.**Step 3**− Create a root node with a value of 40, two child nodes with values of 20 and 60, and designate them as the root node's left and right children, respectively, in the main function.**Step 4**− To make the left and right children of the node with value 20, respectively, create two new nodes with values 10 and 30.**Step 5**− To make the left and right children of the node with value 60, respectively, create two more nodes with values of 50 and 70.**Step 6**− Create the function in Order to traverse the binary tree in order. The argument for this function is a node.**Step 7**− If there is a left child of the current node, the in Order function first calls itself recursively on that node.**Step 8**− The value of the current node is then printed.**Step 9**− Finally, if there is a right child node of the current node, the method calls itself recursively. The binary tree's nodes are all visited sequentially by the function in this fashion.**Step 10**− To begin an in-order traverse of the binary tree, call the in Order function on the root node. Print the values of the nodes visited in the order they were visited.

### Example

In this example we will print the in order traversal.

package main import "fmt" type Node struct { value int left_val *Node //create left_val and right_val elements right_val *Node } func main() { root := &Node{value: 40} //fill the linked list with the elements root.left_val = &Node{value: 20} root.right_val = &Node{value: 60} root.left_val.left_val = &Node{value: 10} root.left_val.right_val = &Node{value: 30} root.right_val.left_val = &Node{value: 50} root.right_val.right_val = &Node{value: 70} fmt.Println("The In-order traversal created here is:") inOrder(root) } func inOrder(node *Node) { if node == nil { return } inOrder(node.left_val) fmt.Println(node.value) inOrder(node.right_val) }

### Output

The In-order traversal created here is: 10 20 30 40 50 60 70

## Conclusion

We executed the program of implementing the binary tree data structure using two methods. In the first example we printed the pre-order traversal of binary tree and in the second case we printed the inorder traversal of binary tree.

- Related Articles
- Python program to implement binary tree data structure
- Golang program to implement stack data structure
- Golang program to implement graph data structure
- Golang program to implement the Queue data structure
- Binary Tree ADT in Data Structure
- Unrooted binary tree in Data Structure
- C++ program to Implement Threaded Binary Tree
- Golang program to define a binary tree
- C++ Program to Implement Randomized Binary Search Tree
- C++ Program to Implement self Balancing Binary Search Tree
- Python Program to Implement Binary Tree using Linked List
- C++ Program to Implement Disjoint Set Data Structure
- Java Program to Implement the graph data structure
- Java Program to Implement the queue data structure
- C++ Program to Implement a Binary Search Tree using Linked Lists