|
| 1 | +""" |
| 2 | +Binary Tree: |
| 3 | +
|
| 4 | +
|
| 5 | + 1 |
| 6 | + / \ |
| 7 | + 2 3 |
| 8 | + / \ / \ |
| 9 | + 4 5 6 7 |
| 10 | +
|
| 11 | +1. Root: is the first node of the tree |
| 12 | +2. Leaf Nodes: Nodes with 0 children/successors |
| 13 | +3. Relationships :Node 1 is the grandparent of node 4 whereas node 4 is the grandchild of node 1 |
| 14 | +4. Ancestor:All the node on the path from node 4 till node 1 is considered at anscestor. |
| 15 | + For ex: Node 2 and 1 are the ancestors of node 4 |
| 16 | +
|
| 17 | +5. Depth of tree: The of the path from a node to root node is depth. The depth of root is 0. |
| 18 | + Example: |
| 19 | + 1 - depth 0 |
| 20 | + / \ |
| 21 | + 2 3 - depth 1 |
| 22 | + / \ / \ |
| 23 | + 4 5 6 7 - depth 2 |
| 24 | + / \ |
| 25 | + 8 9 - depth 3 |
| 26 | +
|
| 27 | +6. Height of the tree: The length of the path from n to its deepest descendent. |
| 28 | +
|
| 29 | + height of the tree = height of root node |
| 30 | + height of leaf node = Always 0 |
| 31 | + |
| 32 | + Example: Height of tree = height of root node = 3 , height of leaf node is 0 |
| 33 | +
|
| 34 | + 1 |
| 35 | + / \ |
| 36 | + 2 3 |
| 37 | + / \ / \ |
| 38 | + 4 5 6 7 |
| 39 | + / \ |
| 40 | + 8 9 |
| 41 | +7. Complete Binary Tree: Except the last level, all the nodes are complete. |
| 42 | + And all the nodes in the last level are as far left as possible |
| 43 | +
|
| 44 | + Example: |
| 45 | +
|
| 46 | + 1 |
| 47 | + / \ |
| 48 | + 2 3 |
| 49 | + / |
| 50 | + 4 |
| 51 | +
|
| 52 | +8. Full Binary Tree: All nodes has exactly 0 or 2 children |
| 53 | +
|
| 54 | + Example: |
| 55 | + 1 |
| 56 | + / \ |
| 57 | + 2 3 |
| 58 | + / \ / \ |
| 59 | + 4 5 6 7 |
| 60 | +
|
| 61 | +9. Traversal: |
| 62 | + i. Pre-order: Root-> Left->Right (+ a b) |
| 63 | + ii. In-order : Left->Root->Right (a + b) |
| 64 | + iii. Post-order : Left->Right->Root (a b +) |
| 65 | +
|
| 66 | + iv. Level order : print values level wise |
| 67 | +
|
| 68 | + Example: o/p 1,2,3,4,5,6,7 |
| 69 | +
|
| 70 | + 1 |
| 71 | + / \ |
| 72 | + 2 3 |
| 73 | + / \ / \ |
| 74 | + 4 5 6 7 |
| 75 | + Implementation: Using Queue |
| 76 | + loop1: 1 o/p= 0 |
| 77 | + loop2: 2,3 o/p= 1 |
| 78 | + loop3: 3,4,5 o/p= 1->2 |
| 79 | + loop4: 4,5,6,7 o/p= 1->2->3 |
| 80 | + loop5: 5, 6, ,7 o/p= 1->2->3->4 |
| 81 | + loop6: 6, 7 o/p= 1->2->3->4->5 |
| 82 | + loop7 : 7 o/p= 1->2->3->4->5->6 |
| 83 | + loop 8: - o/p= 1->2->3->4->5->6->7 |
| 84 | +
|
| 85 | + v. Reverse Level order: print nodes in reverse |
| 86 | + Example: o/p 4,5,6,7,2,3,1 |
| 87 | +
|
| 88 | + 1 |
| 89 | + / \ |
| 90 | + 2 3 |
| 91 | + / \ / \ |
| 92 | + 4 5 6 7 |
| 93 | +10. Height of Tree |
| 94 | +
|
| 95 | + |
| 96 | +""" |
| 97 | +class Queue(object): |
| 98 | + def __init__(self): |
| 99 | + self.items = [] |
| 100 | + |
| 101 | + def enqueue(self,value): |
| 102 | + return self.items.append(value) |
| 103 | + |
| 104 | + |
| 105 | + def dequeue(self): |
| 106 | + return self.items.pop(0) |
| 107 | + |
| 108 | + def is_empty(self): |
| 109 | + if len(self.items) == 0: |
| 110 | + return True |
| 111 | + return False |
| 112 | + |
| 113 | + def peek(self): |
| 114 | + if not self.is_empty(): |
| 115 | + return self.items[0] |
| 116 | + |
| 117 | + def __len__(self): |
| 118 | + return self.size() |
| 119 | + |
| 120 | + def size(self): |
| 121 | + return len(self.items) |
| 122 | + |
| 123 | + def print_queue(self): |
| 124 | + print(self.items) |
| 125 | + |
| 126 | +class Stack(object): |
| 127 | + |
| 128 | + def __init__(self): |
| 129 | + self.items = [] |
| 130 | + |
| 131 | + def push(self,value): |
| 132 | + return self.items.append(value) |
| 133 | + |
| 134 | + def pop(self): |
| 135 | + return self.items.pop() |
| 136 | + |
| 137 | + def peek(self): |
| 138 | + return self.items[-1] |
| 139 | + |
| 140 | + def __len__(self): |
| 141 | + return self.size() |
| 142 | + |
| 143 | + def size(self): |
| 144 | + return len(self.items) |
| 145 | + |
| 146 | + def is_empty(self): |
| 147 | + return len(self.items) == 0 |
| 148 | + |
| 149 | +class Node(object): |
| 150 | + def __init__(self,value): |
| 151 | + self.value = value |
| 152 | + self.left = None |
| 153 | + self.right = None |
| 154 | + |
| 155 | +class BinaryTree(object): |
| 156 | + def __init__(self,root): |
| 157 | + self.root = Node(root) |
| 158 | + |
| 159 | + def print_tree(self,traversal_type): |
| 160 | + if traversal_type == "preorder": |
| 161 | + return self.pre_order(self.root,"")+ "None" |
| 162 | + if traversal_type == "inorder": |
| 163 | + return self.in_order(self.root,"")+ "None" |
| 164 | + if traversal_type == "postorder": |
| 165 | + return self.post_order(self.root,"") + "None" |
| 166 | + if traversal_type == "levelorder": |
| 167 | + return self.level_order(self.root,"") + "None" |
| 168 | + if traversal_type == "reverselevelorder": |
| 169 | + return self.reverse_level_order(self.root, "") +"None" |
| 170 | + |
| 171 | + def pre_order(self,start,output): |
| 172 | + if start: |
| 173 | + output += str(start.value)+"->" |
| 174 | + output = self.pre_order(start.left,output) |
| 175 | + output = self.pre_order(start.right,output) |
| 176 | + return output |
| 177 | + |
| 178 | + def in_order(self,start,output): |
| 179 | + if start: |
| 180 | + output = self.in_order(start.left,output) |
| 181 | + output += str(start.value) + "->" |
| 182 | + output = self.in_order(start.right,output) |
| 183 | + return output |
| 184 | + |
| 185 | + def post_order(self,start,output): |
| 186 | + if start: |
| 187 | + output = self.post_order(start.left,output) |
| 188 | + output = self.post_order(start.right,output) |
| 189 | + output += str(start.value) + "->" |
| 190 | + return output |
| 191 | + |
| 192 | + def level_order(self,start,output): |
| 193 | + |
| 194 | + q = Queue() |
| 195 | + |
| 196 | + if start: |
| 197 | + q.enqueue(start) |
| 198 | + |
| 199 | + while q.size()>0: |
| 200 | + |
| 201 | + output += str(q.peek().value)+"->" |
| 202 | + |
| 203 | + deQ = q.dequeue() |
| 204 | + if deQ.left: |
| 205 | + q.enqueue(deQ.left) |
| 206 | + if deQ.right: |
| 207 | + q.enqueue(deQ.right) |
| 208 | + |
| 209 | + return output |
| 210 | + |
| 211 | + def reverse_level_order(self,start,output): |
| 212 | + |
| 213 | + q = Queue() |
| 214 | + s = Stack() |
| 215 | + if start: |
| 216 | + q.enqueue(start) |
| 217 | + |
| 218 | + while q.size()>0: |
| 219 | + deQ = q.dequeue() |
| 220 | + s.push(deQ.value) |
| 221 | + |
| 222 | + if deQ.right: |
| 223 | + q.enqueue(deQ.right) |
| 224 | + |
| 225 | + if deQ.left: |
| 226 | + q.enqueue(deQ.left) |
| 227 | + |
| 228 | + while not s.is_empty(): |
| 229 | + output += str(s.peek()) + "->" |
| 230 | + s.pop() |
| 231 | + |
| 232 | + return output |
| 233 | + |
| 234 | + def height_of_tree(self,start): |
| 235 | + |
| 236 | + |
| 237 | + if start is None: |
| 238 | + return -1 |
| 239 | + left_height = self.height_of_tree(start.left) |
| 240 | + right_height = self.height_of_tree(start.right) |
| 241 | + return max(left_height,right_height)+1 |
| 242 | + |
| 243 | + def size_of_tree(self,start): |
| 244 | + if start is None: |
| 245 | + return 0 |
| 246 | + else: |
| 247 | + return 1 + self.size_of_tree(start.left) + self.size_of_tree(start.right) |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | +tree = BinaryTree('F') |
| 252 | +tree.root.left = Node('B') |
| 253 | +tree.root.right = Node('G') |
| 254 | +tree.root.left.left = Node('A') |
| 255 | +tree.root.left.right = Node('D') |
| 256 | +tree.root.left.right.left = Node('C') |
| 257 | +tree.root.left.right.right = Node('E') |
| 258 | +tree.root.right.right = Node('I') |
| 259 | +tree.root.right.right.left = Node('H') |
| 260 | + |
| 261 | + |
| 262 | + |
| 263 | +print("pre-order",tree.print_tree("preorder")) |
| 264 | +print("in-order",tree.print_tree("inorder")) |
| 265 | +print("post-order",tree.print_tree("postorder")) |
| 266 | +print("level order",tree.print_tree("levelorder")) |
| 267 | +print("reverse level order",tree.print_tree("reverselevelorder")) |
| 268 | +print("height of tree",tree.height_of_tree(tree.root)) |
| 269 | +print("size of tree",tree.size_of_tree(tree.root)) |
| 270 | + |
| 271 | + |
| 272 | + |
| 273 | + |
0 commit comments