def test_delete(self):
     # Create a complete binary search tree of 7 items in level-order
     items = [4, 2, 6, 1, 3, 5, 7]
     bst = BinarySearchTree()
     for item in items:
         bst.insert(item)
     # Delete the specific nodes
     bst.delete(7)
     assert bst.contains(7) is False
     bst.delete(2)
     assert bst.contains(2) is False
     assert bst.root.left.data == 1
     assert bst.root.left.left.data is None
f.close()

duplicates = []  # Return the list of duplicates in this data structure

# # Replace the nested for loops below with your improvements
# for name_1 in names_1:
#     for name_2 in names_2:
#         if name_1 == name_2:
#             duplicates.append(name_1)
# For each name in names_1, insert name into a binary search tree
best_n = BinarySearchTree(names_1[0])
for name1 in names_1:
    best_n.insert(name1)

# For each name in names_2, check if bst contains name.
for name2 in names_2:
    # If true, append name from names_2 to duplicates
    if best_n.contains(name2):
        duplicates.append(name2)

duplicates.sort()

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
# What's the best time you can accomplish?  Thare are no restrictions on techniques or data
# structures, but you may not import any additional libraries that you did not write yourself.
Beispiel #3
0
# myList.display()
# print()
# myList.delete_first()
# myList.display()
# print()
# myList.delete(8)
# myList.display()
# print()
# print(myList.size)

# Binary Search Tree
print('*****Binary search tree testing*****')
my_tree = BinarySearchTree()
root = None
root = my_tree.insert(root, 3)
my_tree.insert(root, 7)
my_tree.insert(root, 1)
my_tree.insert(root, 5)
my_tree.insert(root, 10)
print('in order')
my_tree.display_in_order(root)
print('pre order')
my_tree.display_pre_order(root)
print('post order')
my_tree.display_post_order(root)
val = 7
print('contains', val, my_tree.contains(root, val))
val = 2
print('contains', val, my_tree.contains(root, val))
val = 10
print('contains', val, my_tree.contains(root, val))