|
| 1 | +""" |
| 2 | +GENERAL TREE |
| 3 | + Data is stored in hierarchical form |
| 4 | +
|
| 5 | + ROOT --- Level 0 |
| 6 | + ____________________|____________________ |
| 7 | + A B C --- Level 1 |
| 8 | + _____|_____ _____|_____ _____|_____ |
| 9 | + | | | | | | | | | |
| 10 | + D E F G H I J K L --- Level 2 |
| 11 | + _____|_____ _____|_____ |
| 12 | + | | | | | | |
| 13 | + M N O P Q R --- Level 3 |
| 14 | +
|
| 15 | +
|
| 16 | +
|
| 17 | +
|
| 18 | + Here, ROOT is "ROOT NODE" and A, B, C are "CHILD NODE" |
| 19 | + > B-D-F-G is a sub-tree |
| 20 | + > B is 'ROOT NODE' for F, G, H and G is 'ROOT NODE' for Q, R |
| 21 | + > Those nodes [E, F, H, I, J, K, L...] who do not have any child node are "LEAF NODE" |
| 22 | + > For (D, E), A will be Ancestor and for A, (D, E) would be Descendants |
| 23 | + Tree is a recursive dta structure where a child node is another tree in itself |
| 24 | +
|
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +class TreeNode: |
| 29 | + """ Class TreeNode """ |
| 30 | + def __init__(self, data): |
| 31 | + self.data = data |
| 32 | + self.children = [] |
| 33 | + self.parent = None |
| 34 | + |
| 35 | + def add_child(self, child): |
| 36 | + """ Adds children elements to Tree """ |
| 37 | + child.parent = self # child is an instance of class(node) |
| 38 | + # adding a child node to tree |
| 39 | + self.children.append(child) |
| 40 | + |
| 41 | + |
| 42 | + def get_level(self): |
| 43 | + """ Gets the level of Tree by counting ancestors: if a node has no ancestor, it is root node """ |
| 44 | + level = 0 # initialised level to 0 |
| 45 | + p = self.parent |
| 46 | + while p: |
| 47 | + """keep on going through parents and increasing levels""" |
| 48 | + level += 1 # increase level by 1 |
| 49 | + p = p.parent |
| 50 | + return level |
| 51 | + |
| 52 | + |
| 53 | + def print_tree(self): |
| 54 | + """Display Tree in hierarchical format""" |
| 55 | + spaces = ' ' * self.get_level() * 3 # printing 3 spaces for each level |
| 56 | + prefix = spaces + "|__" if self.parent else "" |
| 57 | + print(prefix + self.data) # prints prefix(probably spaces) based on level |
| 58 | + |
| 59 | + # At leaf nodes, self.children will be an empty array... so we create a check if self.children() is an empty array or not |
| 60 | + if self.children: # checks if len(self.children > 0) |
| 61 | + for child in self.children: |
| 62 | + child.print_tree() # It will recursively call this fn and print the sub-trees |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +def electronic_product(): |
| 68 | + root = TreeNode("Electronics") |
| 69 | + |
| 70 | + laptop = TreeNode("Laptop") |
| 71 | + laptop.add_child(TreeNode("Mac")) |
| 72 | + laptop.add_child(TreeNode("Surface")) |
| 73 | + laptop.add_child(TreeNode("Thinkpad")) |
| 74 | + |
| 75 | + cellphones = TreeNode("cell Phone") |
| 76 | + cellphones.add_child(TreeNode("iphone")) |
| 77 | + cellphones.add_child(TreeNode("Samsung")) |
| 78 | + cellphones.add_child(TreeNode("Vivo")) |
| 79 | + |
| 80 | + tv = TreeNode("TV") |
| 81 | + tv.add_child(TreeNode("Samsung")) |
| 82 | + tv.add_child(TreeNode("MI")) |
| 83 | + |
| 84 | + |
| 85 | + root.add_child(laptop) |
| 86 | + root.add_child(cellphones) |
| 87 | + root.add_child(tv) |
| 88 | + |
| 89 | + # print(laptop.get_level()) # 1 |
| 90 | + # print(cellphones.get_level()) # 1 |
| 91 | + # print(tv.get_level()) # 1 |
| 92 | + |
| 93 | + return root |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +# main method |
| 100 | +if __name__ == '__main__': |
| 101 | + root = electronic_product() |
| 102 | + root.print_tree() # prints tree in hierarchical format |
| 103 | + |
| 104 | + # print(root.get_level()) # 0 // get level of root node |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
0 commit comments