BINARY
SEARCH TREE
-RACKSAVI.R,
I M.Sc Information Technology,
V.V.Vanniaperumal college for women,
Virudhunagar.
OBJECTIVES
 Definition of binary
search tree.
 Binary search tree
operations.
 Searching data.
 Inserting data.
 Deleting data.
OBJECTIVES Contd…
Algorithm for successor.
 Traversing the tree.
Advantages of binary
search tree.
Disadvantages of binary
search tree.
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)
Operations on binary
search tree
 Searching data.
 Inserting data.
 Deleting data.
 Traversing the tree.
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.
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
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
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.
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. 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
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
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.
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
5. Ptr = ptr->LCHILD
6. Case: ITEM > ptr->DATA
7. parent = ptr
8. ptr = ptr->RCHILD
9. Case: ptr->DATA = ITEM
10. flag = TRUE
11. EndCase
12. EndWhile
13. If (flag = FALSE) then
14. Print “ No deletion”
15. Exit
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
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
*// 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
45. Else
46. parent->RCHILD = ptr->LCHILD
47. EndIf
48. EndIf
49. EndIf
50. ReturnNode(ptr)
51. EndIf
*// DELETION : CASE 3 //*
52. If (Case = 3 )
53. ptr1 = SUCC(ptr)
54. item1 = ptr->DATA
55. Delete_BST (item1)
56. ptr->DATA = item1
57. EndIf
58. Stop
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.
1. ptr1 = PTR->RCHILD
2. If ( ptr1 =! NULL) then
3. While(ptr1->LCHILD =! NULL) do
4. ptr1 = ptr1->LCHILD
5. EndWhile
6. EndIf
7. Return( ptr1 )
8. Stop
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.
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.
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
Binary search tree

Binary search tree

  • 1.
    BINARY SEARCH TREE -RACKSAVI.R, I M.ScInformation Technology, V.V.Vanniaperumal college for women, Virudhunagar.
  • 2.
    OBJECTIVES  Definition ofbinary search tree.  Binary search tree operations.  Searching data.  Inserting data.  Deleting data.
  • 3.
    OBJECTIVES Contd… Algorithm forsuccessor.  Traversing the tree. Advantages of binary search tree. Disadvantages of binary search tree.
  • 4.
    What is Binarysearch 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)
  • 5.
    Operations on binary searchtree  Searching data.  Inserting data.  Deleting data.  Traversing the tree.
  • 6.
    Searching in Binarysearch 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 Binarysearch 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 “ITEMalready 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 Binarysearch 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
  • 15.
    5. Ptr =ptr->LCHILD 6. Case: ITEM > ptr->DATA 7. parent = ptr 8. ptr = ptr->RCHILD 9. Case: ptr->DATA = ITEM 10. flag = TRUE 11. EndCase 12. EndWhile 13. If (flag = FALSE) then 14. Print “ No deletion” 15. Exit
  • 16.
    16. EndIf *// DECIDETHE 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
  • 19.
    45. Else 46. parent->RCHILD= ptr->LCHILD 47. EndIf 48. EndIf 49. EndIf 50. ReturnNode(ptr) 51. EndIf
  • 20.
    *// DELETION :CASE 3 //* 52. If (Case = 3 ) 53. ptr1 = SUCC(ptr) 54. item1 = ptr->DATA 55. Delete_BST (item1) 56. ptr->DATA = item1 57. EndIf 58. Stop
  • 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.
  • 22.
    1. ptr1 =PTR->RCHILD 2. If ( ptr1 =! NULL) then 3. While(ptr1->LCHILD =! NULL) do 4. ptr1 = ptr1->LCHILD 5. EndWhile 6. EndIf 7. Return( ptr1 ) 8. Stop
  • 23.
    Traversal in Binarysearch 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 Binarysearch 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 Binarysearch 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