SlideShare a Scribd company logo
Binary Search Tree
INTRODUCTION 
• Binary Search Tree abbreviated as BST is a special 
form of binary tree whose nodes are arranged in 
such a way that for every node N , the values 
contained in all nodes in its left sub tree are less the 
value contained in N and values contained in the 
right sub tree are larger than the node N
SEARCHING 
• Searching an item is the key operation performed on a BST. 
When we want to search given item(ITEM) in BST ,we begin by 
first comparing the ITEM with the value in root. 
• A)if they are equal then the location of the root node is 
returned. 
• B)If the ITEM is less than the value of the root then we need to 
search the left sub tree of the root. The right sub tree is 
eliminated . 
• C)if the ITEM is greater than the value of the root then we 
need to search the right sub tree of the root.
Searching in BST 
• BSTSEARCH(ROOT,ITEM): Given linked BST whose root 
node is pointed to by pointer ROOT. A variable PTR is 
used which is a pointer that points to the current node 
being processed. Another local variable LOC returns the 
location of a given ITEM. The local variable FLAG contains 
Boolean value which is TRUE(i.e. 1) if search is successful 
otherwise FALSE(i.e. 0) . This algo searches for a given 
ITEM from BST.
1. PTRROOT [Stores address of ROOT into PTR] 
2. FLAG0 [Assume search is unsuccessful] 
3. LOCNULL 
4. Repeat steps while PTR!=NULL and FLAG=0 
5. If ITEM =INFO(PTR) then [Item found] 
FLAG1, LOCPTR 
Else if ITEM < INFO(PTR) then [Item in left subtree] 
PTRLEFT(PTR) 
Else 
PTRRIGHT(PTR) [Item in right subtree] 
[End if structure] 
[End of step 4 loop]
INSERTION IN BST 
• Insertion of new node into binary search tree is very 
simple operation based on the search operation. 
• In order to insert a node with given item into BST ,we 
begin searching at the root and descend level by level 
moving left or right subtree as appropriate comparing the 
value of the current node and node being inserted. 
• When we reach a node with empty subtree(i.e leaf) then 
we insert the new node as its left or right child depending 
on ITEM.
• While searching if the ITEM to be inserted is 
already in the tree then do nothing as we know 
binary tree cannot hold the duplicate values. 
• Also note that as insertion take place only at leaf 
node so we only need ITEM to be inserted and not 
the location of the insertion
• BST_INSERT(ROOT,ITEM,AVAIL) –Given a linked 
list BST whose root node is pointed to by a pointer 
ROOT.A local variable PTR is used which points to the 
current node being processed. The variable PPTR is a 
pointer that points to the parent of the new node 
containing ITEM in its INFO part. The pointer 
variable NEW points to the new node and the AVAIL 
pointer points to the first node in the availability 
list.This algorithm inserts the given ITEM into BST.
1. If AVAIL=NULL then [No space for new node] 
Write “ Overflow”. 
return 
[End of if structure] 
2. [Get new node from availability list] 
a) NEWAVIAL 
b) AVAILLEFT(AVAIL) 
c) INFO(NEW)ITEM [Copy ITEM into INFO part of new node] 
3.[New node has no children] 
a) LEFT(NEW)NULL 
b) RIGHT(NEW)NULL 
4.If ROOT=NULL then [Tree is empty] 
ROOTNEW [Insert new node as root] 
return 
[End of if structure]
[Remaining steps insert new node at appropriate location in non empty tree] 
5.PTRROOT, PPTRNULL 
6. Repeat steps 7 and 8 while PTR!=NULL [Finding parent of new node] 
7.PPTRPTR [ Make current node as parent] 
8. If ITEM> INFO(PTR) then [Item>current node’s INFO part] 
PTRRIGHT(PTR) [Move towards right subtree] 
ELSE 
PTRLEFT(PTR) [Move towards left subtree] 
[End of If structure] 
[End of step 6 loop] 
9.If ITEM<INFO(PPTR) then 
LEFT(PPTR)NEW [Insert new node as left child] 
Else 
RIGHT(PPTR)NEW [Insert new as right child] 
10. Return
DELETION IN BST 
BSTDELETE(ROOT,ITEM ,AVAIL)-Given a linked binary 
search tree whose root is pointed to by pointer ROOT. A local variable 
PTR is used which is a pointer that points to the current node being 
processed. The variable PPTR is a pointer that points to the parent of 
ITEM. The local variable LOC returns the location of given ITEM that is 
to be deleted. The FLAG variable contains a Boolean value which is TRUE 
if search is successful , other wise FALSE. This algo deletes the given 
ITEM from BST.
1.PTRROOT, FLAG0 [Initialize PTR, assume search successful] 
2. Repeat steps 3 while PTR!=NULL [All nodes served ] and FLAG=0 [Search Unsuccessful] 
3. If ITEM=INFO(PTR) then [Node to be deleted found] 
FLAG1, LOCPTR 
Else If ITEM < INFO(PTR) then [ITEM in left subtree] 
PPTRPTR [Make current node as a parent] 
PTRLEFT(PTR) [Make a left child as current node] 
Else [ITEM in right subtree] 
PPTRPTR 
PTRRIGHT(PTR) 
[End of If structure] 
[End of step 3 loop] 
4. If LOC=NULL then 
Write “ITEM does not exist” 
return
[End of If structure] 
5. If RIGHT(LOC)!=NULL and LEFT(LOC)!=NULL then [ Case 3] 
Call DELNODE_TWOCH(ROOT, LOC, PPTR) 
Else[Case 1 and Case 2] 
Call DELNODE_ZRONECH(ROOT,LOC,PPTR) 
[End of If structure] 
6.[Returning deleted node to availability list] 
LEFT(LOC)AVAIL 
AVAILLOC 
7. Return
DELNODE_ZRONECH 
• DELNODE_ZRONECH (ROOT,LOC,PPTR): 
This algorithm deletes a node with zero or one 
child from BST. A local variable CHILDNODE 
will be set to NULL if the deleted node does 
not have any child or will point to left or right 
child of deleted node otherwise.
1.[Initialize childnode] 
If LEFT(LOC)=NULL and RIGHT(LOC)=NULL then [No Child] 
CHILDNODENULL 
Else If LEFT(LOC)!=NULL then 
CHILDNODELEFT(LOC) 
Else 
CHILDNODERIGHT(LOC) 
[End of If structure] 
2.If PPTR=NULL then [No parent] 
ROOTCHILDNODE 
Else If LOC=LEFT(PPTR) then [Deleted node to the left of parent node] 
LEFT(PPTR)CHILDNODE 
ELSE [Deleted Node to the right of parent node] 
RIGHT(PPTR)CHILDNODE 
[End of If structure] [End of structure] 3. Return
DELNODE_TWOCH 
DELNODE_TWOCH(ROOT,LOC,PPTR): This algorithm 
deletes a node with two children from BST . LOC points to 
the node to be deleted and PPTR points to its parent. A local 
variable SUCC points to the inorder successor of node to be 
deleted. The local variable PARSUCC points to the parent of 
inorder successor.
[Find inorder successor node and parent of successor node[Step 1 and 2]] 
1.SUCCRIGHT(LOC), PARSUCCLOC 
2. Repeat while LEFT(SUCC) !=NULL 
PARSUCCSUCC [ Make successor as parent] 
SUCCLEFT(SUCC) [ Make left child of successor as successor node] 
[End of Step 2 loop] 
3. Call DELNODE_ZRONECH(ROOT,SUCC,PARSUCC) [Delete inorder successor] 
[Steps 4 and 5 replace node N by its inorder successor] 
4. If PARSUCC=NULL then [No Parent] 
ROOTSUCC 
Else if LOC=LEFT(PPTR) then [deleted node to left of its parent] 
LEFT(PPTR)SUCC 
Else [deleted node to right of its parent] 
RIGHT(PPTR)SUCC
[End of If structure] 
5. a) LEFT(SUCC)LEFT(LOC) 
b) RIGHT(SUCC)RIGHT(LOC) 
6. Return

More Related Content

PDF
Tree and binary tree
PPTX
Lecture 9 data structures and algorithms
PPT
Binary search tree(bst)
PPTX
Binary tree and Binary search tree
PPT
1.5 binary search tree
PPT
BINARY SEARCH TREE
PPT
Binary search trees
PPT
1.1 binary tree
Tree and binary tree
Lecture 9 data structures and algorithms
Binary search tree(bst)
Binary tree and Binary search tree
1.5 binary search tree
BINARY SEARCH TREE
Binary search trees
1.1 binary tree

What's hot (20)

PPTX
Tree traversal techniques
PPTX
Trees (data structure)
PPTX
Binary Search Tree (BST)
PPT
Binary search trees
PPT
Binary Search Tree
PPT
Data Structure and Algorithms Binary Search Tree
PPTX
THREADED BINARY TREE AND BINARY SEARCH TREE
PPT
Binary Search Tree and AVL
PPTX
Lecture 8 data structures and algorithms
PPT
Bst(Binary Search Tree)
PPT
Binary trees
PDF
Tree Data Structure by Daniyal Khan
PPTX
Binary trees1
PPT
Data Structure and Algorithms Binary Tree
PPTX
Week 8 (trees)
PDF
BinarySearchTree-bddicken
PPT
Binary searchtrees
PPT
(Binary tree)
Tree traversal techniques
Trees (data structure)
Binary Search Tree (BST)
Binary search trees
Binary Search Tree
Data Structure and Algorithms Binary Search Tree
THREADED BINARY TREE AND BINARY SEARCH TREE
Binary Search Tree and AVL
Lecture 8 data structures and algorithms
Bst(Binary Search Tree)
Binary trees
Tree Data Structure by Daniyal Khan
Binary trees1
Data Structure and Algorithms Binary Tree
Week 8 (trees)
BinarySearchTree-bddicken
Binary searchtrees
(Binary tree)
Ad

Similar to binary search tree (20)

PDF
8 chapter4 trees_bst
PPT
1.2 operations of tree representations
PPT
Lecture 7-BinarySearchTrees.ppt
PPTX
Binary search tree
PPTX
Binary Search Tree
PPTX
Binary Search Tree.pptxA binary search i
PPT
Binary searchtrees
PPTX
8.binry search tree
DOCX
Biary search Tree.docx
PPTX
Binary Search Tree
PPTX
Binary Search Tree.pptx
PPTX
Lecture_10 - Revised.pptx
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
PPT
Unit 4 tree
PDF
Chapter 7 - Binary Search Tree in the context of DSA.pdf
PPTX
Binary search tree.pptx
PDF
Trees second part in data structures with examples
PPTX
Lec 10_Binary Search Tree in data structure and algorithm.pptx
DOCX
5220191CS146 Data Structures and AlgorithmsC.docx
PPTX
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
8 chapter4 trees_bst
1.2 operations of tree representations
Lecture 7-BinarySearchTrees.ppt
Binary search tree
Binary Search Tree
Binary Search Tree.pptxA binary search i
Binary searchtrees
8.binry search tree
Biary search Tree.docx
Binary Search Tree
Binary Search Tree.pptx
Lecture_10 - Revised.pptx
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
Unit 4 tree
Chapter 7 - Binary Search Tree in the context of DSA.pdf
Binary search tree.pptx
Trees second part in data structures with examples
Lec 10_Binary Search Tree in data structure and algorithm.pptx
5220191CS146 Data Structures and AlgorithmsC.docx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
Ad

More from Shankar Bishnoi (7)

PPT
PPT
Heap tree
PPT
Avl trees 2
PPT
Avl tree
PPT
PPT
binary tree
PPT
tree in Data Structures
Heap tree
Avl trees 2
Avl tree
binary tree
tree in Data Structures

Recently uploaded (20)

PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
web development for engineering and engineering
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
PPT on Performance Review to get promotions
PPT
Project quality management in manufacturing
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
Construction Project Organization Group 2.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPT
Mechanical Engineering MATERIALS Selection
Foundation to blockchain - A guide to Blockchain Tech
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Automation-in-Manufacturing-Chapter-Introduction.pdf
web development for engineering and engineering
Model Code of Practice - Construction Work - 21102022 .pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
CH1 Production IntroductoryConcepts.pptx
PPT on Performance Review to get promotions
Project quality management in manufacturing
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
573137875-Attendance-Management-System-original
Construction Project Organization Group 2.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Mechanical Engineering MATERIALS Selection

binary search tree

  • 2. INTRODUCTION • Binary Search Tree abbreviated as BST is a special form of binary tree whose nodes are arranged in such a way that for every node N , the values contained in all nodes in its left sub tree are less the value contained in N and values contained in the right sub tree are larger than the node N
  • 3. SEARCHING • Searching an item is the key operation performed on a BST. When we want to search given item(ITEM) in BST ,we begin by first comparing the ITEM with the value in root. • A)if they are equal then the location of the root node is returned. • B)If the ITEM is less than the value of the root then we need to search the left sub tree of the root. The right sub tree is eliminated . • C)if the ITEM is greater than the value of the root then we need to search the right sub tree of the root.
  • 4. Searching in BST • BSTSEARCH(ROOT,ITEM): Given linked BST whose root node is pointed to by pointer ROOT. A variable PTR is used which is a pointer that points to the current node being processed. Another local variable LOC returns the location of a given ITEM. The local variable FLAG contains Boolean value which is TRUE(i.e. 1) if search is successful otherwise FALSE(i.e. 0) . This algo searches for a given ITEM from BST.
  • 5. 1. PTRROOT [Stores address of ROOT into PTR] 2. FLAG0 [Assume search is unsuccessful] 3. LOCNULL 4. Repeat steps while PTR!=NULL and FLAG=0 5. If ITEM =INFO(PTR) then [Item found] FLAG1, LOCPTR Else if ITEM < INFO(PTR) then [Item in left subtree] PTRLEFT(PTR) Else PTRRIGHT(PTR) [Item in right subtree] [End if structure] [End of step 4 loop]
  • 6. INSERTION IN BST • Insertion of new node into binary search tree is very simple operation based on the search operation. • In order to insert a node with given item into BST ,we begin searching at the root and descend level by level moving left or right subtree as appropriate comparing the value of the current node and node being inserted. • When we reach a node with empty subtree(i.e leaf) then we insert the new node as its left or right child depending on ITEM.
  • 7. • While searching if the ITEM to be inserted is already in the tree then do nothing as we know binary tree cannot hold the duplicate values. • Also note that as insertion take place only at leaf node so we only need ITEM to be inserted and not the location of the insertion
  • 8. • BST_INSERT(ROOT,ITEM,AVAIL) –Given a linked list BST whose root node is pointed to by a pointer ROOT.A local variable PTR is used which points to the current node being processed. The variable PPTR is a pointer that points to the parent of the new node containing ITEM in its INFO part. The pointer variable NEW points to the new node and the AVAIL pointer points to the first node in the availability list.This algorithm inserts the given ITEM into BST.
  • 9. 1. If AVAIL=NULL then [No space for new node] Write “ Overflow”. return [End of if structure] 2. [Get new node from availability list] a) NEWAVIAL b) AVAILLEFT(AVAIL) c) INFO(NEW)ITEM [Copy ITEM into INFO part of new node] 3.[New node has no children] a) LEFT(NEW)NULL b) RIGHT(NEW)NULL 4.If ROOT=NULL then [Tree is empty] ROOTNEW [Insert new node as root] return [End of if structure]
  • 10. [Remaining steps insert new node at appropriate location in non empty tree] 5.PTRROOT, PPTRNULL 6. Repeat steps 7 and 8 while PTR!=NULL [Finding parent of new node] 7.PPTRPTR [ Make current node as parent] 8. If ITEM> INFO(PTR) then [Item>current node’s INFO part] PTRRIGHT(PTR) [Move towards right subtree] ELSE PTRLEFT(PTR) [Move towards left subtree] [End of If structure] [End of step 6 loop] 9.If ITEM<INFO(PPTR) then LEFT(PPTR)NEW [Insert new node as left child] Else RIGHT(PPTR)NEW [Insert new as right child] 10. Return
  • 11. DELETION IN BST BSTDELETE(ROOT,ITEM ,AVAIL)-Given a linked binary search tree whose root is pointed to by pointer ROOT. A local variable PTR is used which is a pointer that points to the current node being processed. The variable PPTR is a pointer that points to the parent of ITEM. The local variable LOC returns the location of given ITEM that is to be deleted. The FLAG variable contains a Boolean value which is TRUE if search is successful , other wise FALSE. This algo deletes the given ITEM from BST.
  • 12. 1.PTRROOT, FLAG0 [Initialize PTR, assume search successful] 2. Repeat steps 3 while PTR!=NULL [All nodes served ] and FLAG=0 [Search Unsuccessful] 3. If ITEM=INFO(PTR) then [Node to be deleted found] FLAG1, LOCPTR Else If ITEM < INFO(PTR) then [ITEM in left subtree] PPTRPTR [Make current node as a parent] PTRLEFT(PTR) [Make a left child as current node] Else [ITEM in right subtree] PPTRPTR PTRRIGHT(PTR) [End of If structure] [End of step 3 loop] 4. If LOC=NULL then Write “ITEM does not exist” return
  • 13. [End of If structure] 5. If RIGHT(LOC)!=NULL and LEFT(LOC)!=NULL then [ Case 3] Call DELNODE_TWOCH(ROOT, LOC, PPTR) Else[Case 1 and Case 2] Call DELNODE_ZRONECH(ROOT,LOC,PPTR) [End of If structure] 6.[Returning deleted node to availability list] LEFT(LOC)AVAIL AVAILLOC 7. Return
  • 14. DELNODE_ZRONECH • DELNODE_ZRONECH (ROOT,LOC,PPTR): This algorithm deletes a node with zero or one child from BST. A local variable CHILDNODE will be set to NULL if the deleted node does not have any child or will point to left or right child of deleted node otherwise.
  • 15. 1.[Initialize childnode] If LEFT(LOC)=NULL and RIGHT(LOC)=NULL then [No Child] CHILDNODENULL Else If LEFT(LOC)!=NULL then CHILDNODELEFT(LOC) Else CHILDNODERIGHT(LOC) [End of If structure] 2.If PPTR=NULL then [No parent] ROOTCHILDNODE Else If LOC=LEFT(PPTR) then [Deleted node to the left of parent node] LEFT(PPTR)CHILDNODE ELSE [Deleted Node to the right of parent node] RIGHT(PPTR)CHILDNODE [End of If structure] [End of structure] 3. Return
  • 16. DELNODE_TWOCH DELNODE_TWOCH(ROOT,LOC,PPTR): This algorithm deletes a node with two children from BST . LOC points to the node to be deleted and PPTR points to its parent. A local variable SUCC points to the inorder successor of node to be deleted. The local variable PARSUCC points to the parent of inorder successor.
  • 17. [Find inorder successor node and parent of successor node[Step 1 and 2]] 1.SUCCRIGHT(LOC), PARSUCCLOC 2. Repeat while LEFT(SUCC) !=NULL PARSUCCSUCC [ Make successor as parent] SUCCLEFT(SUCC) [ Make left child of successor as successor node] [End of Step 2 loop] 3. Call DELNODE_ZRONECH(ROOT,SUCC,PARSUCC) [Delete inorder successor] [Steps 4 and 5 replace node N by its inorder successor] 4. If PARSUCC=NULL then [No Parent] ROOTSUCC Else if LOC=LEFT(PPTR) then [deleted node to left of its parent] LEFT(PPTR)SUCC Else [deleted node to right of its parent] RIGHT(PPTR)SUCC
  • 18. [End of If structure] 5. a) LEFT(SUCC)LEFT(LOC) b) RIGHT(SUCC)RIGHT(LOC) 6. Return