Data structures in Python Series 6.3: Minimum Element in Binary Tree
Python hot shot trick finding minimum in a binary tree 😏 :
Now to find a minimum what we are gonna do is just make a list, append all the elements of tree to the list and then use the min method to find the minimum element in the binary tree.
Just add the append in the traversal itself.
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 18 00:39:52 2018
@author: zeus
"""
'''
First we define a structure for the nodes to be
________________________________
NODE = |left-pointer|value|right-pointer|
````````````````````````````````
'''
class Node:
def __init__(self,key):
self.left = None
self.right = None
self.val = key
'''
Now we are going to define a class Binary tree ,
which will have
1.a default initialisation method,
2.a method to add nodes which would take the head pointer
as input and return the resultant head pointer
3.a method to print the binary tree out in inorder :
Left value | middle value | right value|
'''
class Btree:
def __init__(self,data=None):
self.head = None
def add(self,root,data):
'''
The root == None will end the recursive implementation
'''
if root == None:
root = Node(data)
return root
else:
if root.val < data:
root.right = self.add(root.right,data)
'''
If value of the current node is less than data then pass the
data to the right side of the current node as per BST property
less values on left and greater one on right
'''
else:
'''
If value of the current node is greater than data then pass the
data to the left side of the current node as per BST property
less values on left and greater one on right
'''
root.left = self.add(root.left,data)
return root
def inorder(self,root):
global a
if root:
#will run until root == None i.e. end of the particular branch
self.inorder(root.left)
'''
traverse to the extreme left, now when extreme left == None print its value
then print its parents value
then print its right siblings value
'''
a.append(root.val)
print(root.val)
self.inorder(root.right)
def search(self,root,val):
if root :
if root.val > val :
self.search(root.left,val)
elif root.val < val:
self.search(root.right,val)
elif root.val == val:
print(' found ')
return;
if __name__ == "__main__":
#min = 0
a = []
lis = Btree()
lis.head=lis.add(lis.head,50)
lis.head=lis.add(lis.head,30)
lis.head=lis.add(lis.head,20)
lis.head=lis.add(lis.head,40)
lis.head=lis.add(lis.head,70)
lis.head=lis.add(lis.head,60)
lis.head=lis.add(lis.head,80)
lis.inorder(lis.head)
lis.search(lis.head,80)
print(min(a))
Now to find a minimum what we are gonna do is just make a list, append all the elements of tree to the list and then use the min method to find the minimum element in the binary tree.
Just add the append in the traversal itself.
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 18 00:39:52 2018
@author: zeus
"""
'''
First we define a structure for the nodes to be
________________________________
NODE = |left-pointer|value|right-pointer|
````````````````````````````````
'''
class Node:
def __init__(self,key):
self.left = None
self.right = None
self.val = key
'''
Now we are going to define a class Binary tree ,
which will have
1.a default initialisation method,
2.a method to add nodes which would take the head pointer
as input and return the resultant head pointer
3.a method to print the binary tree out in inorder :
Left value | middle value | right value|
'''
class Btree:
def __init__(self,data=None):
self.head = None
def add(self,root,data):
'''
The root == None will end the recursive implementation
'''
if root == None:
root = Node(data)
return root
else:
if root.val < data:
root.right = self.add(root.right,data)
'''
If value of the current node is less than data then pass the
data to the right side of the current node as per BST property
less values on left and greater one on right
'''
else:
'''
If value of the current node is greater than data then pass the
data to the left side of the current node as per BST property
less values on left and greater one on right
'''
root.left = self.add(root.left,data)
return root
def inorder(self,root):
global a
if root:
#will run until root == None i.e. end of the particular branch
self.inorder(root.left)
'''
traverse to the extreme left, now when extreme left == None print its value
then print its parents value
then print its right siblings value
'''
a.append(root.val)
print(root.val)
self.inorder(root.right)
def search(self,root,val):
if root :
if root.val > val :
self.search(root.left,val)
elif root.val < val:
self.search(root.right,val)
elif root.val == val:
print(' found ')
return;
if __name__ == "__main__":
#min = 0
a = []
lis = Btree()
lis.head=lis.add(lis.head,50)
lis.head=lis.add(lis.head,30)
lis.head=lis.add(lis.head,20)
lis.head=lis.add(lis.head,40)
lis.head=lis.add(lis.head,70)
lis.head=lis.add(lis.head,60)
lis.head=lis.add(lis.head,80)
lis.inorder(lis.head)
lis.search(lis.head,80)
print(min(a))
Comments
Post a Comment