SlideShare a Scribd company logo
13
Most read
15
Most read
16
Most read
introduction,
searching,
insertion and
deletion
Binary Search Trees (BST)
A data structure for efficient searching, insertion
and deletion
Binary search tree property
For every node X
All the keys in its left
subtree are smaller than
the key value in X
All the keys in its right
subtree are larger than the
key value in X
Binary Search Trees
A binary search tree Not a binary search tree
Binary Search Trees
Average depth of a node is O(log N)
Maximum depth of a node is O(N)
The same set of keys may have different BSTs
Searching BST
If we are searching for root (15), then we are done.
If we are searching for a key < root , then we
should search in the left subtree.
If we are searching for a key > root, then we should
search in the right subtree.
Data Structure and Algorithms Binary Search Tree
Searching (Find)
FIND(info, left, right, root, item, loc, par)- finds the item in tree T with root is root and
info, left and right is three array represented in memory. This algorithm returns loc
i.e. location of item and par i.e. parent.
1. [Tree Empty??]
if root==NULL, then set LOC=NULL & PAR=NULL and return.
1. [Item root ??]
If item==INFO[ROOT], then LOC=ROOT & PAR=NULL and return.
1. [Initialize pointer ptr and save]
If item<INFO[ROOT]
then set PTR = LEFT[ROOT] and SAVE=ROOT
Else
set PTR = RIGHT[ROOT] and SAVE=ROOT
[End of if]
1. Repeat 5 and 6 while ptr!=NULL
2. [item found??]
If ITEM=INFO[PTR], then set LOC=PTR and PAR=SAVE, and return.
1. If ITEM<INFO[PTR], then SAVE=PTR and PTR=LEFT[PTR]
Else
Set SAVE=PTR and PTR=RIGHT[PTR]
1. [Search unsuccessful] Set, LOC=NULL and PAR = SAVE
2. Exit
 Time complexity: O(height of the tree)
Sorting: Inorder Traversal of BST
Inorder Traversal of BST prints out all the keys in
sorted order
Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
Insertion
Proceed down the tree as you would with a find
If X is found, do nothing (or update something)
Otherwise, insert X at the last spot on the path traversed
Time complexity = O(height of the tree)
Inserting (ADD node)INSBST(info, left, right, root, item, loc, avail)- insert the item in tree
T with root is root and info, left and right is three array
represented in memory. This algorithm returns loc i.e. location of
item or ADD item as new node in tree.
1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)
2. If LOC!=NULL, then Exit.
3. [Copy ITEM into new node in AVAIL list]
a) If AVAIL==NULL, Print “OVER FLOW”;
b) Set NEW=AVAIL, AVAIL=LEFT[AVAIL] and
INFO[NEW]=ITEM.
c) Set LOC=NEW,LEFT[NEW]=RIGHT[NEW]=NULL
4. [ADD ITEM to TREE]
If PAR=NULL then, Set ROOT=NEW.
Else IF ITEM<INFO[PAR] , Set LEFT[PAR]=NEW
Else Set RIGHT[PAR]=NEW
1. Exit
Time complexity: O(height of the tree)
Deletion
When we delete a node, we need to consider how we
take care of the children of the deleted node.
This has to be done such that the property of the
search tree is maintained.
Deletion under Different Cases
Case 1: the node is a leaf
Delete it immediately
Case 2: the node has one child
Adjust a pointer from the parent to bypass that node
Deletion Case 3
Case 3: the node has 2 children
Replace the key of that node with the minimum element
at the right subtree
Delete that minimum element
 Has either no child or only right child because if it has a left
child, that left child would be smaller and would have been
chosen. So invoke case 1 or 2.
 Time complexity = O(height of the tree)
Deletion Algorithm
DEL(INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM)
A binary search tree T is in memory, and an ITEM of information is
given. This algorithm delete ITEM from the tree.
1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)
2. If LOC=NULL, then write ITEM not in tree and Exit
3. If RIGHT[LOC]!=NULL and LEFT[LOC]!=NULL, then:
Call CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR)
Else:
Call CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR)
4. Set LEFT[LOC]:=AVAIL and AVAIL :=LOC.
5. Exit
CASEA: only one or, no child
CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR)-delete
the Node N at location LOC, where N doesn’t have two
Children. PAR is location of parent node or, PAR=NULL
i.e. ROOT node.
1. [initialize CHILD]
If LEFT[LOC]=NULL and RIGHT[LOC]=NULL, then
CHILD=NULL
Else if LEFT[LOC]!=NULL , then CHILD=LEFT[LOC]
Else CHILD=RIGHT[LOC]
1. If PAR != NULL then: (i.e. NOT A ROOT NODE)
If LOC=LEFT[PAR], then set LEFT[PAR]=CHILD
Else RIGHT[PAR]=CHILD
[End of IF]
Else set ROOT=CHILD.
[End of IF]
1. Exit
CASEB: has 2 children
 CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR)-delete the Node N at location
LOC, where N has two Children. PAR is location of parent node or, PAR=NULL i.e.
ROOT node. SUC gives location of inorder successor and PARSUC gives location
of parent of inorder successor .
1. [Find SUC and PARSUC]
a) Set PTR=RIGHT[LOC] and SAVE=LOC
b) Repeat while LEFT[PTR]!=NULL
Set, SAVE=PTR and PTR=LEFT[PTR]
[END OF LOOP]
a) Set SUC=PTR and PARSUC=SAVE.
2. [Delete SUC] Call CASEA(INFO, LEFT, RIGHT, ROOT, SUC,PARSUC)
3. [replace node N by SUC]
a) If PAR != NULL then: (i.e. NOT A ROOT NODE)
If LOC=LEFT[PAR], then set LEFT[PAR]=SUC
Else RIGHT[PAR]=SUC
[End of IF]
Else set ROOT=SUC.
[End of IF]
b) Set, LEFT[SUC]=LEFT[LOC] and
RIGHT[SUC]=RIGHT[LOC]
4. Exit
Data Structure and Algorithms Binary Search Tree

More Related Content

PPT
Probability
Maria Romina Angustia
 
PPTX
Tree Traversal
Md. Israil Fakir
 
PPTX
Introduction to ML (Machine Learning)
SwatiTripathi44
 
PPT
Binary search tree in data structures
chauhankapil
 
PPT
1.1 binary tree
Krish_ver2
 
PDF
Data structure ppt
Prof. Dr. K. Adisesha
 
PDF
FINE-TUNING LLAMA 2: DOMAIN ADAPTATION OF A PRE-TRAINED MODEL
ChristopherTHyatt
 
PPTX
Binary Search Tree
sagar yadav
 
Probability
Maria Romina Angustia
 
Tree Traversal
Md. Israil Fakir
 
Introduction to ML (Machine Learning)
SwatiTripathi44
 
Binary search tree in data structures
chauhankapil
 
1.1 binary tree
Krish_ver2
 
Data structure ppt
Prof. Dr. K. Adisesha
 
FINE-TUNING LLAMA 2: DOMAIN ADAPTATION OF A PRE-TRAINED MODEL
ChristopherTHyatt
 
Binary Search Tree
sagar yadav
 

What's hot (20)

PPTX
Terminology of tree
RacksaviR
 
PPTX
Binary Search Tree
Abhishek L.R
 
PPT
List Data Structure
Zidny Nafan
 
PPTX
Trees data structure
Sumit Gupta
 
PPTX
AVL Tree Data Structure
Afaq Mansoor Khan
 
PPT
Binary search tree(bst)
Hossain Md Shakhawat
 
PPTX
AVL Tree in Data Structure
Vrushali Dhanokar
 
PPSX
Data Structure (Queue)
Adam Mukharil Bachtiar
 
PPTX
Priority queue in DSA
junnubabu
 
PPTX
Binary Tree in Data Structure
Meghaj Mallick
 
PPTX
My lectures circular queue
Senthil Kumar
 
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
PPTX
linked list in data structure
shameen khan
 
PPTX
trees in data structure
shameen khan
 
PPTX
Linked list
akshat360
 
PPTX
Binary Search Tree in Data Structure
Dharita Chokshi
 
PPTX
Quick sort-Data Structure
Jeanie Arnoco
 
PPTX
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
Terminology of tree
RacksaviR
 
Binary Search Tree
Abhishek L.R
 
List Data Structure
Zidny Nafan
 
Trees data structure
Sumit Gupta
 
AVL Tree Data Structure
Afaq Mansoor Khan
 
Binary search tree(bst)
Hossain Md Shakhawat
 
AVL Tree in Data Structure
Vrushali Dhanokar
 
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Priority queue in DSA
junnubabu
 
Binary Tree in Data Structure
Meghaj Mallick
 
My lectures circular queue
Senthil Kumar
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
linked list in data structure
shameen khan
 
trees in data structure
shameen khan
 
Linked list
akshat360
 
Binary Search Tree in Data Structure
Dharita Chokshi
 
Quick sort-Data Structure
Jeanie Arnoco
 
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
Ad

Similar to Data Structure and Algorithms Binary Search Tree (20)

PPTX
8.binry search tree
Chandan Singh
 
PPTX
Lecture 9 data structures and algorithms
Aakash deep Singhal
 
PPTX
Data Strcutres-Non Linear DS-Advanced Trees
sailaja156145
 
PPT
Binary Search Tree
Zafar Ayub
 
PDF
BST.pdf
KingKing109265
 
PPTX
BST_algorithm.pptx
MahdiHasan86
 
PDF
8 chapter4 trees_bst
SSE_AndyLi
 
PPT
Introduction to data structure by anil dutt
Anil Dutt
 
DOCX
Biary search Tree.docx
sowmya koneru
 
PPTX
Binary trees1
Saurabh Mishra
 
PDF
Skiena algorithm 2007 lecture05 dictionary data structure trees
zukun
 
PPTX
Data Structures
Rahul Jamwal
 
PPTX
Demonstrate interpolation search
manojmanoj218596
 
PPT
Unit 4 tree
kalyanineve
 
PPT
arrays
Enakshi Chanda
 
PPTX
presentation 1 binary search tree in data structures.pptx
TayybaGhaffar1
 
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
sahadevbkbiet2023
 
PPT
Unit III Heaps.ppt
RohitkumarYadav80
 
PPT
Cinterviews Binarysearch Tree
cinterviews
 
8.binry search tree
Chandan Singh
 
Lecture 9 data structures and algorithms
Aakash deep Singhal
 
Data Strcutres-Non Linear DS-Advanced Trees
sailaja156145
 
Binary Search Tree
Zafar Ayub
 
BST_algorithm.pptx
MahdiHasan86
 
8 chapter4 trees_bst
SSE_AndyLi
 
Introduction to data structure by anil dutt
Anil Dutt
 
Biary search Tree.docx
sowmya koneru
 
Binary trees1
Saurabh Mishra
 
Skiena algorithm 2007 lecture05 dictionary data structure trees
zukun
 
Data Structures
Rahul Jamwal
 
Demonstrate interpolation search
manojmanoj218596
 
Unit 4 tree
kalyanineve
 
presentation 1 binary search tree in data structures.pptx
TayybaGhaffar1
 
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
sahadevbkbiet2023
 
Unit III Heaps.ppt
RohitkumarYadav80
 
Cinterviews Binarysearch Tree
cinterviews
 
Ad

More from ManishPrajapati78 (15)

PPT
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
PPT
Data Structure and Algorithms Queues
ManishPrajapati78
 
PPTX
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
PPTX
Data Structure and Algorithms The Tower of Hanoi
ManishPrajapati78
 
PPT
Data Structure and Algorithms Stacks
ManishPrajapati78
 
PPT
Data Structure and Algorithms Linked List
ManishPrajapati78
 
PPT
Data Structure and Algorithms Sorting
ManishPrajapati78
 
PPT
Data Structure and Algorithms Arrays
ManishPrajapati78
 
PPT
Data Structure and Algorithms
ManishPrajapati78
 
PPT
Data Structure and Algorithms Hashing
ManishPrajapati78
 
PPTX
Data Structure and Algorithms Graph Traversal
ManishPrajapati78
 
PPT
Data Structure and Algorithms Graphs
ManishPrajapati78
 
PPT
Data Structure and Algorithms Huffman Coding Algorithm
ManishPrajapati78
 
PPT
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
PPT
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
Data Structure and Algorithms Queues
ManishPrajapati78
 
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
Data Structure and Algorithms The Tower of Hanoi
ManishPrajapati78
 
Data Structure and Algorithms Stacks
ManishPrajapati78
 
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Data Structure and Algorithms Sorting
ManishPrajapati78
 
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Data Structure and Algorithms
ManishPrajapati78
 
Data Structure and Algorithms Hashing
ManishPrajapati78
 
Data Structure and Algorithms Graph Traversal
ManishPrajapati78
 
Data Structure and Algorithms Graphs
ManishPrajapati78
 
Data Structure and Algorithms Huffman Coding Algorithm
ManishPrajapati78
 
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 

Recently uploaded (20)

PDF
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
PPTX
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PPTX
TestNG for Java Testing and Automation testing
ssuser0213cb
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PDF
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
oapresentation.pptx
mehatdhavalrajubhai
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
TestNG for Java Testing and Automation testing
ssuser0213cb
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
Become an Agentblazer Champion Challenge
Dele Amefo
 

Data Structure and Algorithms Binary Search Tree

  • 2. Binary Search Trees (BST) A data structure for efficient searching, insertion and deletion Binary search tree property For every node X All the keys in its left subtree are smaller than the key value in X All the keys in its right subtree are larger than the key value in X
  • 3. Binary Search Trees A binary search tree Not a binary search tree
  • 4. Binary Search Trees Average depth of a node is O(log N) Maximum depth of a node is O(N) The same set of keys may have different BSTs
  • 5. Searching BST If we are searching for root (15), then we are done. If we are searching for a key < root , then we should search in the left subtree. If we are searching for a key > root, then we should search in the right subtree.
  • 7. Searching (Find) FIND(info, left, right, root, item, loc, par)- finds the item in tree T with root is root and info, left and right is three array represented in memory. This algorithm returns loc i.e. location of item and par i.e. parent. 1. [Tree Empty??] if root==NULL, then set LOC=NULL & PAR=NULL and return. 1. [Item root ??] If item==INFO[ROOT], then LOC=ROOT & PAR=NULL and return. 1. [Initialize pointer ptr and save] If item<INFO[ROOT] then set PTR = LEFT[ROOT] and SAVE=ROOT Else set PTR = RIGHT[ROOT] and SAVE=ROOT [End of if] 1. Repeat 5 and 6 while ptr!=NULL 2. [item found??] If ITEM=INFO[PTR], then set LOC=PTR and PAR=SAVE, and return. 1. If ITEM<INFO[PTR], then SAVE=PTR and PTR=LEFT[PTR] Else Set SAVE=PTR and PTR=RIGHT[PTR] 1. [Search unsuccessful] Set, LOC=NULL and PAR = SAVE 2. Exit  Time complexity: O(height of the tree)
  • 8. Sorting: Inorder Traversal of BST Inorder Traversal of BST prints out all the keys in sorted order Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
  • 9. Insertion Proceed down the tree as you would with a find If X is found, do nothing (or update something) Otherwise, insert X at the last spot on the path traversed Time complexity = O(height of the tree)
  • 10. Inserting (ADD node)INSBST(info, left, right, root, item, loc, avail)- insert the item in tree T with root is root and info, left and right is three array represented in memory. This algorithm returns loc i.e. location of item or ADD item as new node in tree. 1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR) 2. If LOC!=NULL, then Exit. 3. [Copy ITEM into new node in AVAIL list] a) If AVAIL==NULL, Print “OVER FLOW”; b) Set NEW=AVAIL, AVAIL=LEFT[AVAIL] and INFO[NEW]=ITEM. c) Set LOC=NEW,LEFT[NEW]=RIGHT[NEW]=NULL 4. [ADD ITEM to TREE] If PAR=NULL then, Set ROOT=NEW. Else IF ITEM<INFO[PAR] , Set LEFT[PAR]=NEW Else Set RIGHT[PAR]=NEW 1. Exit Time complexity: O(height of the tree)
  • 11. Deletion When we delete a node, we need to consider how we take care of the children of the deleted node. This has to be done such that the property of the search tree is maintained.
  • 12. Deletion under Different Cases Case 1: the node is a leaf Delete it immediately Case 2: the node has one child Adjust a pointer from the parent to bypass that node
  • 13. Deletion Case 3 Case 3: the node has 2 children Replace the key of that node with the minimum element at the right subtree Delete that minimum element  Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2.  Time complexity = O(height of the tree)
  • 14. Deletion Algorithm DEL(INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM) A binary search tree T is in memory, and an ITEM of information is given. This algorithm delete ITEM from the tree. 1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR) 2. If LOC=NULL, then write ITEM not in tree and Exit 3. If RIGHT[LOC]!=NULL and LEFT[LOC]!=NULL, then: Call CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR) Else: Call CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR) 4. Set LEFT[LOC]:=AVAIL and AVAIL :=LOC. 5. Exit
  • 15. CASEA: only one or, no child CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR)-delete the Node N at location LOC, where N doesn’t have two Children. PAR is location of parent node or, PAR=NULL i.e. ROOT node. 1. [initialize CHILD] If LEFT[LOC]=NULL and RIGHT[LOC]=NULL, then CHILD=NULL Else if LEFT[LOC]!=NULL , then CHILD=LEFT[LOC] Else CHILD=RIGHT[LOC] 1. If PAR != NULL then: (i.e. NOT A ROOT NODE) If LOC=LEFT[PAR], then set LEFT[PAR]=CHILD Else RIGHT[PAR]=CHILD [End of IF] Else set ROOT=CHILD. [End of IF] 1. Exit
  • 16. CASEB: has 2 children  CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR)-delete the Node N at location LOC, where N has two Children. PAR is location of parent node or, PAR=NULL i.e. ROOT node. SUC gives location of inorder successor and PARSUC gives location of parent of inorder successor . 1. [Find SUC and PARSUC] a) Set PTR=RIGHT[LOC] and SAVE=LOC b) Repeat while LEFT[PTR]!=NULL Set, SAVE=PTR and PTR=LEFT[PTR] [END OF LOOP] a) Set SUC=PTR and PARSUC=SAVE. 2. [Delete SUC] Call CASEA(INFO, LEFT, RIGHT, ROOT, SUC,PARSUC) 3. [replace node N by SUC] a) If PAR != NULL then: (i.e. NOT A ROOT NODE) If LOC=LEFT[PAR], then set LEFT[PAR]=SUC Else RIGHT[PAR]=SUC [End of IF] Else set ROOT=SUC. [End of IF] b) Set, LEFT[SUC]=LEFT[LOC] and RIGHT[SUC]=RIGHT[LOC] 4. Exit