Here is how to carry out Breadth First Search on a Binary Search Tree in Python. It is performed in two ways - using loop and using recursive function. Both ways are shown below.
Run the code here: https://repl.it/@VinitKhandelwal/breadth-first-search
3 left child of 5
7 right child of 5
4 right child of 3
6 left child of 7
2 left child of 3
8 right child of 7
10 right child of 8
1 left child of 2
9 left child of 10
5 already in Binary Tree
9 already in Binary Tree
15 right child of 10
19 right child of 15
13 left child of 15
11 left child of 13
14 right child of 13
17 left child of 19
18 right child of 17
20 right child of 19
16 left child of 17
12 right child of 11
[5, 3, 7, 2, 4, 6, 8, 1, 10, 9, 15, 13, 19, 11, 14, 17, 20, 12, 16, 18]
[5, 3, 7, 2, 4, 6, 8, 1, 10, 9, 15, 13, 19, 11, 14, 17, 20, 12, 16, 18]
Run the code here: https://repl.it/@VinitKhandelwal/breadth-first-search
class BinaryTree:
def __init__(self):
self.reset()
def reset(self):
self.head = None
self.crawler = self.head
def push(self, value):
new_node = Node(value)
# if no head
if self.head == None:
self.head = new_node
print(f"{value} head")
# if head
else:
crawler = self.head
while True:
if value > crawler.value:
if crawler.child_right is None:
print(f"{value} right child of {crawler.value}")
crawler.child_right = new_node
break
else:
crawler = crawler.child_right
elif value < crawler.value:
if crawler.child_left is None:
print(f"{value} left child of {crawler.value}")
crawler.child_left = new_node
break
else:
crawler = crawler.child_left
else:
print(f"{value} already in Binary Tree")
break
def find(self, value):
if self.head is not None:
crawler = self.head
text = ""
while crawler:
if crawler.value > value:
text += str(crawler.value) + " then left "
crawler = crawler.child_left
elif crawler.value < value:
text += str(crawler.value) + " then right "
crawler = crawler.child_right
else:
text += "found " + str(crawler.value)
return text
return f"{value} not found"
return "No tree"
def remove(self, value):
if self.head is None:
return "Empty Tree"
else:
current_node = self.head
parent_node = None
while True:
if value < current_node.value:
parent_node = current_node
current_node = parent_node.child_left
elif value > current_node.value:
parent_node = current_node
current_node = parent_node.child_right
elif value == current_node.value:
if current_node.child_right == None:
if parent_node == None:
self.head = current_node.child_left
elif current_node.value < parent_node.value:
parent_node.child_left = current_node.child_left
elif current_node.value > parent_node.value:
parent_node.child_right = current_node.child_left
elif current_node.child_right.child_left == None:
if parent_node == None:
self.head = current_node.child_left
elif current_node.value < parent_node.value:
parent_node.child_right.child_left = current_node.child_left
elif current_node.value > parent_node.value:
parent_node.child_right = current_node.child_right
else:
leftmost = current_node.child_right.child_left
leftmostParent = current_node.child_right
while leftmost.child_left is not None:
leftmostParent = leftmost
leftmost = leftmost.child_left
leftmostParent.child_left = leftmost.child_right
leftmost.child_left = current_node.child_left
leftmost.child_right = current_node.child_right
if parent_node is None:
self.head = leftmost
else:
if current_node.value < parent_node.value:
parent_node.child_left = leftmost
elif current_node.value > parent_node.value:
parent_node.child_right = leftmost
return f"deleted {value}"
def bfs(self):
current_node = self.head
list1 = []
queue = []
queue.append(current_node)
while len(queue) > 0:
current_node = queue[0]
queue.pop(0)
list1.append(current_node.value)
if current_node.child_left != None:
queue.append(current_node.child_left)
if current_node.child_right != None:
queue.append(current_node.child_right)
return list1
def bfsRecursive(self, queue=[], list1=[]):
if len(queue) == 0:
return list1
current_node = queue[0]
queue.pop(0)
list1.append(current_node.value)
if current_node.child_left != None:
queue.append(current_node.child_left)
if current_node.child_right != None:
queue.append(current_node.child_right)
return self.bfsRecursive(queue, list1)
class Node:
def __init__(self, value):
self.value = value
self.parent = None
self.child_left = None
self.child_right = None
obj = BinaryTree()
obj.push(5)
obj.push(3)
obj.push(7)
obj.push(4)
obj.push(6)
obj.push(2)
obj.push(8)
obj.push(10)
obj.push(1)
obj.push(9)
obj.push(5)
obj.push(9)
obj.push(15)
obj.push(19)
obj.push(13)
obj.push(11)
obj.push(14)
obj.push(17)
obj.push(18)
obj.push(20)
obj.push(16)
obj.push(12)
print(obj.bfs())
print(obj.bfsRecursive([obj.head]))
OUTPUT
5 head3 left child of 5
7 right child of 5
4 right child of 3
6 left child of 7
2 left child of 3
8 right child of 7
10 right child of 8
1 left child of 2
9 left child of 10
5 already in Binary Tree
9 already in Binary Tree
15 right child of 10
19 right child of 15
13 left child of 15
11 left child of 13
14 right child of 13
17 left child of 19
18 right child of 17
20 right child of 19
16 left child of 17
12 right child of 11
[5, 3, 7, 2, 4, 6, 8, 1, 10, 9, 15, 13, 19, 11, 14, 17, 20, 12, 16, 18]
[5, 3, 7, 2, 4, 6, 8, 1, 10, 9, 15, 13, 19, 11, 14, 17, 20, 12, 16, 18]
Comments
Post a Comment