Python program to find an element in binary search tree.

Description:
This program is to find an element in binary search tree..
Algo: -
1. Make a class for node in a tree.
2. Form the tree by creating nodes.
3. Traverse the tree.
4. During traversal check the node one by one.
5. If the node matches the searched value then return true.
Program:
#A class that represents the individual node of the Binary tree
class node:
def __init__(self, key):
self.left = None
self.right = None
self.data = key

#A function to count no. of nodes.
def finde(root,e,res):
if root:
if res==True:
return True
#First recur the left child
if root.left:
res=finde(root.left,e,res)
if root.data==e:
return True
#Recur the right child at last
if root.right:
res=finde(root.right,e,res)
return res

def main():

root = node(5)  #root node of the tree
root.left = node(4) #node at left of root
root.right = node(6) #node at right of root
root.left.left = node(10)
root.right.left = node(12)
root.right.right =node(15)
e=int(input('Enter the element to be searched = '))
print('      ',root.data)
print('      / \\')
print('   ',root.left.data,'  ',root.right.data)
print('    /   / \\')
print(' ',root.left.left.data,'  ',root.right.left.data,root.right.right.data)
var=finde(root,e,False)
if var==True:
print('Found')
else:

#Driver code
if __name__ == "__main__":
main()
Output:
Enter the element to be searched = 6 5 / \ 4 6 / / \ 10 12 15 Found

More Articles of Shivam Kalra:

Name Views Likes
Python program to print all the elements of binary search tree 549 17
Python program to calculate sum of k smallest elements in binary search tree. 1288 15
Python program to count binary search tree nodes that lie in a given range. 577 19
Python program to add two Matrices 484 16
Python program to check that an year is Leap Year or Not. 479 14
Python program to Convert a list of characters into a string 512 19
Python program to Transpose a Matrix 552 17
Python program to convert a List into a Tuple 528 24
Python program to find an element in binary tree. 521 17
Python program to Multiply two matrices. 628 19
Python program to covert a temperature from Celsius to Fahrenheit 484 15
Python program to print the first non-repeated character from a string 564 16
Python program to Convert a list of Tuples into Dictionary 716 26
Python program to convert a temperature from Fahrenheit to Celsius 550 12
Python program to gain understanding of Matplotlib python library 1019 18
Python program to convert Decimal to Binary, Octal and Hexadecimal. 670 23
Python program to convert set into a list 466 18
Python program to convert distance from Kilometers to Miles 518 21
Python program to count the number of nodes in Binary Search Tree. 867 23
Python program to remove duplicates from an array without using any library 958 28
Python program to Decimal to Binary using Recursion 579 24
This program is to gain understanding of Numpy python module. 664 11
Python program to show implementation of Binary Search Tree 546 22
Python program to print all the elements of binary tree 482 18
Python program to find an element in binary search tree. 1327 20
Python program to find largest number in binary search tree which is less than or equal to N 855 21
Python program to count the number of nodes in Binary Search Tree. 1329 20
Python program to print leaf nodes of the binary search tree 1649 13