\ 4 Pre-order: [5, 2, 1, 3, 4, 7, 6, 8] """ root = Node(5, left=Node(2, left=Node(1), right=Node(3, right=Node(4) ) ), right=Node(7, left=Node(6), right=Node(8) ) ) print("\nInput Tree - \n") print_tree(root) print("\n************* SERIALIZATION *************\n") obj = BSTSerialization() serialized_tree = obj.serialize(root) print("Serialized Tree (Preorder) -", serialized_tree) print("\n************* DE-SERIALIZATION *************\n") deserialized_tree_root = obj.deserialize_v2(serialized_tree) print("\nOutput Tree - \n") print_tree(deserialized_tree_root)
30 70 \ 80 \ 90 """ root = None root = insert(root, 50) insert(root, 30) insert(root, 70) insert(root, 80) insert(root, 90) print("\n ---- Unbalanced BST ----\n") print_tree(root) print("\n ---- Balanced BST ----\n") balanced_root = balance_tree(root) print_tree(balanced_root) """ Balanced BST -> 70 / \ 30 80 \ \ 50 90 """
12 15 / \ / 25 30 36 """ n10 = Node(10) n12 = Node(12) n25 = Node(25) n30 = Node(30) n15 = Node(15) n36 = Node(36) root = n10 n10.left = n12 n10.right = n15 n12.left = n25 n12.right = n30 n15.left = n36 print_tree(root) print("\nDDL\n") binary_tree_to_DDL.head = None binary_tree_to_DDL.prev = None binary_tree_to_DDL(root) # 25 <--> 12 <--> 30 <--> 10 <--> 36 <--> 15 print_DDL(binary_tree_to_DDL.head)
and check_isoporphism(root1.right, root2.left)) # Flipped ) if __name__ == '__main__': ''' 2 3 4 5 6 7 8 2 4 3 8 7 5 6 ''' root1: Node = Node(2, left=Node(3, left=Node(5), right=Node(6)), right=Node(4, left=Node(7), right=Node(8))) root2: Node = Node(2, left=Node(4, left=Node(8), right=Node(7)), right=Node(3, left=Node(5), right=Node(6))) print("Tree: 1") print_tree(root1) print("\nTree: 2") print_tree(root2) are_isomorphic = check_isoporphism(root1, root2) print(are_isomorphic)
# Driver program to test above function if __name__ == "__main__": """ Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 """ root = None root = insert(root, 50) insert(root, 30) insert(root, 20) insert(root, 40) insert(root, 70) insert(root, 60) insert(root, 80) # move_children_left(root) move_children_left_v2(root) #move_children_right(root) print_tree(root) # 50 : left->70 , right->NULL # 70 : left->80 , right->NULL # 80 : left->60 , right->NULL # 60 : left->30 , right->NULL # 30 : left->40 , right->NULL # 40 : left->20 , right->NULL # 20 : left->NULL , right->NULL
# Driver program to test above function if __name__ == "__main__": """ Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 / \ 15 25 """ root = None root = insert(root, 50) insert(root, 30) insert(root, 20) insert(root, 15) insert(root, 25) insert(root, 40) insert(root, 70) insert(root, 60) insert(root, 80) print("\n----- Tree -----\n") print_tree(root) print("\n----- Mirror -----\n") mirror_root = deepcopy(root) convert_to_mirror(mirror_root) print_tree(mirror_root) print("\n---- Are mirror ? ----\n", is_mirror(root, mirror_root))
30 70 \ 80 \ 90 """ root = None root = insert(root, 50) insert(root, 30) insert(root, 70) insert(root, 80) insert(root, 90) print ("\n ---- Unbalanced BST ----\n") print_tree(root) print ("\n ---- Balanced BST ----\n") balanced_root = balance_tree(root) print_tree(balanced_root) """ Balanced BST -> 70 / \ 30 80 \ \ 50 90 """