|
| 1 | +# This file contains the basic structure of tree |
| 2 | + |
| 3 | +import sys |
| 4 | +from tree_utils import * |
| 5 | + |
| 6 | + |
| 7 | +# Finding maximum element in a binary tree |
| 8 | +def find_max(root): |
| 9 | + max_val = -sys.maxint -1 |
| 10 | + if root: |
| 11 | + root_val = root.data |
| 12 | + left_val = find_max(root.left) |
| 13 | + right_val = find_max(root.right) |
| 14 | + if left_val > right_val: |
| 15 | + max_val = left_val |
| 16 | + else: |
| 17 | + max_val = right_val |
| 18 | + if max_val < root_val: |
| 19 | + max_val = root_val |
| 20 | + return max_val |
| 21 | + |
| 22 | + |
| 23 | +# Finding an element in Binary Tree |
| 24 | +def find_element(root, data): |
| 25 | + if root: |
| 26 | + if root.data == data: |
| 27 | + return 1 |
| 28 | + else: |
| 29 | + temp = find_element(root.left, data) |
| 30 | + if temp != 0: |
| 31 | + return temp |
| 32 | + else: |
| 33 | + return find_element(root.right, data) |
| 34 | + return 0 |
| 35 | + |
| 36 | + |
| 37 | +# Finding size of tree : Number of nodes in tree |
| 38 | +def size_of_tree(root): |
| 39 | + if not root: |
| 40 | + return 0 |
| 41 | + else: |
| 42 | + left_size = size_of_tree(root.left) |
| 43 | + right_size = size_of_tree(root.right) |
| 44 | + return left_size + 1 + right_size |
| 45 | + |
| 46 | + |
| 47 | +# Printing level order data in reverse order : 1 2 3 4 5 6 7 -> 4 5 6 7 2 3 1 |
| 48 | +def reverse_level_order(root): |
| 49 | + level_queue = list() |
| 50 | + level_stack = list() |
| 51 | + level_queue.append(root) |
| 52 | + while level_queue: |
| 53 | + curr_node = level_queue.pop(0) |
| 54 | + if curr_node.right: |
| 55 | + level_queue.append(curr_node.right) |
| 56 | + if curr_node.left: |
| 57 | + level_queue.append(curr_node.left) |
| 58 | + level_stack.append(curr_node) |
| 59 | + while level_stack: |
| 60 | + curr_node = level_stack.pop() |
| 61 | + print curr_node.data, |
| 62 | + |
| 63 | + |
| 64 | +# LeftView of Tree |
| 65 | +def left_view_of_tree(root): |
| 66 | + if root: |
| 67 | + print root.data |
| 68 | + if root.left: |
| 69 | + left_view_of_tree(root.left) |
| 70 | + else: |
| 71 | + left_view_of_tree(root.right) |
| 72 | + |
| 73 | + |
| 74 | +# Height of Tree |
| 75 | +def height_of_tree(root): |
| 76 | + if not root: |
| 77 | + return -1 |
| 78 | + else: |
| 79 | + left_height = height_of_tree(root.left) |
| 80 | + right_height = height_of_tree(root.right) |
| 81 | + if left_height > right_height: |
| 82 | + max_height = left_height + 1 |
| 83 | + else: |
| 84 | + max_height = right_height + 1 |
| 85 | + return max_height |
| 86 | + |
| 87 | + |
| 88 | +# Bottom View of Tree |
| 89 | +def bottom_view(root): |
| 90 | + if root: |
| 91 | + bottom_view(root.left) |
| 92 | + if not root.left and not root.right: |
| 93 | + print root.data |
| 94 | + bottom_view(root.right) |
| 95 | + |
| 96 | + |
| 97 | +# Leaf Node Count |
| 98 | +def get_leaf_node_count(root): |
| 99 | + if not root: |
| 100 | + return 0 |
| 101 | + |
| 102 | + if root.left is None and root.right is None: |
| 103 | + return 1 |
| 104 | + else: |
| 105 | + return get_leaf_node_count(root.left) + get_leaf_node_count(root.right) |
| 106 | + |
| 107 | +def test(): |
| 108 | + # l1 = TreeNode(4, None, None) |
| 109 | + # l2 = TreeNode(5, None, None) |
| 110 | + # l3 = TreeNode(6, None, None) |
| 111 | + # l4 = TreeNode(7, None, None) |
| 112 | + # l11 = TreeNode(2, l1, l2) |
| 113 | + # l21 = TreeNode(3, l3, l4) |
| 114 | + # root = TreeNode(1, l11, l21) |
| 115 | + |
| 116 | + root = TreeNode(3) |
| 117 | + root.left = TreeNode(2) |
| 118 | + root.right = TreeNode(5) |
| 119 | + root.left.left = TreeNode(1) |
| 120 | + root.left.right = TreeNode(4) |
| 121 | + |
| 122 | + # print "find_element:", find_element(root, 6) |
| 123 | + # print "size_of_tree", size_of_tree(root) |
| 124 | + # print "reverse_level_order: " |
| 125 | + # reverse_level_order(root) |
| 126 | + # print "left_view: " |
| 127 | + # left_view_of_tree(root) |
| 128 | + # print "height: " |
| 129 | + # print height_of_tree(l1) |
| 130 | + bottom_view(root) |
| 131 | + print "Leaf:",get_leaf_node_count(root) |
0 commit comments