def hier2bintree (l, n =1): if n >= len(l) or l[n] == None: return None else: B = bintree.BinTree(l[n],None,None) B.left = hier2bintree(l,n*2) B.right = hier2bintree(l,n*2+1) return B
def addparent (B, P = None): if B == None : return None else : C = bintree.BinTree((B.key, P),None,None) C.left = addparent(B.left,C) C.right = addparent(B.right,C) return C
def leaf_insert(B, x): if B == None: return bintree.BinTree(x, None, None) else: if x <= B.key: B.left = leaf_insert(B.left, x) else: B.right = leaf_insert(B.right, x) return B
def __list2bst(L, left, right): if left == right: return None else: mid = left + (right - left) // 2 B = bintree.BinTree(L[mid], None, None) B.left = __list2bst(L, left, mid) B.right = __list2bst(L, mid+1, right) return B
def add_leaf(B, x): if B == None: B = bintree.BinTree(x, None, None) return B else: if x <= B.key: B.left = add_leaf(B.left, x) else: B.right = add_leaf(B.right, x) return B
def __deSerializeTree(serial, i): i += 1 key = serial[i] if key == "#": return None, i else: node = bintree.BinTree(key, None, None) node.left, i = __deSerializeTree(serial, i) node.right, i = __deSerializeTree(serial, i) return node, i
def __build_abr(inf, sup, l): if inf == sup: return None else: n = (((sup - inf) // 2) + inf) while n + 1 < sup and l[n] == l[n + 1]: n += 1 B = bintree.BinTree(l[n], None, None) B.left = __build_abr(inf, n, l) B.right = __build_abr(n + 1, sup, l) return B
def _add_bin(self) -> None: """ private method. Creates a new BinTree and adds it to bin_dict and the AVL tree. """ self.bin_dict[self.bin_count] = bintree.BinTree( width=self.bin_size[0], height=self.bin_size[1]) bin_key = product(*self.bin_dict[self.bin_count].largest_child) self.tree.insert(bin_key) self.tree[bin_key].data.append(self.bin_count) self.bin_count += 1
def simple_test(): # Setup the tree bt = bintree.BinTree() root = bt.addRoot("Root") left = bt.addLeft(root, "Left") right = bt.addRight(root, "Right") # Run the algorithm pre = preorder(bt) # Test its output print "Preorder is in this order: " + str(show_values(pre)) assert pre == [root, left, right], "Preorder missed assertion!"
def postorder_Test(): with pytest.raises(InvalidInputException): postorder(None) bt = bintree.BinTree() assert postorder(bt) == [] root = bt.addRoot("A") left = bt.addLeft(root, "B" ) leftLeftGrandChild = bt.addLeft(left, "D") leftRightGrandChild = bt.addRight(left, "E") right = bt.addRight(root, "C") rightLeftGrandChild = bt.addLeft(right, "F") rightRightGrandChild = bt.addRight(right, "G") nodes = postorder(bt) assert nodes == [leftLeftGrandChild, leftRightGrandChild, left, rightLeftGrandChild, rightRightGrandChild, right, root]
def leaf_insert_iter(B, x): new = bintree.BinTree(x, None, None) P = None T = B while T != None: P = T if x <= T.key: T = T.left else: T = T.right if P == None: return new else: if x <= P.key: P.left = new else: P.right = new return B
def add_root(B, x): (G, D) = __coupe(x, B) return bintree.BinTree(x, G, D)
def root_insertion(B, x): (G, D) = cut(B, x) return bintree.BinTree(x, G, D)