This is a Python program to print all the paths from the root to all the leaves in the tree.
The program creates a tree and presents a menu to the user to perform various operations including printing all the paths from root to leaves in the tree.
1. Create a class Tree with instance variables key and children.
2. Define methods set_root, add, print_all_paths_to_leaf, print_all_paths_to_leaf_helper and search.
3. The method set_root takes a key as argument and sets the instance variable key equal to it.
4. The method add appends a node to the list children.
5. The method search returns a node with a specified key.
6. The method print_all_paths_to_leaf prints all the paths from root to leaves in the tree. It calls the recursive method print_all_paths_to_leaf_helper.
Here is the source code of a Python program to print all the paths from the root to all the leaves in the tree. The program output is shown below.
class Tree: def __init__(self, data=None): self.key = data self.children = [] def set_root(self, data): self.key = data def add(self, node): self.children.append(node) def search(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 print_all_paths_to_leaf(self): self.print_all_paths_to_leaf_helper([]) def print_all_paths_to_leaf_helper(self, path_till_now): path_till_now.append(self.key) if self.children == []: for key in path_till_now: print(key, end=' ') print() else: for child in self.children: child.print_all_paths_to_leaf_helper(path_till_now[:]) tree = None print('Menu (this assumes no duplicate keys)') print('add <data> at root') print('add <data> below <data>') print('paths') print('quit') while True: do = input('What would you like to do? ').split() operation = do[0].strip().lower() if operation == 'add': data = int(do[1]) new_node = Tree(data) suboperation = do[2].strip().lower() if suboperation == 'at': tree = new_node elif suboperation == 'below': position = do[3].strip().lower() key = int(position) ref_node = None if tree is not None: ref_node = tree.search(key) if ref_node is None: print('No such key.') continue ref_node.add(new_node) elif operation == 'paths': if tree is None: print('Tree is empty.') else: tree.print_all_paths_to_leaf() elif operation == 'quit': break
1. A variable is created to store the binary tree.
2. The user is presented with a menu to perform operations on the tree.
3. The corresponding methods are called to perform each operation.
4. The method print_all_paths_to_leaf is called on the tree to print all the paths from root to leaves in the tree.
Case 1: Menu (this assumes no duplicate keys) add <data> at root add <data> below <data> paths quit What would you like to do? paths Tree is empty. What would you like to do? add 1 at root What would you like to do? paths 1 What would you like to do? add 2 below 1 What would you like to do? paths 1 2 What would you like to do? add 3 below 1 What would you like to do? paths 1 2 1 3 What would you like to do? add 4 below 1 What would you like to do? paths 1 2 1 3 1 4 What would you like to do? add 8 below 2 What would you like to do? paths 1 2 8 1 3 1 4 What would you like to do? add 7 below 2 What would you like to do? paths 1 2 8 1 2 7 1 3 1 4 What would you like to do? quit Case 2: Menu (this assumes no duplicate keys) add <data> at root add <data> below <data> paths quit What would you like to do? add 1 at root What would you like to do? add 10 below 1 What would you like to do? add 11 below 10 What would you like to do? add 12 below 11 What would you like to do? add 20 below 1 What would you like to do? add 21 below 20 What would you like to do? add 30 below 1 What would you like to do? paths 1 10 11 12 1 20 21 1 30 What would you like to do? quit
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Check Information Technology Books
- Apply for Python Internship
- Check Python Books
- Practice Programming MCQs
- Apply for Programming Internship