- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Python Program to Count Number of Leaf Node in a Tree
When it is required to count the number of leaf nodes in a Tree, a ‘Tree_structure’ class is created, methods to add root value, and other children values are defined. Various options are given that the user can select. Based on the user’s choice, the operation is performed on the Tree elements.
Below is a demonstration of the same −
Example
class Tree_structure: def __init__(self, data=None): self.key = data self.children = [] def set_root_node(self, data): self.key = data def add_vals(self, node): self.children.append(node) def search_val(self, key): if self.key == key: return self for child in self.children: temp = child.search(key) if temp is not None: return temp return None def count_leaf_node(self): leaf_nodes = [] self.count_leaf_node_helper_fun(leaf_nodes) return len(leaf_nodes) def count_leaf_node_helper_fun(self, leaf_nodes): if self.children == []: leaf_nodes.append(self) else: for child in self.children: child.count_leaf_node_helper_fun(leaf_nodes) tree = None print('Menu (this assumes no duplicate keys)') print('add <data> at root') print('add <data> below <data>') print('count') print('quit') while True: my_input = input('What operation would you like to perform ? ').split() operation = my_input[0].strip().lower() if operation == 'add': data = int(my_input[1]) newNode = Tree_structure(data) sub_op = my_input[2].strip().lower() if sub_op == 'at': tree = newNode elif sub_op == 'below': my_pos = my_input[3].strip().lower() key = int(my_pos) ref_node = None if tree is not None: ref_node = tree.search_val(key) if ref_node is None: print('No such key.') continue ref_node.add_vals(newNode) elif operation == 'count': if tree is None: print('The tree is empty') else: count = tree.count_leaf_node() print('The number of leaf nodes are : {}'.format(count)) elif operation == 'quit': break
Output
Menu (this assumes no duplicate keys) add <data> at root add <data> below <data> count quit What operation would you like to perform ? add 78 at root What operation would you like to perform ? add 90 below 78 What operation would you like to perform ? add 8 below 78 What operation would you like to perform ? count The number of leaf nodes are : 2 What operation would you like to perform ? quit
Explanation
The ‘Tree_structure’ class is created.
It sets the ‘key’ to True and sets an empty list to children of tree.
It has a ‘set_root’ function that helps set the root value for the Tree.
A method named ‘add_vals’ is defined, that helps add an element to the Tree.
Another method named ‘search_val’ is defined, that helps search for an element in the Tree.
Another method named ‘count_leaf_nodes’ is defined, that helps get the count of the leaf nodes of the Tree.
Another method named ‘count_leaf_nodes_helper’ is defined, that calls the previously defined function- this is a recursive function.
Four options are given, such as ‘add at root’, ‘add below’, ‘count’ and ‘quit’.
Depending on the option given by user, the respective operation is performed.
This output is displayed on the console.
- Related Articles
- Python Program to Count Number of Non Leaf Nodes of a given Tree
- Program to find leaf and non-leaf nodes of a binary tree in Python
- Deepest left leaf node in a binary tree in C++
- Program to find leftmost deepest node of a tree in Python
- Program to find Kth ancestor of a tree node in Python
- Number of leaf nodes in the subtree of every node of an n-ary tree in C++
- Program to find sibling value of a binary tree node in Python
- Count Non-Leaf nodes in a Binary Tree in C++
- Python Program to Find Nth Node in the Inorder Traversal of a Tree
- Program to find second deepest node in a binary tree in python
- Program to print the longest leaf to leaf path in a Binary tree using C++
- Program to find sum of longest sum path from root to leaf of a binary tree in Python
- C++ Program to Find Deepest Left Leaf in a Binary Tree
- Print the nodes of binary tree as they become the leaf node in C++ Programming.
- Program to find number of good leaf nodes pairs using Python
