-
-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathInsert_elements_in_BST.py
More file actions
34 lines (33 loc) · 890 Bytes
/
Insert_elements_in_BST.py
File metadata and controls
34 lines (33 loc) · 890 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
class Node:
def __init__(self,data):
self.left=None
self.right=None
self.data=data
def PrintTree(self):
if self.left:
self.left.PrintTree()
print(self.data)
if self.right:
self.right.PrintTree()
def insert(self,data):
if self.data:
if data<self.data:
if self.left is None:
self.left=Node(data)
else:
self.left.insert(data)
elif data>self.data:
if self.right is None:
self.right=Node(data)
else:
self.right.insert(data)
else:
self.data=data
if __name__=='__main__':
root=Node(12)
root.insert(10)
root.insert(6)
root.insert(14)
root.insert(2)
root.insert(1)
root.PrintTree()