SlideShare a Scribd company logo
1 of 18
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

What's hot

What's hot (20)

Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Lecture7 data structure(tree)
 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Bst(Binary Search Tree)
Bst(Binary Search Tree)Bst(Binary Search Tree)
Bst(Binary Search Tree)
 
Binary trees
Binary treesBinary trees
Binary trees
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Tree
TreeTree
Tree
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)
 
BinarySearchTree-bddicken
BinarySearchTree-bddickenBinarySearchTree-bddicken
BinarySearchTree-bddicken
 
Binary searchtrees
Binary searchtreesBinary searchtrees
Binary searchtrees
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 

Similar to binary search tree

Similar to binary search tree (20)

Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
 
Trees
TreesTrees
Trees
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithms
 
8.binry search tree
8.binry search tree8.binry search tree
8.binry search tree
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
 
Linked lists
Linked listsLinked lists
Linked lists
 
4.linked list(contd.)
4.linked list(contd.)4.linked list(contd.)
4.linked list(contd.)
 
Insertion and Redix Sort
Insertion and Redix SortInsertion and Redix Sort
Insertion and Redix Sort
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Sorting techniques
Sorting techniquesSorting techniques
Sorting techniques
 
arrays
arraysarrays
arrays
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 

More from Shankar Bishnoi (6)

Heap tree
Heap treeHeap tree
Heap tree
 
Avl trees 2
Avl trees 2Avl trees 2
Avl trees 2
 
Avl tree
Avl treeAvl tree
Avl tree
 
trees
trees trees
trees
 
binary tree
binary treebinary tree
binary tree
 
tree in Data Structures
tree in Data Structurestree in Data Structures
tree in Data Structures
 

Recently uploaded

microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
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
 
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
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
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
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
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
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 

Recently uploaded (20)

microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
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
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
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
 
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
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
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
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
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
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 

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