1+ from typing import Optional
12from insert_in_bst import insert
23from delete_a_node_in_bst import delete_node
34from search_in_bst import search
45from mirror_a_bst import create_mirror_bst
56from print_in_range import print_in_range
67from root_to_leaf_paths import print_root_to_leaf_paths
78from validate_bst import is_valid_bst
9+ from tree_node import Node
810
9-
10- def main ():
11-
11+ def main () -> None :
1212 # Create a BST
13- root = None
13+ root : Optional [ Node ] = None
1414 root = insert (root , 50 )
1515 root = insert (root , 30 )
1616 root = insert (root , 20 )
@@ -28,56 +28,55 @@ def main():
2828 print_root_to_leaf_paths (root , [])
2929
3030 # Check if the tree is a BST
31- print ("Is the tree a BST:" , is_valid_bst (root ,None ,None ))
31+ print ("Is the tree a BST:" , is_valid_bst (root , None , None ))
3232
3333
3434 # Delete nodes from the BST
3535 print ("Deleting 20 from the BST:" )
36- root = delete_node (root , 20 )
36+ if root is not None :
37+ root = delete_node (root , 20 )
3738
3839 # Print the inorder traversal of the BST
3940 print ("Inorder traversal of the BST after deleting 20:" )
4041 print_in_range (root , 10 , 90 )
4142
4243 # Check if the tree is a BST
43- print ("Is the tree a BST:" , is_valid_bst (root ,None ,None ))
44+ print ("Is the tree a BST:" , is_valid_bst (root , None , None ))
4445
4546
4647 # Delete nodes from the BST
4748 print ("Deleting 30 from the BST:" )
48- root = delete_node (root , 30 )
49+ if root is not None :
50+ root = delete_node (root , 30 )
4951
5052 # Print the inorder traversal of the BST after deleting 30
5153 print ("Inorder traversal of the BST after deleting 30:" )
5254 print_in_range (root , 10 , 90 )
5355
5456 # Check if the tree is a BST
55- print ("Is the tree a BST:" , is_valid_bst (root ,None ,None ))
57+ print ("Is the tree a BST:" , is_valid_bst (root , None , None ))
5658
5759 # Delete nodes from the BST
5860 print ("Deleting 50 from the BST:" )
59- root = delete_node (root , 50 )
61+ if root is not None :
62+ root = delete_node (root , 50 )
6063
6164 # Print the inorder traversal of the BST after deleting 50
6265 print ("Inorder traversal of the BST after deleting 50:" )
6366 print_in_range (root , 10 , 90 )
6467
6568 # Check if the tree is a BST
66- print ("Is the tree a BST:" , is_valid_bst (root ,None ,None ))
69+ print ("Is the tree a BST:" , is_valid_bst (root , None , None ))
6770
6871
6972 print ("Searching for 70 in the BST:" , search (root , 70 ))
7073 print ("Searching for 100 in the BST:" , search (root , 100 ))
7174 print ("Inorder traversal of the BST:" )
7275 print_in_range (root , 10 , 90 )
7376 print ("Creating a mirror of the BST:" )
74- mirror_root = create_mirror_bst (root )
77+ mirror_root : Optional [ Node ] = create_mirror_bst (root )
7578 print ("Inorder traversal of the mirror BST:" )
7679 print_in_range (mirror_root , 10 , 90 )
7780
7881if __name__ == "__main__" :
79- main ()
80-
81-
82-
83-
82+ main ()
0 commit comments