SlideShare a Scribd company logo
1 of 12
Binary Search Tree in Python
Name : Keshav Bandil
Branch : AI&DS
Year : 2nd Year ( 3rd Semister)
Submitted To : Geetika Hazra
Binary Search Tree in Python
A binary search tree is a data structure where every node has at most two
children, and the left child is always smaller than the parent node, while
the right child is larger. Let's explore the key properties and code
implementation in Python.
Key Properties of a Binary Search Tree
Unique Value (NO SAME VALUE)
Each node in a binary search tree holds a
unique value, and no two nodes in the tree
can have the same value.
Efficient Insertion and Deletion
Insertions and deletions in a binary search
tree can be performed efficiently, providing a
flexible data structure.
Tree Traversal Flexibility
Tree traversal algorithms allow you to visit nodes in specific orders, such as preorder, inorder, and
postorder.
Code of Binary Search Tree in Python
1 Node Class
Create a Node class
with attributes for storing
the node value and
references to the left and
right children.
2 Insertion
Implementation
Implement the logic to
insert a new value into
the binary search tree
while maintaining its
ordered structure.
3 Deletion
Implementation
Implement the logic to
delete a node from the
binary search tree while
preserving its properties.
Code for Binary Search Tree in Python
class Node:
def init(self, val):
self.val = val
self.left = None
self.right = None
##Defining a function that will search the given value of x in the tree
def search (root, x):
##The base conditions are If the given root is a Null Node
Or if the value we are searching for is present at the root only##
if root is None or root.val == x:
return root.val
if root.val < x: ## check If the value of x is greater than the
value root.val < x:
# We will search in the right subtree
return search(root.right, x)
##If the value of x is smaller than the value of the root node
we will search in left subtree
return search(root.left, x)
##Creating a binary search tree and searching for x in the tree
root = Node(9)
root.left = Node(1)
root.right = Node(10)
root.left.left = Node(0)
root.left.right = Node(3)
root.left.right.right = Node(4)
x = 4
v = search(root, x)
print("The node we are searching for is present in the given BST: ", v)
Tree Traversal Algorithms
Inorder Traversal
Visits the left subtree, then
the current node, and finally
the right subtree.
LEFT SUBTREE-ROOT
NODE -RIGHT SUBTREE
Preorder Traversal
Visits the current node, then
the left subtree, and finally the
right subtree.
ROOT NODE-LEFT
SUBTREE-RIGHT SUBTREE
Postorder Traversal
Visits the left subtree, then
the right subtree, and finally
the current node.
LEFT SUBTREE-RIGHT
SUBTREE-ROOT NODE
Implementation of a Binary Search Tree in Python
Creating a Tree
Use the Node class to create a
binary search tree instance and
add nodes with specific values.
Inserting Values
Use the insertion
implementation to add new
values to the binary search tree
in their appropriate positions.
Deleting Values
Use the deletion
implementation to remove
nodes from the binary search
tree.
Complete Structure of Binary Search Tree
Searching for a Node in a Binary Search Tree
1 Start at the Root
Begin the search from the root node,
comparing the target value with the
current node's value.
2
Follow the Path
If the target value is smaller, move to
the left child node; otherwise, move to
the right child node. 3 Found or Not Found
Repeat the process until the target
value is found or the traversal reaches
a null (not found).
Time Complexity of Binary Search Tree
Operation Time Complexity
Search O(log n) average, O(n) worst case
Insertion O(log n) average, O(n) worst case
Deletion O(log n) average, O(n) worst case
The time complexity of binary search tree operations depends on the tree's height and whether it is
balanced or unbalanced.
Balancing a Binary Search Tree
AVL Trees
AVL trees are self-balancing binary search
trees that automatically adjust their structure
to maintain efficiency.
Red-Black Trees
Red-Black trees are another type of self-
balancing binary search tree that guarantee a
balanced structure.
Balancing Algorithms
Various algorithms exist to rebalance an
unbalanced binary search tree, ensuring
optimal time complexities.
Benefits and Trade-Offs
Balancing a binary search tree improves the
time complexity of operations but may involve
additional complexity in implementation.
THANK YOU

More Related Content

Similar to nptel 2nd presentation.pptx

Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treeszukun
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docxsowmya koneru
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptxskilljiolms
 
Binary search tree
Binary search treeBinary search tree
Binary search treeSana Yameen
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Treecinterviews
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxasimshahzad8611
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms CourseHunt
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
lecture-i-trees.ppt
lecture-i-trees.pptlecture-i-trees.ppt
lecture-i-trees.pptMouDhara1
 
binarysearchtreeindatastructures-200604055006 (1).pdf
binarysearchtreeindatastructures-200604055006 (1).pdfbinarysearchtreeindatastructures-200604055006 (1).pdf
binarysearchtreeindatastructures-200604055006 (1).pdfajajkhan16
 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overviewElifTech
 
Binary search in ds
Binary search in dsBinary search in ds
Binary search in dschauhankapil
 

Similar to nptel 2nd presentation.pptx (20)

Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure trees
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
 
Binary trees
Binary treesBinary trees
Binary trees
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
Materi Searching
Materi Searching Materi Searching
Materi Searching
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
lecture-i-trees.ppt
lecture-i-trees.pptlecture-i-trees.ppt
lecture-i-trees.ppt
 
binarysearchtreeindatastructures-200604055006 (1).pdf
binarysearchtreeindatastructures-200604055006 (1).pdfbinarysearchtreeindatastructures-200604055006 (1).pdf
binarysearchtreeindatastructures-200604055006 (1).pdf
 
Binary trees
Binary treesBinary trees
Binary trees
 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
 
Binary search in ds
Binary search in dsBinary search in ds
Binary search in ds
 
L 19 ct1120
L 19 ct1120L 19 ct1120
L 19 ct1120
 

More from KeshavBandil2

Introduction-to-Iteration (2).pptx
Introduction-to-Iteration (2).pptxIntroduction-to-Iteration (2).pptx
Introduction-to-Iteration (2).pptxKeshavBandil2
 
keshavnptel_ppt[1].docx
keshavnptel_ppt[1].docxkeshavnptel_ppt[1].docx
keshavnptel_ppt[1].docxKeshavBandil2
 
Introduction-to-Iteration.pdf
Introduction-to-Iteration.pdfIntroduction-to-Iteration.pdf
Introduction-to-Iteration.pdfKeshavBandil2
 

More from KeshavBandil2 (6)

25541100.pptx
25541100.pptx25541100.pptx
25541100.pptx
 
sensor2.pdf
sensor2.pdfsensor2.pdf
sensor2.pdf
 
sensor1.pdf
sensor1.pdfsensor1.pdf
sensor1.pdf
 
Introduction-to-Iteration (2).pptx
Introduction-to-Iteration (2).pptxIntroduction-to-Iteration (2).pptx
Introduction-to-Iteration (2).pptx
 
keshavnptel_ppt[1].docx
keshavnptel_ppt[1].docxkeshavnptel_ppt[1].docx
keshavnptel_ppt[1].docx
 
Introduction-to-Iteration.pdf
Introduction-to-Iteration.pdfIntroduction-to-Iteration.pdf
Introduction-to-Iteration.pdf
 

Recently uploaded

power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 

Recently uploaded (20)

power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 

nptel 2nd presentation.pptx

  • 1. Binary Search Tree in Python Name : Keshav Bandil Branch : AI&DS Year : 2nd Year ( 3rd Semister) Submitted To : Geetika Hazra
  • 2. Binary Search Tree in Python A binary search tree is a data structure where every node has at most two children, and the left child is always smaller than the parent node, while the right child is larger. Let's explore the key properties and code implementation in Python.
  • 3. Key Properties of a Binary Search Tree Unique Value (NO SAME VALUE) Each node in a binary search tree holds a unique value, and no two nodes in the tree can have the same value. Efficient Insertion and Deletion Insertions and deletions in a binary search tree can be performed efficiently, providing a flexible data structure. Tree Traversal Flexibility Tree traversal algorithms allow you to visit nodes in specific orders, such as preorder, inorder, and postorder.
  • 4. Code of Binary Search Tree in Python 1 Node Class Create a Node class with attributes for storing the node value and references to the left and right children. 2 Insertion Implementation Implement the logic to insert a new value into the binary search tree while maintaining its ordered structure. 3 Deletion Implementation Implement the logic to delete a node from the binary search tree while preserving its properties.
  • 5. Code for Binary Search Tree in Python class Node: def init(self, val): self.val = val self.left = None self.right = None ##Defining a function that will search the given value of x in the tree def search (root, x): ##The base conditions are If the given root is a Null Node Or if the value we are searching for is present at the root only## if root is None or root.val == x: return root.val if root.val < x: ## check If the value of x is greater than the value root.val < x: # We will search in the right subtree return search(root.right, x) ##If the value of x is smaller than the value of the root node we will search in left subtree return search(root.left, x) ##Creating a binary search tree and searching for x in the tree root = Node(9) root.left = Node(1) root.right = Node(10) root.left.left = Node(0) root.left.right = Node(3) root.left.right.right = Node(4) x = 4 v = search(root, x) print("The node we are searching for is present in the given BST: ", v)
  • 6. Tree Traversal Algorithms Inorder Traversal Visits the left subtree, then the current node, and finally the right subtree. LEFT SUBTREE-ROOT NODE -RIGHT SUBTREE Preorder Traversal Visits the current node, then the left subtree, and finally the right subtree. ROOT NODE-LEFT SUBTREE-RIGHT SUBTREE Postorder Traversal Visits the left subtree, then the right subtree, and finally the current node. LEFT SUBTREE-RIGHT SUBTREE-ROOT NODE
  • 7. Implementation of a Binary Search Tree in Python Creating a Tree Use the Node class to create a binary search tree instance and add nodes with specific values. Inserting Values Use the insertion implementation to add new values to the binary search tree in their appropriate positions. Deleting Values Use the deletion implementation to remove nodes from the binary search tree.
  • 8. Complete Structure of Binary Search Tree
  • 9. Searching for a Node in a Binary Search Tree 1 Start at the Root Begin the search from the root node, comparing the target value with the current node's value. 2 Follow the Path If the target value is smaller, move to the left child node; otherwise, move to the right child node. 3 Found or Not Found Repeat the process until the target value is found or the traversal reaches a null (not found).
  • 10. Time Complexity of Binary Search Tree Operation Time Complexity Search O(log n) average, O(n) worst case Insertion O(log n) average, O(n) worst case Deletion O(log n) average, O(n) worst case The time complexity of binary search tree operations depends on the tree's height and whether it is balanced or unbalanced.
  • 11. Balancing a Binary Search Tree AVL Trees AVL trees are self-balancing binary search trees that automatically adjust their structure to maintain efficiency. Red-Black Trees Red-Black trees are another type of self- balancing binary search tree that guarantee a balanced structure. Balancing Algorithms Various algorithms exist to rebalance an unbalanced binary search tree, ensuring optimal time complexities. Benefits and Trade-Offs Balancing a binary search tree improves the time complexity of operations but may involve additional complexity in implementation.