- 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 Count number of leaf nodes in a tree
In Go programming language, a tree is a data structure in which each node has a value and zero to many nodes as children. The root node is the node without a parent, while the leaf node is the node without children. Trees can be employed for a variety of tasks, including data storage, sorting, and searching in hierarchical structures. We will use two methods to count no. of leaf nodes in a tree. In the first method TreeNode struct is used whereas queue is used in the second example to execute the program.
Method 1: Using TreeNode Struct
This method implements the count_leaf_nodes function, which takes a root node as an argument and returns the number of leaf nodes in the tree. It also constructs a tree data structure using a TreeNode struct. The count_leaf_nodes function is invoked by the main function to count the leaf nodes of a sample tree that has been put up. The program will produce 4 as its output.
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 struct called TreeNode that has fields for the node value and pointers to its left and right children to represent a node in the tree.
Step 3 − Create a function called count_leaf_nodes that takes a TreeNode pointer as an argument and returns the number of leaf nodes in the tree.
Step 4 − Verify whether the root node is nil in the count_leaf_nodes function. Return 0 if it is because there are no leaf nodes.
Step 5 − Check to see if the left and right children are nil if the root node is not nil. Return 1 if they are, as this node is a leaf node.
Step 6 − Recursively call countLeafNodes on the left and right children if they are not null, and then return the total of the two results.
Step 7 − Create a TreeNode struct instance in the main function to represent the tree's root node, and set its left and right children to other TreeNode struct instances to represent the rest of the tree.
Step 8 − Use the root node as a parameter when calling the count_leaf_nodes method. The number of leaf nodes in the tree will be the function's output.
Example
In this example, we will use TreeNode struct to implement no. of leaf nodes in a tree.
package main import "fmt" type TreeNode struct { //create a TreeNode struct whose values are to be counted Val int Left_val *TreeNode Right_val *TreeNode } func count_leaf_nodes(root *TreeNode) int { if root == nil { return 0 } if root.Left_val == nil && root.Right_val == nil { return 1 } return count_leaf_nodes(root.Left_val) + count_leaf_nodes(root.Right_val) } func main() { root := &TreeNode{Val: 10, Left_val: &TreeNode{Val: 20, //assign values to the list Left_val: &TreeNode{Val: 40, Left_val: nil, Right_val: nil, }, Right_val: &TreeNode{Val: 50, Left_val: nil, Right_val: nil, }, }, Right_val: &TreeNode{Val: 30, Left_val: &TreeNode{Val: 60, Left_val: nil, Right_val: nil, }, Right_val: &TreeNode{Val: 70, Left_val: nil, Right_val: nil, }, }, } fmt.Println("The total no. of leaf nodes in the tree are:") fmt.Println(count_leaf_nodes(root)) // output:4 }
Output
The total no. of leaf nodes in the tree are: 4
Method 2: Using queue to count no. of leaf nodes in a tree
This method tracks the next nodes to visit using a queue. When adding nodes to the queue, it begins by adding the root node. Then, it repeatedly takes the next node from the front, determines whether it is a leaf node (that is, if its left and right children are nil), and adds its children to the back of the queue if they are not nil. A count variable, which is increased each time a leaf node is encountered, is used to keep track of the number of leaf nodes. After visiting each node, the function returns the count.
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 struct called TreeNode that has fields for the node value and pointers to its left and right children to represent a node in the tree.
Step 3 − Create a function called count_leaf_nodes that takes a TreeNode pointer as an argument and returns the number of leaf nodes in the tree.
Step 4 − Create a queue and set its root node and count to 0.
Step 5 −
a. Dequeue the initial node from the queue while it still contains nodes.
b. Increase the count if the dequeued node is a leaf node (that is, if its left and right children are nil).
c. Enqueue the left child of the dequeued node if it is not null.
d. Enqueue the right child of the dequeued node if it is not null.
Step 6 − Return the count to the function.
Step 7 − Create a TreeNode struct instance in the main function to represent the tree's root node, and set its left and right children to other TreeNode struct instances to represent the rest of the tree.
Example
In this example we will use queue data structure to implement the program.
package main import "fmt" type TreeNode struct { //create a TreeNode struct whose leaf nodes are to be counted Val int Left_val *TreeNode Right_val *TreeNode } func count_leaf_nodes(root *TreeNode) int { if root == nil { return 0 } queue := []*TreeNode{root} //use a queue to count no. of leaf nodes count := 0 for len(queue) > 0 { node := queue[0] queue = queue[1:] if node.Left_val == nil && node.Right_val == nil { count++ } if node.Left_val != nil { queue = append(queue, node.Left_val) } if node.Right_val != nil { queue = append(queue, node.Right_val) } } return count } func main() { root := &TreeNode{Val: 1, Left_val: &TreeNode{Val: 2, Left_val: &TreeNode{Val: 4, //assign the values to the list Left_val: nil, Right_val: nil, }, Right_val: &TreeNode{Val: 5, Left_val: nil, Right_val: nil, }, }, Right_val: &TreeNode{Val: 3, Left_val: &TreeNode{Val: 6, Left_val: nil, Right_val: nil, }, Right_val: &TreeNode{Val: 7, Left_val: nil, Right_val: nil, }, }, } fmt.Println("The no. of leaf nodes in the tree are:") fmt.Println(count_leaf_nodes(root)) //output:4 }
Output
The no. of leaf nodes in the tree are: 4
Conclusion
We executed the program of counting no. of leaf nodes in a tree using two examples. In the first example we used the tree data structure using TreeNode struct and in the second example we used queue data structure to execute the program.
- Related Articles
- Python Program to Count Number of Non Leaf Nodes of a given Tree
- Python Program to Count Number of Leaf Node in a Tree
- Count Non-Leaf nodes in a Binary Tree in C++
- Program to find leaf and non-leaf nodes of a binary tree in Python
- Golang Program to count the number of nodes in a linked list.
- Golang program to count number of nodes in a circular linked list
- Golang program to count the number of nodes in a doubly linked list.
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- Product of all leaf nodes of binary tree in C++
- Program to find number of good leaf nodes pairs using Python
- Print all leaf nodes of a binary tree from right to left in C++
- Number of leaf nodes in the subtree of every node of an n-ary tree in C++
- Golang Program to Count the Number of Digits in a Number
- Count Complete Tree Nodes in C++
- Find the sum of left leaf nodes of a given Binary Tree in C++
