def _balance_factor(node): ''' Returns the balance factor of a node. ''' if node is None: return 0 return BinaryTree._height(node.left) - BinaryTree._height(node.right)
def buildParseTree(fpexp): fplist = fpexp.split() pStack = Stack() eTree = BinaryTree('') pStack.push(eTree) currentTree = eTree for i in fplist: if i == '(': currentTree.insertLeft('') pStack.push(currentTree) currentTree = currentTree.getLeftChild() elif i not in ['+', '-', '*', '/', ')']: currentTree.setRootVal(int(i)) parent = pStack.pop() currentTree = parent elif i in ['+', '-', '*', '/']: currentTree.setRootVal(i) currentTree.insertRight('') pStack.push(currentTree) currentTree = currentTree.getLeftChild() elif i == ')': currentTree = pStack.pop() else: raise ValueError return eTree
def _balance_factor(node): if node is None: return 0 return BinaryTree._height(node.left) - BinaryTree._height(node.right)
rh = find_height(node.right) return max(lh, rh) + 1 def level_order(root): # O(n^2) algorithm using recursion, prints elements at every level ht = find_height(root) for i in range(1, ht + 1): print_level(root, i) def print_level(node, level): if not node: return if level == 1: print(node.data) else: print_level(node.left, level - 1) print_level(node.right, level - 1) if __name__ == '__main__': bt = BinaryTree() for i in range(1, 10): bt.add_node(i) #_level_order(bt.root) level_order(bt.root)
childs = [] if not node: childs if node.left_child: childs.append(node.left_child) if node.right_child: childs.append(node.right_child) return childs bin_tree = BinaryTree(1) left_child1 = bin_tree.insert_left(2) right_child2 = bin_tree.insert_right(3) left_child1.insert_left(4) left_child1.insert_right(5) right_child2.insert_left(6) right_child2.insert_right(7) level_order_print(bin_tree) # preorder(bin_tree) # postorder(bin_tree)
def preorder(tree: BinaryTree): if tree: print(tree.get_root_value()) preorder(tree.left_child) preorder(tree.right_child)
else: full_node = True if node.right: if full_node: return False que.put(node.right) else: full_node = True return True if __name__ == '__main__': bt = BinaryTree() for i in range(1, 5): bt.add_node(i) for i in range(2): bt.add_node(None) for i in range(6, 15): bt.add_node(i) bt.print_levelorder() print(check_complete(bt.root)) bt2 = BinaryTree() for j in range(1, 15):
from Trees.BinaryTree import BinaryTree, Node _example0 = BinaryTree() _example1 = BinaryTree() _example1.root = Node(1) _example1.root.left = Node(2) _example1.root.left.left = Node(4) _example1.root.left.right = Node(5) _example1.root.right = Node(3) _example2 = BinaryTree() _example2.root = Node(0) _example2.root.left = Node(1) _example2.root.left.left = Node(2) _example2.root.left.left.left = Node(3) _example2.root.left.left.left.left = Node(4) _example2.root.left.left.left.left.left = Node(5) _example2.root.left.left.left.left.left.left = Node(6) _example2.root.left.left.left.left.left.right = Node(7) _example2.root.left.left.right = Node(8) _example2.root.left.left.right.left = Node(9) _example2.root.left.left.right.right = Node(10) _example2.root.left.right = Node(11) _example2.root.right = Node(12) _example2.root.right.right = Node(13) _example2.root.right.right.right = Node(14) _example3 = BinaryTree() _example3.root = Node(0) _example3.root.left = Node(1)
find_max(node.get_right())) def _find_max(root): """ Non-Recursive implementation for finding the maximum element in the tree :return: """ maximum = -sys.maxsize if not root: return None else: que = Queue.Queue1() que.enqueue(root) while not que.is_empty(): node = que.dequeue() if node.data > maximum: maximum = node.data if node.get_left(): que.enqueue(node.get_left()) if node.get_right(): que.enqueue(node.get_right()) return maximum if __name__ == "__main__": btree = BinaryTree() for i in range(1, 10): btree.add_node(i) #print(find_max(btree.root)) print(_find_max(btree.root))
if current.val >= val: if current.left is None: current.left = TreeNode(val) return return self.insertRecur(current.left, val) else: if current.right is None: current.right = TreeNode(val) return return self.insertRecur(current.right, val) def insert(self, val): if self.root is None: self.root = TreeNode(val) self.insertRecur(self.root, val) if __name__ == "__main__": s = BinarySearchTree() s.root = s.create_bst() s.insert(9) s.insert(17) s.insert(50) s.insert(60) s.insert(70) b = BinaryTree() b.root = s.root print(b) print(s.search(60)) print(s.search(8)) print(s.search(90))