def build_from_PostIn(postorder, inorder): if inorder and postorder: index = inorder.index(postorder.pop()) root = Node(inorder[index]) root.right = build_from_PostIn(postorder, inorder[index + 1:]) root.left = build_from_PostIn(postorder, inorder[:index]) return root
def _create_min_bst(self, array, start, end): if end < start: return None mid = (start + end) // 2 node = Node(array[mid]) node.left = self._create_min_bst(array, start, mid - 1) node.right = self._create_min_bst(array, mid + 1, end) return node
def build_from_PreIn(preorder, inorder): if inorder and preorder: index = inorder.index(preorder[0]) root = Node(inorder[index]) root.left = build_from_PreIn(preorder[1:index + 1], inorder[:index]) root.right = build_from_PreIn(preorder[index + 1:], inorder[index + 1:]) return root
def test_get(): root = Node('b', 5) root.left = Node('a', 3) root.right = Node('c', 2) bst = BST(root) assert bst.get('a') == 3 assert bst.get('c') == 2 assert bst.get('d') is None
def print_Trees(first, last): trees = [] for i in range(first, last + 1): for j in print_Trees(first, i - 1): for y in print_Trees(i + 1, last): # print("hi") BT = Binary_Tree() root = Node(i) root.left = j root.right = y BT.root = root trees.append(root) return trees or [None]
from BST import Node def inorder(root,arr): if root: inorder(root.left,arr) arr.append(root.key) inorder(root.right,arr) def isBST(root): arr = [] inorder(root,arr) return arr == sorted(arr) root = Node(6) root.left = Node(9) root.right = Node(10) root.left.left = Node(17) root.left.right = Node(4) root.right.right = Node(11) print isBST(root) root = Node(6) root.left = Node(4) root.right = Node(7) print isBST(root)
from BST import Node def inorder(root, arr): if root: inorder(root.left, arr) arr.append(root.key) inorder(root.right, arr) def isBST(root): arr = [] inorder(root, arr) return arr == sorted(arr) root = Node(6) root.left = Node(9) root.right = Node(10) root.left.left = Node(17) root.left.right = Node(4) root.right.right = Node(11) print isBST(root) root = Node(6) root.left = Node(4) root.right = Node(7) print isBST(root)
root = new_node else: if node.key > root.key: if root.right is None: root.right = node else: Insert_rec(root.right, node) else: if root.left is None: root.left = node else: Insert_rec(root.left, node) root = Node(20) root.left = Node(8) root.right = Node(22) root.left.left = Node(4) root.left.right = Node(12) root.left.right.left = Node(10) root.left.right.right = Node(14) n1 = 10 n2 = 14 t = lca(root, n1, n2) print "LCA of %d and %d is %d" % (n1, n2, t.key) n1 = 14 n2 = 8 t = lca(root, n1, n2) print "LCA of %d and %d is %d" % (n1, n2, t.key)
tree = BST() tree.add(1) tree.add(0) tree.remove(1) assert tree.root.key == 0 tree.remove(0) assert tree.root is None ################################# ## Validate BST ################################# n1 = Node(1) root = Node(2) root.left = n1 def is_bst(root): if root is None: return True if root.left and root.left.key > root.key: return False if root.right and root.right.key < root.key: return False return is_bst(root.left) and is_bst(root.right) assert is_bst(root) == True fake_tree = Node(0, right=n1) n1.left = root
from BST import Node def max_sum(root): if root is None: return 0 l = max_sum(root.left) r = max_sum(root.right) max_single = max(max(l, r) + root.key, root.key) max_top = max(max_single, l + r + root.key) max_sum.res = max(max_sum.res, max_top) return max_single def max_util(root): max_sum.res = float("-inf") max_sum(root) return max_sum.res root = Node(10) root.left = Node(2) root.right = Node(10) root.left.left = Node(20) root.left.right = Node(1) root.right.right = Node(-25) root.right.right.left = Node(3) root.right.right.right = Node(4) print max_util(root)
if not root: root = new_node else: if node.key > root.key: if root.right is None: root.right = node else: Insert_rec(root.right,node) else: if root.left is None: root.left = node else: Insert_rec(root.left,node) root = Node(20) root.left = Node(8) root.right = Node(22) root.left.left = Node(4) root.left.right = Node(12) root.left.right.left = Node(10) root.left.right.right = Node(14) n1 = 10 ; n2 = 14 t = lca(root, n1, n2) print "LCA of %d and %d is %d" %(n1, n2, t.key) n1 = 14 ; n2 = 8 t = lca(root, n1, n2) print "LCA of %d and %d is %d" %(n1, n2 , t.key) n1 = 10 ; n2 = 22
from BST import Node def max_sum(root): if root is None: return 0 l = max_sum(root.left) r = max_sum(root.right) max_single = max(max(l,r)+root.key,root.key) max_top = max(max_single,l+r+root.key) max_sum.res = max(max_sum.res,max_top) return max_single def max_util(root): max_sum.res = float("-inf") max_sum(root) return max_sum.res root = Node(10) root.left = Node(2) root.right = Node(10) root.left.left = Node(20) root.left.right = Node(1) root.right.right = Node(-25) root.right.right.left = Node(3) root.right.right.right = Node(4) print max_util(root)
from BST import Node def validate_tree(root): if not root or (not root.left and not root.right): return True #for an imbalanced tree, there has to be cond to check if one of the childs is none if (root.left and root.right and root.left.value < root.value and root.right.value > root.value) or ( root.left.value < root.value and not root.right) or (root.right.value > root.value and not root.left): return validate_tree(root.left) and validate_tree(root.right) return False n = Node(2) n.left = Node(1) n.left.left = Node(0) n.right = Node(3) print(validate_tree(n))