-
Notifications
You must be signed in to change notification settings - Fork 624
Expand file tree
/
Copy pathinsertion_recursive.py
More file actions
43 lines (33 loc) · 846 Bytes
/
insertion_recursive.py
File metadata and controls
43 lines (33 loc) · 846 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
39
40
41
42
43
class Node():
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def insertion_recursive(root, val):
if not root:
return Node(val)
else:
if root.val < val:
if root.right is None:
root.right = Node(val)
else:
insertion_recursive(root.right, val)
else:
if root.left is None:
root.left = Node(val)
else:
insertion_recursive(root.left, val)
def inorder(root):
if root:
inorder(root.left)
print(root.val)
inorder(root.right)
root = Node(5)
root.left = Node(3)
root.right = Node(7)
root.left.left = Node(1)
root.left. right = Node(4)
root.right.right = Node(8)
inorder(root)
insertion_recursive(root, 6)
inorder(root)