
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Display the Nodes of a Tree using BFS Traversal
When it is required to display the nodes of a tree using the breadth first search traversal, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, perform ‘bfs’ (breadth first search) and so on. An instance of the class can be created to access and use these methods.
Below is a demonstration of the same −
Example
class Tree_struct: def __init__(self, data=None): self.key = data self.children = [] def set_root(self, data): self.key = data def add_node(self, node): self.children.append(node) def search_node(self, key): if self.key == key: return self for child in self.children: temp = child.search_node(key) if temp is not None: return temp return None def bfs_operation(self): queue = [self] while queue != []: popped = queue.pop(0) for child in popped.children: queue.append(child) print(popped.key, end=' ') my_instance = None print('Menu (assume no duplicate keys)') print('add <data> at root') print('add <data> below <data>') print('bfs') print('quit') while True: my_input = input('What operation would you do ? ').split() operation = my_input[0].strip().lower() if operation == 'add': data = int(my_input[1]) new_node = Tree_struct(data) suboperation = my_input[2].strip().lower() if suboperation == 'at': my_instance = new_node elif suboperation == 'below': position = my_input[3].strip().lower() key = int(position) ref_node = None if my_instance is not None: ref_node = my_instance.search_node(key) if ref_node is None: print('No such key') continue ref_node.add_node(new_node) elif operation == 'bfs': if my_instance is None: print('The tree is empty') else: print('Breadth First Search traversal is : ', end='') my_instance.bfs_operation() print() elif operation == 'quit': break
Output
Menu (assume no duplicate keys) add <data> at root add <data> below <data> bfs quit What operation would you do ? add 6 at root What operation would you do ? add 4 below 6 What operation would you do ? add 9 below 4 What operation would you do ? bfs Breadth First Search traversal is : 6 4 9 What operation would you do ? quit
Explanation
The ‘Tree_struct’ class with required attributes is created.
It has an ‘init’ function that is used to create an empty list.
A ‘set_root’ method is defined that helps set the root value of the binary tree.
It has an ‘add_node’ method that helps add elements to the tree.
A method named ‘search_elem’ is defined, that helps search for a specific element.
A method named ‘bfs_operation’ is defined, that helps perform breadth first search traversal on the tree.
An instance is created and assigned to ‘None’.
The user input is taken for the operation that needs to be performed.
Depending on the user’ choice, the operation is performed.
Relevant output is displayed on the console.
- Related Articles
- Python Program to Create a Mirror Copy of a Tree and Display using BFS Traversal
- Count the number of nodes at given level in a tree using BFS in C++
- Python Program to Find All Nodes Reachable from a Node using BFS in a Graph
- Python Program to Find the Largest value in a Tree using Inorder Traversal
- Program to generate tree using preorder and inorder traversal in python
- Python Program to Display the Nodes of a Linked List in Reverse using Recursion
- Python Program to Display all the Nodes in a Linked List using Recursion
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
- Python Program to Find Nth Node in the Inorder Traversal of a Tree
- Python Program to Find the Sum of all Nodes in a Tree
- Program to perform an Inorder Traversal of a binary tree in Python
- Program to find out the lowest common ancestor of a binary tree of given nodes using Python
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Program to print the nodes at odd levels of a tree using C++
- Program to find number of nodes in the sub-tree with the same label using Python
