forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_tree.py
More file actions
38 lines (27 loc) · 760 Bytes
/
Copy pathbinary_search_tree.py
File metadata and controls
38 lines (27 loc) · 760 Bytes
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
class Node():
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class BST():
def __init__(self):
self.root = Node(None)
def insert(self, new_data):
if self.root is None:
self.root = Node(new_data)
else:
if self.root.val < new_data:
self.insert(self.root.right, new_data)
else:
self.insert(self.root.left, new_data)
def inorder(self):
if self.root:
self.inorder(self.root.left)
print(self.root.val)
self.inorder(self.root.right)
if __name__ == '__main__':
tree = BST()
tree.insert(5)
tree.insert(4)
tree.insert(7)
tree.inorder()