-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBInarySearchTree_Practise_v4.py
More file actions
75 lines (62 loc) · 1.55 KB
/
Copy pathBInarySearchTree_Practise_v4.py
File metadata and controls
75 lines (62 loc) · 1.55 KB
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
class Node:
def __init__(self,data):
self.val = data
self.leftChild = None
self.rightChild = None
def bst_insert(root,data):
if root is None:
root = Node(data)
else:
if root.val > data:
if root.leftChild:
bst_insert(root.leftChild,data)
else:
root.leftChild = Node(data)
else:
if root.rightChild:
bst_insert(root.rightChild,data)
else:
root.rightChild = Node(data)
def print_inorder(root):
if root.leftChild:
print_inorder(root.leftChild)
print(root.val)
if root.rightChild:
print_inorder(root.rightChild)
#Queue based approach
def printLevelOrder_using_queue(root):
print("inside quque")
# Base Case
if root is None:
return
# Create an empty queue for level order traversal
queue = []
# Enqueue Root and initialize height
queue.append(root)
while (len(queue) > 0):
# Print front of queue and remove it from queue
print(queue[0].val)
node = queue.pop(0)
# Enqueue left child
if node.leftChild is not None:
queue.append(node.leftChild)
# Enqueue right child
if node.rightChild is not None:
queue.append(node.rightChild)
node = Node(5)
bst_insert(node,4)
bst_insert(node,6)
bst_insert(node,7)
bst_insert(node,8)
bst_insert(node,3)
print_inorder(node)
printLevelOrder_using_queue(node)
'''
5
/ \
4 6
/ \
3 7
\
8
'''