Skip to content

Commit af8e781

Browse files
committed
fix code
1 parent 99c3bda commit af8e781

12 files changed

+126
-108
lines changed
Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
1+
from typing import Optional
12
from inorder_successor import inorder_successor
3+
from tree_node import Node
4+
25
# The above line imports the inorder_successor function from the inorder_successor.py file
3-
def delete_node(root,val):
4-
""" This function deletes a node with value val from the BST"""
5-
6-
# search in the left subtree
7-
if root.data < val:
8-
root.right = delete_node(root.right,val)
6+
def delete_node(root: Node, val: int) -> Optional[Node]:
7+
"""This function deletes a node with value val from the BST"""
98

10-
# search in the right subtree
11-
elif root.data>val:
12-
root.left=delete_node(root.left,val)
9+
# Search in the right subtree
10+
if root.data < val:
11+
root.right = delete_node(root.right, val)
1312

14-
# node to be deleted is found
15-
else:
16-
# case 1: no child leaf node
17-
if root.left is None and root.right is None:
18-
return None
13+
# Search in the left subtree
14+
elif root.data > val:
15+
root.left = delete_node(root.left, val)
1916

20-
# case 2: one child
21-
if root.left is None:
22-
return root.right
23-
24-
# case 2: one child
25-
elif root.right is None:
26-
return root.left
27-
28-
# case 3: two children
29-
30-
# find the inorder successor
31-
IS=inorder_successor(root.right)
32-
root.data=IS.data
33-
root.right=delete_node(root.right,IS.data)
34-
return root
35-
17+
# Node to be deleted is found
18+
else:
19+
# Case 1: No child (leaf node)
20+
if root.left is None and root.right is None:
21+
return None
22+
23+
# Case 2: One child
24+
if root.left is None:
25+
return root.right
26+
27+
# Case 2: One child
28+
elif root.right is None:
29+
return root.left
30+
31+
# Case 3: Two children
32+
# Find the inorder successor
33+
is_node: Node = inorder_successor(root.right)
34+
root.data = is_node.data
35+
root.right = delete_node(root.right, is_node.data)
36+
return root
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
def inorder_successor(root):
2-
# This function returns the inorder successor of a node in a BST
3-
4-
# The inorder successor of a node is the node with the smallest value greater than the value of the node
5-
current=root
6-
7-
# The inorder successor is the leftmost node in the right subtree
8-
while current.left is not None:
9-
current=current.left
10-
return current
1+
from tree_node import Node
2+
3+
def inorder_successor(root: Node) -> Node:
4+
"""This function returns the inorder successor of a node in a BST"""
5+
6+
# The inorder successor of a node is the node with the smallest value greater than the value of the node
7+
current: Node = root
8+
9+
# The inorder successor is the leftmost node in the right subtree
10+
while current.left is not None:
11+
current = current.left
12+
return current

binary_search_trees/inorder_traversal.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
def inorder(root):
2-
""" This function performs an inorder traversal of a BST"""
1+
from typing import Optional
2+
from tree_node import Node
3+
4+
def inorder(root: Optional[Node]) -> None:
5+
"""This function performs an inorder traversal of a BST"""
36

47
# The inorder traversal of a BST is the nodes in increasing order
58
if root is None:

binary_search_trees/insert_in_bst.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
from typing import Optional
12
from tree_node import Node
2-
def insert(root,val):
3-
4-
""" This function inserts a node with value val into the BST"""
3+
4+
def insert(root: Optional[Node], val: int) -> Node:
5+
"""This function inserts a node with value val into the BST"""
56

67
# If the tree is empty, create a new node
78
if root is None:
89
return Node(val)
910

1011
# If the value to be inserted is less than the root value, insert in the left subtree
1112
if val < root.data:
12-
root.left = insert(root.left,val)
13+
root.left = insert(root.left, val)
1314

1415
# If the value to be inserted is greater than the root value, insert in the right subtree
1516
else:
16-
root.right = insert(root.right,val)
17+
root.right = insert(root.right, val)
1718
return root

binary_search_trees/main.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
from typing import Optional
12
from insert_in_bst import insert
23
from delete_a_node_in_bst import delete_node
34
from search_in_bst import search
45
from mirror_a_bst import create_mirror_bst
56
from print_in_range import print_in_range
67
from root_to_leaf_paths import print_root_to_leaf_paths
78
from 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

7881
if __name__ == "__main__":
79-
main()
80-
81-
82-
83-
82+
main()

binary_search_trees/mirror_a_bst.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
def create_mirror_bst(root):
2-
""" Function to create a mirror of a binary search tree"""
1+
from typing import Optional
2+
from tree_node import Node
3+
4+
def create_mirror_bst(root: Optional[Node]) -> Optional[Node]:
5+
"""Function to create a mirror of a binary search tree"""
36

47
# If the tree is empty, return None
58
if root is None:
69
return None
710

8-
# Create a new node with the root value
9-
1011
# Recursively create the mirror of the left and right subtrees
11-
left_mirror = create_mirror_bst(root.left)
12-
right_mirror = create_mirror_bst(root.right)
12+
left_mirror: Optional[Node] = create_mirror_bst(root.left)
13+
right_mirror: Optional[Node] = create_mirror_bst(root.right)
14+
15+
# Swap left and right subtrees
1316
root.left = right_mirror
1417
root.right = left_mirror
1518
return root

binary_search_trees/print_in_range.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
def print_in_range(root,k1,k2):
2-
3-
""" This function prints the nodes in a BST that are in the range k1 to k2 inclusive"""
4-
5-
# If the tree is empty, return
6-
if root is None:
7-
return
8-
9-
# If the root value is in the range, print the root value
10-
if root.data >= k1 and root.data <= k2:
11-
print_in_range(root.left,k1,k2)
12-
print(root.data)
13-
print_in_range(root.right,k1,k2)
1+
from typing import Optional
2+
from tree_node import Node
3+
4+
def print_in_range(root: Optional[Node], k1: int, k2: int) -> None:
5+
"""This function prints the nodes in a BST that are in the range k1 to k2 inclusive"""
146

15-
# If the root value is less than k1, the nodes in the range will be in the right subtree
16-
elif root.data < k1:
17-
print_in_range(root.left,k1,k2)
7+
# If the tree is empty, return
8+
if root is None:
9+
return
1810

19-
# If the root value is greater than k2, the nodes in the range will be in the left subtree
20-
else:
21-
print_in_range(root.right,k1,k2)
11+
# If the root value is in the range, print the root value
12+
if k1 <= root.data <= k2:
13+
print_in_range(root.left, k1, k2)
14+
print(root.data)
15+
print_in_range(root.right, k1, k2)
16+
17+
# If the root value is less than k1, the nodes in the range will be in the right subtree
18+
elif root.data < k1:
19+
print_in_range(root.right, k1, k2) # Fixed: original had left, which is incorrect
20+
21+
# If the root value is greater than k2, the nodes in the range will be in the left subtree
22+
else:
23+
print_in_range(root.left, k1, k2) # Fixed: original had right, which is incorrect

binary_search_trees/root_to_leaf_paths.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
def print_root_to_leaf_paths(root, path):
2-
""" This function prints all the root to leaf paths in a BST"""
1+
from typing import Optional, List
2+
from tree_node import Node
3+
4+
def print_root_to_leaf_paths(root: Optional[Node], path: List[int]) -> None:
5+
"""This function prints all the root to leaf paths in a BST"""
36

47
# If the tree is empty, return
58
if root is None:

binary_search_trees/search_in_bst.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
def search(root, val):
2-
""" This function searches for a node with value val in the BST and returns True if found, False otherwise"""
1+
from typing import Optional
2+
from tree_node import Node
3+
4+
def search(root: Optional[Node], val: int) -> bool:
5+
"""This function searches for a node with value val in the BST and returns True if found, False otherwise"""
36

47
# If the tree is empty, return False
5-
if root == None:
8+
if root is None:
69
return False
710

811
# If the root value is equal to the value to be searched, return True

binary_search_trees/tree_node.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
from typing import Optional
12

23
# Node class for binary tree
3-
44
class Node:
5-
def __init__(self, data):
6-
self.data = data
7-
self.left = None
8-
self.right = None
5+
def __init__(self, data: int) -> None:
6+
self.data: int = data
7+
self.left: Optional[Node] = None
8+
self.right: Optional[Node] = None

0 commit comments

Comments
 (0)