- 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 Perform the preorder tree traversal
In Go programming language, pre-order traversal is a tree traversal technique in which the root node is visited first, then the left subtree, and finally the right subtree. A recursive function is used that calls itself on the root node, the left child node, the right child node, and finally the left child node again. When the node is nil, the recursion's basic case, occurs. We will execute pre-order traversal in this program using two methods- the TreeNode struct and the stacks.
Method 1: Using TreeNode struct
This method builds a tree node structure With a Value field, a pointer to the Left child node, and a pointer to the Right child node. The pre_order_traversal function uses a pointer to the root node to recursively visit the root node, the left subtree, and then the right subtree in the tree in pre-order. The pre_order_traversal method is called by the main function to print the tree's pre-order traversal after creating a sample tree.
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 − Create a TreeNode struct with value of type int, left_val and right_val of type TreeNode.
Step 3 − Create a function pre_order_traversal and in that particular function return whether the current node is null. Print the node's value at the moment.
Step 4 − In the next step, call pre_order_traversal with the left child of the current node as the parameter to traverse the left subtree.
Step 5 − Call pre_order_traversal with the right child of the current node as the parameter to navigate the correct subtree.
Step 6 − For every child node, repeat steps 1-4 until all nodes have been visited.
Step 7 − This algorithm visits the root node first, then the left subtree, and ultimately the right subtree as it traverses the tree via recursion. When a branch of the tree comes to an end or when all nodes have been visited, the recursion comes to an end.
Example
In this example we will use TreeNode struct to implement pre-order traversal. Let’s have a look at the code.
package main import "fmt" // Define a tree node structure type TreeNode struct { Value int Left_val *TreeNode Right_val *TreeNode } // Function to traverse the tree in pre-order func pre_order_traversal(root *TreeNode) { if root == nil { return } fmt.Print(root.Value, " ") pre_order_traversal(root.Left_val) pre_order_traversal(root.Right_val) } func main() { // Create a tree root := &TreeNode{Value: 10} root.Left_val = &TreeNode{Value: 20} root.Right_val = &TreeNode{Value: 30} root.Left_val.Left_val = &TreeNode{Value: 40} root.Left_val.Right_val = &TreeNode{Value: 50} // Call the pre-order traversal function fmt.Print("Pre-order traversal: ") pre_order_traversal(root) fmt.Println() }
Output
Pre-order traversal: 10 20 40 50 30
Method 2: Using Stacks
This method employs a stack to keep track of the nodes that need to be visited. The method repeatedly removes a node from the stack, prints its value, and pushes its right and left children into the stack in that sequence. The stack starts with the root node. This results in a pre-order traversal of the tree, where each node's left child is examined before its right child.
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 − Create a TreeNode struct with value of type int, left_val and right_val of type TreeNode.
Step 3 − Create a function pre_order_traversal and in that function return if the root node is null.
Step 4 − In the next step, create a stack from the root node up.
Step 5 − Run a loop by checking if the length of the stack is greater than 0 and repeat the following actions as long as the stack is still full −
a. Remove the node from the stack's top and assign it to a variable called curr.
b. Print the current value.
Step 6 − In the main function the function will be called and the print statement is executed using fmt.Println() function where ln means new line.
Example
In this example we will use stacks to show pre-order traversal. Let’s see the execution through the code.
package main import ( "fmt" ) // Define a tree node structure type TreeNode struct { Value int Left_val *TreeNode Right_val *TreeNode } // Traverse the tree in pre-order using a stack func pre_order_traversal(root *TreeNode) { if root == nil { return } stack := []*TreeNode{root} for len(stack) > 0 { curr := stack[len(stack)-1] stack = stack[:len(stack)-1] fmt.Print(curr.Value, " ") if curr.Right_val != nil { stack = append(stack, curr.Right_val) } if curr.Left_val != nil { stack = append(stack, curr.Left_val) } } } func main() { // Create a tree root := &TreeNode{Value: 10} root.Left_val = &TreeNode{Value: 20} root.Right_val = &TreeNode{Value: 30} root.Left_val.Left_val = &TreeNode{Value: 40} root.Left_val.Right_val = &TreeNode{Value: 50} // Call the pre-order traversal function fmt.Print("Pre-order traversal: ") pre_order_traversal(root) fmt.Println() }
Output
Pre-order traversal: 10 20 40 50 30
Conclusion
We executed the above program using two examples. In the first example we used the TreeNode struct and in the second example we used stacks on TreeNode struct to show pre-order traversal.
- Related Articles
- Golang program to perform inorder tree traversal
- Golang Program to Perform the postorder tree traversal
- C++ Program to Perform Preorder Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
- Golang Program to traverse a given binary tree in Preorder Traversal (Recursive)
- Java Program to Perform the inorder tree traversal
- Preorder Tree Traversal in Data Structures
- Binary Tree Preorder Traversal in Python
- Program to generate tree using preorder and inorder traversal in python
- N-ary Tree Preorder Traversal in C++
- Recover a Tree From Preorder Traversal in C++
- Construct Binary Search Tree from Preorder Traversal in Python
- Program to perform level order traversal of binary tree in C++
- Construct Binary Tree from Preorder and Inorder Traversal in Python
- Preorder Traversal of N-ary Tree Without Recursion in C++
