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

Lec-15 Binary Search Tree

A binary search tree (BST) is a hierarchical, nonlinear data structure where each node has a value and a total order is defined on the node values. The left subtree of a node contains values less than the node's value, and the right subtree contains values greater than or equal to the node's value. Operations like searching, insertion, deletion, and traversal can be performed on a BST in O(log n) time on average, where n is the number of nodes, but may require O(n) time in the worst case for unbalanced trees. A BST can also be used to sort a list of items by inserting all items into the tree, then traversing it in order to output the sorted list.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Lec-15 Binary Search Tree

A binary search tree (BST) is a hierarchical, nonlinear data structure where each node has a value and a total order is defined on the node values. The left subtree of a node contains values less than the node's value, and the right subtree contains values greater than or equal to the node's value. Operations like searching, insertion, deletion, and traversal can be performed on a BST in O(log n) time on average, where n is the number of nodes, but may require O(n) time in the worst case for unbalanced trees. A BST can also be used to sort a list of items by inserting all items into the tree, then traversing it in order to output the sorted list.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Binary Search Tree

1
Binary Search Tree (BST)
- It is a hierarchical, nonlinear data structure.
- Its properties are given bellow:
- Each node has a value.
- A total order is defined on these node values.
- Left subtree of a node contains the values less than the node’s value.
- Right subtree of a node contains the values greater than or equal to
the node’s value.
Example: 8
Root R
3 12

Left Subtree
1 6 9 14

Right Subtree
2 10

2
Searching a node with a specific value from BST Rules
• Examine the root R.
• If the searching value equals the root, then the value exists in the
tree.
• If it is less than the root, then it must be in the left subtree.
• If it is greater than the root, then it must be in the right subtree.
• This process continues until the searching value is found or a leaf
node is reached without having that value where it would be.
Algorithm Here,
Root = Root of BST
• Search_BST(Root, Left, Right, Key )
Left = Left Subtree
1. Loc := Search(Root, Key, NULL)
Right = Right Subtree
2. If Loc = NULL then Print: “Item not in BST”.
Key = Searching Item
3. Else Print: “Item is in BST”.
4. End.
3
• Search(Node, Key, Par)
1. If Node = NULL then return NULL.
2. If Key < Node.Key then Search(Node.Left, Key, Node).
3. Else if Key > Node.Key then Search(Node.Right, Key, Node).
4. Else return Node
Example:
Searching Item: 12
AA
15 Steps:

BB CC
1. Search(Root, 12) Search(AA,12)
13 100 2. Search(AA->Left,12) Search(BB, 12)
DD EE
3. Search(BB->Left, 12) Search(DD, 12)
12 90 4. Return DD and Print: Item is in BST.

4
Complexity of Searching a Node from BST

• In Binary Search Tree, Depth = log2N

• For balanced BST, Complexity of Searching=O(log2N) [Average Case]

• For unbalanced BST, Complexity of Searching=O(N) [Worst Case]

5
Inserting a Node into a BST
Rules
• Examine the root R.
• If the new value is less than the root’s, then it must be inserted at the left subtree.
• Otherwise it must be inserted at the right subtree.
• This process continues until the new value is compared with a leaf node and then it is
added as that node’s (leaf node) left or right child depending on its value.

Algorithm
Here,
• Insert_BST(Root, Left, Right, Item)
Root = Root of BST
1. Set NewNode.Value := Item.
Left = Left Subtree
2. Insert(Root, NewNode).
Right = Right Subtree
End.
Item = New Item to be inserted

6
Insert(Node, NewNode)
1. If Node = NULL then Node := NewNode and return.

2. If NewNode.Value < Node.Value then Insert(Node.Left, NewNode).

3. Else Insert(Node.Right, NewNode).

Example:
New Item: 110
AA
15
FF.Value=110 (FF is the New Node address)

BB CC Steps:
13 100 1. Insert(Root, FF) Insert(AA, FF)

DD 2. Insert(AA->Right, FF) Insert(CC, FF)


EE FF

12 90 110 3. Insert(CC->Right, FF)

Figure: BST with 5 Nodes

7
Complexity of Inserting a Node into BST

• In Binary Search Tree, Depth = log2N

• Complexity of Searching = O(log2N)

• Insertion of a Node =1

• Complexity of an insertion into BST = O(log2N) [Average Case]

• Complexity of an insertion into BST = O(N) [Worst Case]

8
Deleting a Node from BST
Rules
- A node N has no children. N is deleted from BST by simply replacing the
location of N in the parent node P(N) by the NULL pointer.
- A node N has one child. N is deleted from BST by simply replacing the location
of N in the parent node P(N) by the location of the only child of N.
- A node N has two children. Let S(N) denote the inorder successor of N. N is
deleted from BST by first deleting S(N) from BST and then replacing node N
by S(N).
Algorithm
Delete_BST(Root, Left, Right, Key)
1. Set Loc := Search_BST(Root, Left, Right, Key)
2. If Loc = NULL then Print: “Item not in BST” and return
3. If Loc.Left ≠ NULL and Loc.Right, Delete_A(Root, Left, Right, Loc)
4. Else Delete_B(Root, Left, Right, Loc)
5. End.
9
Delete_B(Root, Left, Right, Loc, Par)

1. If Loc.Left = NULL and Loc.Right = NULL then Set Child := NULL

2. Else If Loc.Left ≠ NULL then Set Child := Loc.Left

3. Else Set Child := Loc.Right.

4. If Par ≠ NULL then

5. If Loc = Par.Left then Set Par.Left := Child

6. Else Par.Right := Child

7. Else Set Root := Child

8. Return

10
1.Delete_A(Root, Left, Right, Loc, Par)

1. Set PTR := Loc.Right and Save := Loc

2. While PTR.Left ≠ NULL Set Save := PTR and PTR := PTR.Left

3. Set Suc := PTR and ParSuc := Save

4. Delete_B(Root, Left, Right, Suc, ParSuc)

5. If Par ≠ NULL then

6. If Loc = Par.Left then Set Par.Left := Suc

7. Else Set Par.Right := Suc.

8. Else Root := Suc

9. Set Suc.Left := Loc.Left and Suc.Right := Loc.Right

10. Return.
11
Example:
Deleted Item: 15
AA Loc = AA and Par = NULL
15
Steps:
BB CC
1. Delete_A(Root, Loc, Par)
13 100
2. Inorder Successor, S(Loc) = EE
DD EE FF 3. Delete EE from BST
12 90 110 4. Replace AA by EE by EE.Left = AA.Left and
EE.Right := AA.Right.
Figure: Given BST
EE
90

BB CC
13 100
After Delete Operation DD Figure: Resulting BST
FF
12 110

12
Complexity of Deleting a Node into BST

• In Binary Search Tree, Depth = log2N

• No. of Nodes =N

• Complexity of Searching = O(log2N)

• If it is needed to find inorder successor, then it also takes O(log 2N).

• Deletion of a Node =C [C=Constant]

• Complexity of a Deletion from BST = O(log2N) [Average Case]

• Complexity of a Deletion from BST = O(N) [Worst Case]

13
Traversal of BST

- Once the binary search tree has been created, its elements can be traversed in 3 ways.

(1) Inorder (3) Postorder

Traverse_InOrder(TreeNode) Traverse_PostOrder(TreeNode)
1.If TreeNode is NULL, then Return 1.If TreeNode is NULL, then Return
2.Traverse_Inorder(TreeNode.Left) 2.Traverse_Inorder(TreeNode.Left)
3.Visit(TreeNode) 3.Traverse_Inorder(TreeNode.Right)
4.Traverse_Inorder(TreeNode.Right) 4.Visit(TreeNode)
(2) Preorder

Traverse_PreOrder(TreeNode)
1.If TreeNode is NULL, then Return
2.Visit(TreeNode)
3.Traverse_PreOrder(TreeNode.Left)
4.Traverse_PreOrder(TreeNode.Right)

14
Example:

AA
15
Inorder Traversal: 12, 13, 15, 90, 100, 110
BB CC
Preorder Traversal : 15, 13, 12, 100, 90, 110
13 100
Postorder Traversal : 12, 13, 90, 110, 100, 15
DD EE FF

12 90 110

Figure: Binary Search Tree

Complexity of Traversing a BST

Since every node of BST is visited, so the complexity of traversing a BST is O(N), where
N is the no. of nodes in BST.

15
Sorting of A List of Items Using BST
• A binary search tree can be used to implement a simple but inefficient sorting algorithm.

• Two steps are followed for sorting:

- TreeNode := Create_BST(List of Items)

- Traverse_InOrder(TreeNode)

• Example:
List of Items: 50, 10, 5, 100, 120, 90, 20
BST: 50

10 100

5 20 90 120

Inorder Traverse: 5, 10, 20, 50, 90, 100, 120 [Sorted List]
16
END!!!

17

You might also like