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. PTRROOT [Stores address of ROOT into PTR]
2. FLAG0 [Assume search is unsuccessful]
3. LOCNULL
4. Repeat steps while PTR!=NULL and FLAG=0
5. If ITEM =INFO(PTR) then [Item found]
FLAG1, LOCPTR
Else if ITEM < INFO(PTR) then [Item in left subtree]
PTRLEFT(PTR)
Else
PTRRIGHT(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) NEWAVIAL
b) AVAILLEFT(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]
ROOTNEW [Insert new node as root]
return
[End of if structure]
10. [Remaining steps insert new node at appropriate location in non empty tree]
5.PTRROOT, PPTRNULL
6. Repeat steps 7 and 8 while PTR!=NULL [Finding parent of new node]
7.PPTRPTR [ Make current node as parent]
8. If ITEM> INFO(PTR) then [Item>current node’s INFO part]
PTRRIGHT(PTR) [Move towards right subtree]
ELSE
PTRLEFT(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.PTRROOT, FLAG0 [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]
FLAG1, LOCPTR
Else If ITEM < INFO(PTR) then [ITEM in left subtree]
PPTRPTR [Make current node as a parent]
PTRLEFT(PTR) [Make a left child as current node]
Else [ITEM in right subtree]
PPTRPTR
PTRRIGHT(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
AVAILLOC
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]
CHILDNODENULL
Else If LEFT(LOC)!=NULL then
CHILDNODELEFT(LOC)
Else
CHILDNODERIGHT(LOC)
[End of If structure]
2.If PPTR=NULL then [No parent]
ROOTCHILDNODE
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.SUCCRIGHT(LOC), PARSUCCLOC
2. Repeat while LEFT(SUCC) !=NULL
PARSUCCSUCC [ Make successor as parent]
SUCCLEFT(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]
ROOTSUCC
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