def exec(): root = BinaryNode(1) root.left = BinaryNode(2) root.right = BinaryNode(3) root.right.right = BinaryNode(4) root.right.left = BinaryNode(5) root.left.left = BinaryNode(6) root.left.right = BinaryNode(7) list = buildDepthList([], root) for llist in list: print(llist)
def test_add_node(root_node: BinaryNode): values = [] items = [{ "index": 7, "data": 'G' }, { "index": 8, "data": "last" }, { "index": 6, "data": "F" }] for item in items: root_node.add_node(**item) root_node.traverse_inorder(processor=lambda x: values.append({ "index": x.index, "name": x.data })) print(values)
def __init__(self, root_node: BinaryNode): self.window = tk.Tk() self.window.title("Tree") self.window.protocol("WM_DELETE_WINDOW", self.kill_callback) canvas = tk.Canvas( self.window, bg="white", borderwidth=2, relief=tk.SUNKEN, ) canvas.pack(padx=10, pady=10, fill=tk.BOTH, expand=True) root_node.position_subtree(10, 10) root_node.draw_subtree_links(canvas, "black") root_node.draw_subtree_nodes(canvas, "white", "blue") self.window.focus_force() self.window.mainloop()
second = node_one else: first = node_two second = node_one # This brings us to an equal playing field, is important for the following while loop. second = go_up_by_levels(second, abs(h_one - h_two)) # This check only works because we force second to be of that of the # same height as first, this means we can check on by one as we go up levels # if the nodes are the same, then we have found the ancestor! while (first != second and first != None and second != None): first = first.parent second = second.parent # This means either reached the top and the while did not detect where first equalled second # We could not find ancestor :/ ret None. if (first == None or second == None): return None # Else first and second are equal now so, return either. return first root = BinaryNode(1) root.set_left(BinaryNode(2)) root.set_right(BinaryNode(3)) root.left.set_left(BinaryNode(4)) root.left.set_right(BinaryNode(5)) root.right.set_right(BinaryNode(6)) root.right.set_left(BinaryNode(7)) common = find_first_common_ancestor(root.right.right, root.right.left) print(common)
def test_traverse_deep_first(root_node: BinaryNode): values = [] root_node.traverse_deep_first(processor=lambda x: values.append(x.data)) print(values) assert values == ['D', 'B', 'E', 'A', 'C']
def test_traverse_postorder(root_node: BinaryNode): values = [] root_node.traverse_postorder(processor=lambda x: values.append(x.data)) print(values) assert values == ['A', 'C', 'B', 'E', 'D']
def create_test_tree(): root_node = BinaryNode(data="D", index=4) b_node = BinaryNode(data="B", index=2) e_node = BinaryNode(data="E", index=5) a_node = BinaryNode(data="A", index=1) c_node = BinaryNode(data="C", index=3) root_node.left_child = b_node root_node.right_child = e_node b_node.left_child = a_node b_node.right_child = c_node return root_node
def test_find_node(root_node: BinaryNode): node_ = root_node.find_node(7) assert node_ is not None assert node_.index == 7
print(values) def test_find_node(root_node: BinaryNode): node_ = root_node.find_node(7) assert node_ is not None assert node_.index == 7 if __name__ == "__main__": root = create_test_tree() test_traverse_preorder(root) test_traverse_inorder(root) test_traverse_postorder(root) test_traverse_deep_first(root) test_add_node(root) test_find_node(root) root_node = BinaryNode(data="A", index=8) root_node.add_node(data="B", index=3) root_node.add_node(data="C", index=10) root_node.add_node(data="D", index=1) root_node.add_node(data="D", index=6) root_node.add_node(data="D", index=14) root_node.add_node(data="D", index=4) root_node.add_node(data="D", index=7) root_node.add_node(data="D", index=13) root_node.delete_node(3) AppWindow(root_node)
def create_tree(): root = BinaryNode(12) root.insert_node(6) root.insert_node(14) root.insert_node(3) root.insert_node(7) root.insert_node(13) root.insert_node(18) print('\n\nNode entry order: 12, 6, 14, 3, 7, 13, 18') print('This uses an in-order traversal method.') print('Printing the sorted tree') root.print_tree() print('\n\nNode entry order: 12, 6, 14, 3, 7, 13, 18') print('This uses a pre-order traversal method.') print('Printing the sorted tree') root.print_tree_pre_order() print('\n\nNode Search Tests:') root.search_tree(13) root.search_tree(6), root.search_tree(5)
from binary_node import BinaryNode, make_binary_tree zero = BinaryNode(0) one = BinaryNode(1) three = BinaryNode(3) five = BinaryNode(5) eight = BinaryNode(8) ten = BinaryNode(10) nine = BinaryNode(9) four = BinaryNode(4) zero.left_child = one zero.right_child = three one.left_child = five one.right_child = eight three.left_child = ten three.right_child = nine nine.left_child = four # print(zero.to_string()) def visit(value): print(value) # zero.traverse_in_order(visit) # zero.traverse_pre_order(visit)