forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_tree.py
164 lines (135 loc) · 3.74 KB
/
binary_search_tree.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
""" Binary Search Tree in Python """
class TreeNode:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
# Search Methods
def recursive_search(node, key):
if node is None:
print(f"{key} was not found in the tree")
return None
if node.key == key:
print(f"{key} was found in the tree")
return node
if key > node.key:
return recursive_search(node.right, key)
else:
return recursive_search(node.left, key)
def linear_search(node, key):
while node is not None:
if node.key == key:
return node
elif key > node.key:
node = node.right
else:
node = node.left
return None
# Insertion Method
def insert(node, key):
if node is None:
return TreeNode(key)
else:
if key < node.key:
node.left = insert(node.left, key)
else:
node.right = insert(node.right, key)
return node
# Printing Methods
def pre_order(node):
if node is None:
return []
result = [node.key]
result.extend(pre_order(node.left))
result.extend(pre_order(node.right))
return result
def in_order(node):
if node is None:
return []
result = []
result.extend(in_order(node.left))
result.append(node.key)
result.extend(in_order(node.right))
return result
def post_order(node):
if node is None:
return []
result = []
result.extend(post_order(node.left))
result.extend(post_order(node.right))
result.append(node.key)
return result
# Finding the Tree's Height
def tree_height(node):
if node is None:
return 0
return 1 + max(tree_height(node.left), tree_height(node.right))
# Deletion Methods
def find_parent(node, ch):
parent_node = node
while node is not None:
if node.key == ch:
return parent_node
parent_node = node
if node.key < ch:
node = node.right
else:
node = node.left
return parent_node
def largest_on_left(node):
node = node.left
while node.right is not None:
node = node.right
return node
def delete(node, ch):
current = linear_search(node, ch)
if current is None:
return False
parent = find_parent(node, ch)
if current.left is None or current.right is None:
if current.left is None:
substitute = current.right
else:
substitute = current.left
if parent is None:
node = substitute
elif ch > parent.key:
parent.right = substitute
else:
parent.left = substitute
else:
substitute = largest_on_left(current)
current.key = substitute.key
if substitute.left is not None:
current.left = substitute.left
else:
current.left = None
return True
if __name__ == "__main__":
tree = TreeNode(3) # Create a tree (root)
# Insert several values into the tree
tree = insert(tree, 2)
tree = insert(tree, 1)
tree = insert(tree, 4)
tree = insert(tree, 6)
tree = insert(tree, 8)
tree = insert(tree, 5)
tree = insert(tree, 7)
tree = insert(tree, 0)
result = recursive_search(tree, 6)
if result is not None:
print("Value found")
else:
print("Value not found")
print(f"Height: {tree_height(tree)}")
# Delete various values
delete(tree, 7)
delete(tree, 5)
delete(tree, 8)
delete(tree, 3)
# Call printing methods
print(f"PreOrder: {pre_order(tree)}")
print(f"InOrder: {in_order(tree)}")
print(f"PostOrder: {post_order(tree)}")
# Display the height of the tree after removing items
print(f"Height: {tree_height(tree)}")