- 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 postorder tree traversal
In Go, postorder tree traversal is a technique in which the left child is visited first, then the right child, and finally the root node is the definition of the order. This order is frequently used to carry out certain activities on a tree, including releasing memory that the tree has consumed. We will implement postorder tree traversal using two methods the first one uses slices and the second one uses stacks.
Syntax
func append(slice, element_1, element_2…, element_N) []T
The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.
Method 1: Using Slices
In this method, the root node is first examined to see if it is null. If so, we give back an empty slice. If not, we proceed to explore the left and right subtrees and add the outcomes to our result slice. We then return the result slice after adding the value of the current node to the result.
Algorithm
Step 1 − Create a package main and declare fmt(format package) and strings package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 − Create a TreeNode struct to represent the binary tree's nodes.
Step 3 − Create a function called post_order_traversal to go over the tree in postorder.
Step 4 − Verify whether the root node is null. Return an empty slice if it is.
Step 5 − Call post_order_traversal on the Left child node to iteratively explore the left subtree.
Step 6 − Call post_order_traversal on the Right child node to iteratively navigate the right subtree.
Step 7 − Add the current node's value to the result slice.
Step 8 − Return the result slice.
Step 9 − The postorder traversal function iterates recursively across the tree, recording the outcome in a slice. The function adds the nodes' values to the result slice in the postorder traversal order in which they are encountered.
Example
In this example we will use slices to store result after left and right sub trees are explored.
package main import "fmt" // Definition for a binary tree node. type TreeNode struct { Val int Left_val *TreeNode Right_val *TreeNode } func post_order_traversal(root *TreeNode) []int { var result []int if root == nil { return result } result = append(result, post_order_traversal(root.Left_val)...) result = append(result, post_order_traversal(root.Right_val)...) result = append(result, root.Val) return result } func main() { root := &TreeNode{Val: 10} //assign values to the list root.Left_val = &TreeNode{Val: 20} root.Right_val = &TreeNode{Val: 30} root.Left_val.Left_val = &TreeNode{Val: 40} root.Left_val.Right_val = &TreeNode{Val: 50} result := post_order_traversal(root) fmt.Println("The postorder traversal is created here:") fmt.Println(result) //print the postorder traversal }
Output
The postorder traversal is created here: [40 50 20 30 10]
Method 2: Using stacks to implement postorder traversal
in this method, to keep track of the nodes to visit during the postorder traversal we use a stack. The root node is first pushed onto the stack. We remove the top node from the stack after each iteration, add its value to the result slice, and then push the top node's right and left child nodes back onto the stack. By visiting the nodes in reverse order in this manner, we can build the result slice by appending values to the front of the result slice in the proper postorder order.
Algorithm
Step 1 − Create a package main and declare fmt(format package) and strings package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 − Create a TreeNode struct to represent the binary tree's nodes.
Step 3 − Create a function called post_order_traversal to go over the tree in postorder.
Step 4 − Verify whether the root node is null. Return an empty slice if it is.
Step 5 − Create a stack from scratch and add the root node to it.
Step 6 − Repeat the steps until the stack is empty.
a. Remove the stack's top node.
b. Add the node's value as a prefix to the result slice.
c. Add the left and right child nodes to the stack.
Step 7 − Return the result of slice
Step 8 − The result is printed on the console using fmt.Println() function where ln means new line.
Example
In this example we will use stacks to implement postorder traversal.
package main import "fmt" // Definition for a binary tree node. type TreeNode struct { Val int Left_val *TreeNode Right_val *TreeNode } func post_order_traversal(root *TreeNode) []int { var result []int if root == nil { return result } stack := []*TreeNode{root} for len(stack) > 0 { node := stack[len(stack)-1] stack = stack[:len(stack)-1] result = append([]int{node.Val}, result...) if node.Left_val != nil { stack = append(stack, node.Left_val) } if node.Right_val != nil { stack = append(stack, node.Right_val) } } return result } func main() { root := &TreeNode{Val: 10} //assign the values to the list root.Left_val = &TreeNode{Val: 20} root.Right_val = &TreeNode{Val: 30} root.Left_val.Left_val = &TreeNode{Val: 40} root.Left_val.Right_val = &TreeNode{Val: 50} result := post_order_traversal(root) fmt.Println("The postorder traversal created here is:") fmt.Println(result) }
Output
The postorder traversal created here is: [40 50 20 30 10]
Conclusion
We executed the program of postorder tree traversal using two examples. In the first example we used result slice to store the result after left and right subtree is explored and in the second example, we used stack to perform the operation
- Related Articles
- Golang program to perform inorder tree traversal
- Golang Program to Perform the preorder tree traversal
- C++ Program to Perform Postorder Recursive Traversal of a Given Binary Tree
- Golang Program to traverse a given tree in Postorder Traversal (Recursive).
- C++ Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
- Java Program to Perform the inorder tree traversal
- Postorder Tree Traversal in Data Structures
- Binary Tree Postorder Traversal in Python
- Binary Tree Postorder Traversal in Python Programming
- Python Program to Build Binary Tree if Inorder or Postorder Traversal as Input
- Program to perform level order traversal of binary tree in C++
- Construct Binary Tree from Inorder and Postorder Traversal in Python
- Construct Binary Tree from Preorder and Postorder Traversal in Python
- C++ Program to Perform Preorder Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree
