0% found this document useful (0 votes)
5 views

Binary Search Tree

BST notes

Uploaded by

kamitpatra06
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Binary Search Tree

BST notes

Uploaded by

kamitpatra06
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Binary Search Tree

• A binary tree T is termed binary search tree


(or binary sorted tree) if each node N of T
satisfies the following property.
– The value at N is greater than every value in the
left subtree of N and is less than every value in the
right subtree of N.
• The following operations are possible on a
binary search tree.
– Searching data
– Inserting data
– Deleting data
– Traversing data
• Searching a binary search tree:
– Suppose in a binary search tree T , ITEM is the
item of search.
– We start from the root node R.
– If ITEM is less than the value in the root node R,
we proceed to its left child.
– If ITEM is greater than the value in the node R , we
proceed to its right child.
– The process will be continued till the ITEM is not
found or we reach a leaf node.
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. Pointer to the root node is ROOT.
Steps –
1. ptr = ROOT , flag = FALSE //Start from the root
2. While (ptr ≠ NULL) and (flag = FALSE) do
3. Case: ITEM < ptr --> DATA //Go to the left subtree
4. ptr = ptr --> LCHILD
5. Case: ptr --> DATA = ITEM //Search is successful
6. flag = TRUE
7. Case: ITEM > ptr --> DATA //Go to the right subtree
4. ptr = ptr --> RCHILD
7. EndCase
8. EndWhile
9. If (flag = TRUE) then
10. Print “ITEM is found at the node” , ptr
11. Else
12. Print “ITEM does not exist : Search is unsuccessful”
13. EndIf
• Inserting a node into a binary search tree:
– To insert a node with data ITEM into a tree , the
tree is required to be searched starting from the
root node.
– If ITEM is found , do nothing .
– Otherwise ITEM is to be inserted at the dead end
where the search halts.
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. Pointer to the root node is ROOT.
Steps –

1. ptr = ROOT , flag = FALSE //Start from the root node


2. While (ptr ≠ NULL) and (flag = FALSE) do
3. Case: ITEM < ptr --> DATA // Go to the left subtree
4. ptr1 = ptr
5. ptr = ptr --> LCHILD
6. Case: ITEM > ptr --> DATA // Go to the right subtree
7. ptr1 = ptr
8. ptr = ptr --> RCHILD
9. Case: ptr --> DATA = ITEM //Node Exists
10. flag = TRUE
11. Print “ITEM already exists”
12. Exit //Quit the execution
13. EndCase
14. EndWhile
15. If (ptr = NULL) then //Insert when the search halts at the dead end
16. new = GetNode(NODE) //Avail a node and then initialize it
17. new--> DATA = ITEM
18. new --> LCHILD = NULL
19. new --> RCHILD = NULL
20. If (ptr1 --> DATA < ITEM) then //Insert as the right child
21. ptr1 --> RCHILD = new
22. Else
23. ptr1 --> LCHILD = new //Insert as the left child
24. EndIf
25. EndIf
26. Stop
• Deleting a node from a binary search tree:
– Suppose T is a binary search tree and ITEM is the
information given which has to be deleted from T.
– Suppose N is the node which contains the
information ITEM.
– PARENT(N) denotes the parent node of N and
SUCC(N) denotes the inorder successor of the
node N.
– Then the deletion of the node N depends on the
number of its children.
 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 node 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. Pointer to the root node is ROOT.
Steps –
1. ptr = ROOT , flag = FALSE
2. While (ptr ≠ NULL) and (flag = FALSE) do
3. Case: ITEM < ptr --> DATA
4. parent = ptr
5. ptr = ptr --> LCHILD
6. Case: ptr --> DATA = ITEM
7. flag = TRUE
8. Case: ITEM > ptr --> DATA
9. parent = ptr
10. ptr = ptr --> RCHILD
11. EndCase
12. EndWhile
13. If (flag = FALSE) then
14. Print “ITEM does not exist : No deletion”
15. Exit
16. EndIf
17. If (ptr --> LCHILD = NULL ) and (ptr --> RCHILD = NULL ) then
18. case = 1
33. If (case = 2) then
34. If(parent --> LCHILD = ptr) then
35. If(ptr --> LCHILD = NULL) then
36. parent --> LCHILD = ptr --> RCHILD
37. Else
38. parent --> LCHILD = ptr --> LCHILD
39. EndIf
40. Else
41. If(parent --> RCHILD = ptr) then
42. If(ptr --> LCHILD = NULL) then
43. parent --> RCHILD = ptr --> RCHILD
44. Else
45. parent --> RCHILD = ptr --> LCHILD
46. EndIf
47. EndIf
48. EndIf
49. ReturnNode(ptr)
50. EndIf
51. If ( case =3 )
52. ptr1=SUCC(ptr)
53. item1 = ptr1 --> DATA
54. Delete_BST(item1)
55. ptr-->DATA = item1
56. EndIf
57. Stop

• Algorithm SUCC
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 binary tree.
Steps –
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

You might also like