3. OBJECTIVES Contd…
Algorithm for successor.
Traversing the tree.
Advantages of binary
search tree.
Disadvantages of binary
search tree.
4. What is Binary search tree?
Binary Search Tree is a binary tree in
which every node contains only smaller
values in its left subtree and only larger
values in its right subtree.
left_subtree(Keys) <=
node(Keys) <=
right_subtree(Keys)
6. Searching in Binary search tree
Searching operation on a binary search tree,
searches for a node in the tree
Algorithm Search_BST:
Input : ITEM is the data that has to
be searched.
Output : If found then pointer to the
node containing data ITEM else a
message.
Data Structure : Linked Structure
of the binary tree.
7. STEPS:
1. Ptr = ROOT,flag = FALSE
2. While (ptr =! NULL) and (flag = FALSE) do
3. Case: ITEM < ptr->DATA
4. ptr = ptr->LCHILD
5. Case: ptr->DATA = ITEM
6. flag = TRUE
8. 7. Case: ITEM > ptr->DATA
8. ptr = ptr->RCHILD
9. EndCase
10. EndWhile
11. If (flag = TRUE) then
12. Print “ITEM has found at the node”, ptr
13. Else
14. Print “Search is unsuccessful”
15. EndIf
16. Stop
9. Inserting in Binary search tree
Inserting operation on a binary search tree,
insert a node in the tree.
Algorithm Insert_BST:
Input : ITEM is the data
component of a node that has to
be inserted.
Output : If there is no node having
data ITEM, it is inserted
into the tree else a message.
10. Data Structure : Linked Structure of
the binary tree.
STEPS:
1. Ptr = ROOT,flag = FALSE
2. While (ptr =! NULL) and (flag = FALSE) do
3. Case: ITEM < ptr->DATA
4. ptr1 = ptr
5. ptr = ptr->LCHILD
6. Case: ITEM > ptr->DATA
7. ptr1 = ptr
8. ptr = ptr->RCHILD
9. Case: ptr->DATA = ITEM
10. flag = TRUE
11. 11. Print “ITEM already exists”
12. Exit
13. EndCase
14. EndWhile
15. If (ptr = NULL) then
16. new = GetNode(NODE)
17. new->DATA = ITEM
18. new->LCHILD = NULL
12. 19. new->RCHILD = NULL
20. If(ptr1->DATA < ITEM) then
21. ptr1->RCHILD = new
22. Else
23. ptr->RCHILD = new
24. EndIf
25. EndIf
26. Stop
13. Deletion in Binary search
tree
Deletion operation on a binary search tree,
deletes a node from the tree.
Deletion of the node depends on the
number of its children. Hence, 3 cases
may arise :
Case 1 : N is the leaf node.
Case 2 : N has exactly one child.
Case 3 : N has two childs.
14. Algorithm Delete_BST:
Input : ITEM is the data of the nodes to
be deleted.
Output : If the node with data ITEM
exists it is deleted else a message.
Data Structure : Linked Structure of the
binary tree.
STEPS:
1. Ptr = ROOT,flag = FALSE
2. While (ptr =! NULL) and (flag = FLASE) do
3. Case: ITEM < ptr->DATA
4. parent = ptr
16. 16. EndIf
*// DECIDE THE CASE OF DELETION //*
17. If(ptr->LCHILD = NULL) and (ptr->RCHILD
= NULL) then
18. Case = 1
19. Else
20. If(ptr->LCHILD =! NULL) and
(ptr->RCHILD =! NULL) then
21. Case = 3
22. Else
23. Case = 2
24. EndIf
17. 25. EndIf
*// DELETION : CASE 1 //*
26.If ( Case = 1) then
27. If (parent->LCHILD = ptr)then
28. parent->LCHILD = NULL
29. Else
30. parent->RCHILD = NULL
31. EndIf
32. ReturnNode(ptr)
33. EndIf
18. *// DELETION : CASE 2 //*
34.If (Case = 2) then
35. If (parent->LCHILD = ptr) then
36. If (ptr->LCHILD = NULL) then
37. parent->LCHILD = ptr->RCHILD
38. Else
39. parent->LCHILD = ptr->LCHILD
40. EndIf
41. Else
42. If (parent->RCHILD = ptr) then
43. If (ptr->LCHILD = NULL) then
44. parent->RCHILD = ptr->RCHILD
21. Algorithm for Succossor
Input : Pointer to a node PTR whose
inorder successor is to be found.
Output : Pointer to the inorder successor
of ptr.
Data Structure : Linked Structure of
the binary tree.
23. Traversal in Binary search tree
Traversal operation on a binary
search tree, visits every node of the tree.
There are three types of traversal
they are :
In-order traversal.
Pre-order traversal.
Post-order traversal.
24. Advantages of Binary search tree
The major advantage of
binary search trees over other data
structures is that the related sorting
algorithms and search algorithms
such as in – order traversal can be
very efficient.
25. Disadvantages of Binary search tree
The shape of the Binary search
tree totally depends on the order
of insertions and it can be
regenerated
It takes a long time to search an
element in a BST because key
value of each node has to be
compared with the key element to
be searched