forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (64 loc) · 2.47 KB
/
Copy pathmain.py
File metadata and controls
81 lines (64 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from typing import Optional
from insert_in_bst import insert
from delete_a_node_in_bst import delete_node
from search_in_bst import search
from mirror_a_bst import create_mirror_bst
from print_in_range import print_in_range
from root_to_leaf_paths import print_root_to_leaf_paths
from validate_bst import is_valid_bst
from tree_node import Node
def main() -> None:
# Create a BST
root: Optional[Node] = None
root = insert(root, 50)
root = insert(root, 30)
root = insert(root, 20)
root = insert(root, 40)
root = insert(root, 70)
root = insert(root, 60)
root = insert(root, 80)
# Print the inorder traversal of the BST
print("Inorder traversal of the original BST:")
print_in_range(root, 10, 90)
# Print the root to leaf paths
print("Root to leaf paths:")
print_root_to_leaf_paths(root, [])
# Check if the tree is a BST
print("Is the tree a BST:", is_valid_bst(root, None, None))
# Delete nodes from the BST
print("Deleting 20 from the BST:")
if root is not None:
root = delete_node(root, 20)
# Print the inorder traversal of the BST
print("Inorder traversal of the BST after deleting 20:")
print_in_range(root, 10, 90)
# Check if the tree is a BST
print("Is the tree a BST:", is_valid_bst(root, None, None))
# Delete nodes from the BST
print("Deleting 30 from the BST:")
if root is not None:
root = delete_node(root, 30)
# Print the inorder traversal of the BST after deleting 30
print("Inorder traversal of the BST after deleting 30:")
print_in_range(root, 10, 90)
# Check if the tree is a BST
print("Is the tree a BST:", is_valid_bst(root, None, None))
# Delete nodes from the BST
print("Deleting 50 from the BST:")
if root is not None:
root = delete_node(root, 50)
# Print the inorder traversal of the BST after deleting 50
print("Inorder traversal of the BST after deleting 50:")
print_in_range(root, 10, 90)
# Check if the tree is a BST
print("Is the tree a BST:", is_valid_bst(root, None, None))
print("Searching for 70 in the BST:", search(root, 70))
print("Searching for 100 in the BST:", search(root, 100))
print("Inorder traversal of the BST:")
print_in_range(root, 10, 90)
print("Creating a mirror of the BST:")
mirror_root: Optional[Node] = create_mirror_bst(root)
print("Inorder traversal of the mirror BST:")
print_in_range(mirror_root, 10, 90)
if __name__ == "__main__":
main()