Binary search tree
Binary search tree
Sana Yameen
Presented By:
Definition
• A binary search tree is a rooted binary tree, whose internal nodes
each store a key (and optionally, an associated value) and each
have two distinguished sub-trees, commonly denoted left and
right.
• The left sub-tree of a node has a key less than or equal to its
parent node's key.
• The right sub-tree of a node has a key greater than to its parent
node's key.
Representation
• BST is a collection of nodes arranged in a way where they
maintain BST properties. Each node has a key and an associated
value. While searching, the desired key is compared to the keys in
BST and if found, the associated value is retrieved.
• left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
Some BST terminologies
• The root node is the top node of tree.
• A child node has exactly one parent node and a parent node has at
most two child nodes, sibling nodes shares the same parent node.
• A leaf node has no child nodes, an interior node has at least one
child node.
• Every node in BST is a sub tree of the BST rooted at the node.
Basic Operations
• Search
• Inserting
• Deleting
• Traversing
Searching in BST
• Searching a binary search tree for a specific key can be
programmed recursively or iteratively.
• To search a given key in Binary Search Tree, we first compare it
with root, if the key is present at root, we return root. If key is
greater than root’s key, we recur for right sub tree of root node.
Otherwise we recur for left sub tree.
Algorithm of searching
If root==search-data
Return root
Else
While data not found
If search-data>node-data
Go to right sub tree
Else
Go to left sub tree
If data found
Return node
End while
Return data not found
End if
Binary search tree
Inserting
Whenever an element is to be inserted, first locate its
proper location. Start searching from the root node, then
if the data is less than the key value, search for the empty
location in the left subtree and insert the data.
Otherwise, search for the empty location in the right sub
tree and insert the data.
Binary search tree. Adding a value
Adding a value to BST can be divided into two stages:
• Search for a place to put a new element
• Insert the new element to this place.
• A new key is always inserted at leaf. We start searching a key from
root till we hit a leaf node. Once a leaf node is found, the new
node is added as a child of the leaf node.
Algorithm of inserting
If root==null
Create root node
Return
If root exist then
If data> node
Go to right sub tree
Else
Go to left sub tree
End if
Insert data
End if
Binary search tree
Deleting
There are three possible cases to consider:
• Deleting a node with no children: simply remove the node from
the tree.
• Deleting a node with one child: remove the node and replace it
with its child.
• Deleting a node with two children: call the node to be deleted N.
Do not delete N. Instead, choose either its in-order successor node
or its in-order predecessor node, R. Copy the value of R to N, then
recursively call delete on the original R until reaching one of the
first two cases.
Deleting a node with no children: simply remove
the node from the tree.
Deleting a node with one child: remove the
node and replace it with its child.
Deleting a node with two children:
Traversing
Traversal of a binary tree is to access every node of binary tree at
most.
Such traversals are classified by the order in which the nodes are
visited.
There are three ways of traversing:
Pre order (Root, Left, Right)
In order (Left, Root, Right)
Post order (Left, Right, Root)
Pre order traversing
In this traversal method, the root node is visited first, then the left
subtree and finally the right subtree.
Pre-order (Root, Left, Right) : F, B, A, D, C, E, G, I, H.
Algorithm of pre order
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
In order traversing
• In this traversal method, the left subtree is visited first, then the
root and later the right sub-tree. We should always remember that
every node may represent a subtree itself.
• If a binary tree is traversed in-order, the output will produce
sorted key values in an ascending order.
In-order (Left, Root, Right) : A, B, C, D, E, F, G, H, I.
Algorithm of in order
• Until all nodes are traversed −
• Step 1 − Recursively traverse left subtree.
• Step 2 − Visit root node.
• Step 3 − Recursively traverse right subtree.
Post order traversing
In this traversal method, the root node is visited last, hence the
name. First we traverse the left subtree, then the right subtree and
finally the root node.
Post-order(Left, Right, Root): A, C, E, D, B, H, I, G, F.
Algorithm of post order traversing
• Until all nodes are traversed −
• Step 1 − Recursively traverse left subtree.
• Step 2 − Recursively traverse right subtree.
• Step 3 − Visit root node.
Any Question
Binary search tree

More Related Content

PPTX
Binary tree
PPTX
Trees data structure
PPT
358 33 powerpoint-slides_10-trees_chapter-10
PPTX
Tree data structure
PPT
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
PPTX
Binary Search Tree (BST)
PPT
Binary tree
PDF
Difference between complete,ordered,full,strict,perfect and balanced binary tree
Binary tree
Trees data structure
358 33 powerpoint-slides_10-trees_chapter-10
Tree data structure
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
Binary Search Tree (BST)
Binary tree
Difference between complete,ordered,full,strict,perfect and balanced binary tree

What's hot (20)

PPT
Lecture 5 trees
PDF
Lecture notes data structures tree
PPT
Ch13 Binary Search Tree
PDF
Binary Search Tree
PPTX
Tree
PPTX
Treesandgraphs
PPTX
Mca iii dfs u-4 tree and graph
PPT
introduction to_trees
PPTX
PPTX
Tree - Data Structure
PPT
Tree
PPTX
Unit – vi tree
PPTX
PPTX
Binary tree traversal ppt
PPTX
Unit 6 tree
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
PPT
Data Structure: TREES
PPT
PPT
Data Structures 5
Lecture 5 trees
Lecture notes data structures tree
Ch13 Binary Search Tree
Binary Search Tree
Tree
Treesandgraphs
Mca iii dfs u-4 tree and graph
introduction to_trees
Tree - Data Structure
Tree
Unit – vi tree
Binary tree traversal ppt
Unit 6 tree
Trees, Binary Search Tree, AVL Tree in Data Structures
Data Structure: TREES
Data Structures 5
Ad

Similar to Binary search tree (20)

PPTX
learn tree, linked list, queue, stack, and other algo
PPT
BINARY SEARCH TREE
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
PDF
Unit iv data structure-converted
PPTX
Trees in data structure
PPTX
Introduction to Tree_Data Structure.pptx
PPTX
Search tree,Tree and binary tree and heap tree
PPTX
GEC_Soumik[1].pptx.interview of a person from JU.
PPTX
trees in data structure
PPTX
Saikat techhnology of techtechhnology of techGhorai.pptx
PDF
B Tree, Introduction ,example,Splay tree
PPTX
Binary tree
PPTX
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
PPTX
lecture on introduction to Binary Search Tree
PPTX
Group 5-DSA.pptx........................
PPTX
VCE Unit 05.pptx
PPTX
presentation 1 binary search tree in data structures.pptx
PPTX
Tree.pptx
PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
learn tree, linked list, queue, stack, and other algo
BINARY SEARCH TREE
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
Unit iv data structure-converted
Trees in data structure
Introduction to Tree_Data Structure.pptx
Search tree,Tree and binary tree and heap tree
GEC_Soumik[1].pptx.interview of a person from JU.
trees in data structure
Saikat techhnology of techtechhnology of techGhorai.pptx
B Tree, Introduction ,example,Splay tree
Binary tree
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
lecture on introduction to Binary Search Tree
Group 5-DSA.pptx........................
VCE Unit 05.pptx
presentation 1 binary search tree in data structures.pptx
Tree.pptx
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
Ad

Recently uploaded (20)

PPTX
9 Bioterrorism.pptxnsbhsjdgdhdvkdbebrkndbd
PPTX
cp-and-safeguarding-training-2018-2019-mmfv2-230818062456-767bc1a7.pptx
PDF
CS3352FOUNDATION OF DATA SCIENCE _1_MAterial.pdf
PPTX
AI AND ML PROPOSAL PRESENTATION MUST.pptx
PDF
technical specifications solar ear 2025.
PPTX
AI_Agriculture_Presentation_Enhanced.pptx
PPTX
recommendation Project PPT with details attached
PPTX
DATA MODELING, data model concepts, types of data concepts
PPTX
ai agent creaction with langgraph_presentation_
PPTX
machinelearningoverview-250809184828-927201d2.pptx
PDF
Grey Minimalist Professional Project Presentation (1).pdf
PDF
book-34714 (2).pdfhjkkljgfdssawtjiiiiiujj
PPTX
865628565-Pertemuan-2-chapter-03-NUMERICAL-MEASURES.pptx
PPTX
Stats annual compiled ipd opd ot br 2024
PDF
REPORT CARD OF GRADE 2 2025-2026 MATATAG
PDF
The Role of Pathology AI in Translational Cancer Research and Education
PDF
A biomechanical Functional analysis of the masitary muscles in man
PPT
PROJECT CYCLE MANAGEMENT FRAMEWORK (PCM).ppt
PPTX
chuitkarjhanbijunsdivndsijvndiucbhsaxnmzsicvjsd
PPTX
Introduction to Fundamentals of Data Security
9 Bioterrorism.pptxnsbhsjdgdhdvkdbebrkndbd
cp-and-safeguarding-training-2018-2019-mmfv2-230818062456-767bc1a7.pptx
CS3352FOUNDATION OF DATA SCIENCE _1_MAterial.pdf
AI AND ML PROPOSAL PRESENTATION MUST.pptx
technical specifications solar ear 2025.
AI_Agriculture_Presentation_Enhanced.pptx
recommendation Project PPT with details attached
DATA MODELING, data model concepts, types of data concepts
ai agent creaction with langgraph_presentation_
machinelearningoverview-250809184828-927201d2.pptx
Grey Minimalist Professional Project Presentation (1).pdf
book-34714 (2).pdfhjkkljgfdssawtjiiiiiujj
865628565-Pertemuan-2-chapter-03-NUMERICAL-MEASURES.pptx
Stats annual compiled ipd opd ot br 2024
REPORT CARD OF GRADE 2 2025-2026 MATATAG
The Role of Pathology AI in Translational Cancer Research and Education
A biomechanical Functional analysis of the masitary muscles in man
PROJECT CYCLE MANAGEMENT FRAMEWORK (PCM).ppt
chuitkarjhanbijunsdivndsijvndiucbhsaxnmzsicvjsd
Introduction to Fundamentals of Data Security

Binary search tree

  • 2. Binary search tree Sana Yameen Presented By:
  • 3. Definition • A binary search tree is a rooted binary tree, whose internal nodes each store a key (and optionally, an associated value) and each have two distinguished sub-trees, commonly denoted left and right. • The left sub-tree of a node has a key less than or equal to its parent node's key. • The right sub-tree of a node has a key greater than to its parent node's key.
  • 4. Representation • BST is a collection of nodes arranged in a way where they maintain BST properties. Each node has a key and an associated value. While searching, the desired key is compared to the keys in BST and if found, the associated value is retrieved. • left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
  • 5. Some BST terminologies • The root node is the top node of tree. • A child node has exactly one parent node and a parent node has at most two child nodes, sibling nodes shares the same parent node. • A leaf node has no child nodes, an interior node has at least one child node. • Every node in BST is a sub tree of the BST rooted at the node.
  • 6. Basic Operations • Search • Inserting • Deleting • Traversing
  • 7. Searching in BST • Searching a binary search tree for a specific key can be programmed recursively or iteratively. • To search a given key in Binary Search Tree, we first compare it with root, if the key is present at root, we return root. If key is greater than root’s key, we recur for right sub tree of root node. Otherwise we recur for left sub tree.
  • 8. Algorithm of searching If root==search-data Return root Else While data not found If search-data>node-data Go to right sub tree Else Go to left sub tree If data found Return node End while Return data not found End if
  • 10. Inserting Whenever an element is to be inserted, first locate its proper location. Start searching from the root node, then if the data is less than the key value, search for the empty location in the left subtree and insert the data. Otherwise, search for the empty location in the right sub tree and insert the data.
  • 11. Binary search tree. Adding a value Adding a value to BST can be divided into two stages: • Search for a place to put a new element • Insert the new element to this place. • A new key is always inserted at leaf. We start searching a key from root till we hit a leaf node. Once a leaf node is found, the new node is added as a child of the leaf node.
  • 12. Algorithm of inserting If root==null Create root node Return If root exist then If data> node Go to right sub tree Else Go to left sub tree End if Insert data End if
  • 14. Deleting There are three possible cases to consider: • Deleting a node with no children: simply remove the node from the tree. • Deleting a node with one child: remove the node and replace it with its child. • Deleting a node with two children: call the node to be deleted N. Do not delete N. Instead, choose either its in-order successor node or its in-order predecessor node, R. Copy the value of R to N, then recursively call delete on the original R until reaching one of the first two cases.
  • 15. Deleting a node with no children: simply remove the node from the tree.
  • 16. Deleting a node with one child: remove the node and replace it with its child.
  • 17. Deleting a node with two children:
  • 18. Traversing Traversal of a binary tree is to access every node of binary tree at most. Such traversals are classified by the order in which the nodes are visited. There are three ways of traversing: Pre order (Root, Left, Right) In order (Left, Root, Right) Post order (Left, Right, Root)
  • 19. Pre order traversing In this traversal method, the root node is visited first, then the left subtree and finally the right subtree. Pre-order (Root, Left, Right) : F, B, A, D, C, E, G, I, H.
  • 20. Algorithm of pre order Until all nodes are traversed − Step 1 − Visit root node. Step 2 − Recursively traverse left subtree. Step 3 − Recursively traverse right subtree.
  • 21. In order traversing • In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We should always remember that every node may represent a subtree itself. • If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order. In-order (Left, Root, Right) : A, B, C, D, E, F, G, H, I.
  • 22. Algorithm of in order • Until all nodes are traversed − • Step 1 − Recursively traverse left subtree. • Step 2 − Visit root node. • Step 3 − Recursively traverse right subtree.
  • 23. Post order traversing In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node. Post-order(Left, Right, Root): A, C, E, D, B, H, I, G, F.
  • 24. Algorithm of post order traversing • Until all nodes are traversed − • Step 1 − Recursively traverse left subtree. • Step 2 − Recursively traverse right subtree. • Step 3 − Visit root node.