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)
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)
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 t = lca(root, n1, n2)
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))