0% found this document useful (0 votes)
9 views28 pages

7 BST

The document provides an overview of Binary Search Trees (BST), detailing their structure, properties, and key operations such as search, insertion, and deletion. It explains the algorithms for searching and inserting nodes, as well as the different cases for deleting nodes, including handling nodes with zero, one, or two children. The document emphasizes maintaining the BST properties during these operations to ensure efficient data processing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views28 pages

7 BST

The document provides an overview of Binary Search Trees (BST), detailing their structure, properties, and key operations such as search, insertion, and deletion. It explains the algorithms for searching and inserting nodes, as well as the different cases for deleting nodes, including handling nodes with zero, one, or two children. The document emphasizes maintaining the BST properties during these operations to ensure efficient data processing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

BST

CSC-114 Data Structure and Algorithms


Outline
 Search Trees
 Binary Search Tree
 Search
 Insertion
 Deletion
 Constructing with Traversal Orders

2 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Binary Search Tree
 A very popular search tree which supports efficient processing of data. It stores data in a
manner which allows faster lookup.
 A Binary Search Tree is a binary tree with a special property:
 Each node has some comparable data field(key) and it fulfills following rule:
 Node’s left sub tree holds keys less than the node's key,
 Node’s right sub tree holds keys greater than the node's key. 50
 Note: No duplicate keys are allowed
 BSTs are used to present sorted data: 25 70
 If you traverse tree in order, it will produce sorted keys:
 10 13 25 38 50 70 77 90 99 10 38 90
 Operations:
 Search 13 77 99
 Insertion
 Deletion
3 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025
Search
 Search 77 Search 97
50 77>50 50 97>50

25 77>70 25 97>70
70 70

90 77<90 90 97>90
10 38 10 38

13 77 99 13 77 99 97<90

77=77
null
 How much time will it take?
 What would be the worst case scenario?
Grey node is shown here to present null
node, just for understanding

4 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Search: Algorithm
 BST_SEARCH(node, key)
 Input: root node of tree, key to be searched
 Output: node which contains key
 Steps:

Start
1. If node== null OR key==node.key // value found or not found
2. Return node
3. Else If key < node.key // < node.key
4. Return BST_SEARCH( node.left, key)
5. Else // > node.key
6. Return BST_SEARCH( node.right, key)
End
5 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025
Insertion
 Let say we want to insert 5
 What is the correct position for it

 5<10 5<50 50
 Move to left
 Left child of 10 is null 5<25 25 70
 Make 5 it’s left child
5<10 10 38 90

13 77 99
null

6 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Insertion
 Insert 5
5<50 50 50

5<25 25 25
70 70

5<10 10 38 90 10 38 90

13 77 99 5 13 77 99
null

7 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Insertion
 Insert 85

50 85>50 50

25 70 85>70 25 70

10 38 90 85<90 10 38 90
85>77
5 13 77 99 5 13 77 99

85
null

8 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Insert: Algorithm
 Have you observed?
 New node is always inserted as leaf node
 So
 If root is null
 New node is root
 And if root node is not null
 Some other node’s links will be updated
 If new node is smaller, update left child
 Else update right child
 How to write a recursive function?
 What it should receive?
 What it should return?

9 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Insert: Algorithm
 BST_INSERT(node, newNode)
 Input: root node of tree, node to be inserted
 Output: updated tree with newNode
 Steps:
1. If node == null //base case
2. node = newNode
3. Else If newNode.key < node.key
4. node.left = BST_INSERT(node.left, newNode)
5. Else If newNode.key > node.key
6. node.right = BST_INSERT(node.right, newNode)
7. End If
8. Return node
10 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025
Deletion
 After deletion of any node, Tree must remains BST
 Let say:
50
 We want to delete 5
 We want to delete 70 25 70
 We want to delete 25
 What is difference between these nodes? 10 38 90

5 13 77 99

85

11 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Deletion
 There are three cases when deleting a node:
 Node is leaf node
 Simply delete 50
 Node has only one child
25 70
 Link parent of node to child of node
 Node has two child 10 38 90
 Complex case
5 13 77 99

85

12 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Deletion
 Case 1: If node is leaf node
 Unlink from parent
50 50

25 70 25 70

10 38 90 10 38 90

5 13 77 99 5 13 77 99

85
null

13 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Deletion
 Case 2: If node has one child
 Link parent of node to child of node
50 50

25 70 25

10 38 90 10 38 90

5 13 77 99 5 13 77 99

85 85

14 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Deletion
 Case 3: If Node has two child
 Consider node 25 in following modified tree
 50
Left link of 50 needs to be updated
 But with which node?
25 70
 BST order property must be maintained.
 What about 5? Order will be disturbed
10 38 90
 What about 10? It has two children, where will 38 go?
 38 is more appropriate choice?
5 13 77 99
 What about 13?

85

15 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Deletion
 Case 3: If Node has two child
 Delete 30
 Which node will become left child of 65
 13? Order will be disturbed
 15? Better choice is 17
 No order disturbance, leaf node
 43? Order will be disturbed
 36? yes

 Have you observed the pattern for choosing node?


 Chosen node has single child – simple case
 Or it is leaf node – simple case
 If we choose node from left sub-tree
 It will not have a right child and it is largest in left-sub tree
 If we choose from right sub-tree
 It will not have left-child and it is smallest in right sub-tree
 See the next slide
16 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025
Deletion
 Case 3: If Node has two child
 See the tree with few changes
 Now is it good to choose 17?
 Order will be disturbed?
 So now choose 19
 Same is the case with 36
 Now 33 is choice

 Actually if you do the in-order traversal of tree


 10, 13, 15, 17, 18, 19 , 30 , 33 , 36, 39, 43, 65, 71, 83, 91
 Then your chosen node is either predecessor or successor of deleted node
 In-order Successor of a node is a node which comes after the node in in-order traversal.
 In-order predecessor of a node is a node which comes before the node in in-order traversal.

17 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Deletion
 Case 3: If Node has two child
1. Either
 Find in-order successor of node
 Replace contents of node with successor node
 Delete successor
2. OR
 Find in-order predecessor of node
 Replace contents of node with predecessor node
 Delete predecessor

 Next slides discuss the first approach.

18 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Deletion
 Case 3: If Node has two child

50 50
50

25 70 38 70 38 70

10 38 90 10 38 90
10 90

5 13 77 99 5 13 77 99 5 13 77 99

85 85
85

Grey node is shown here to present null


node, just for understanding

19 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Deletion
 Case 3: If Node has two child

30 36

36 39

20 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Deletion
 Find in-order successor of node?
INORDER_SUCCESSOR(node)
Set curr= node.right 50

While(curr.left!=null)
25 70
curr=curr.left
End While
10 38 90
Return curr

5 13 77 99

For 25 it will return 38


85

21 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


 BST_DELETE(node, key) getChildNode(node)
 Input: root node of tree, key to be deleted
1. If hasLeft(node)
 Output: updated tree without node that contains key
 Steps:
2. return node.left
1. If(node!=null) 3. Else If hasRight(node)
2. If key ==node.key 4. return node.right
3. If hasLeft(node) and hasRight(node) //case 3 5. Else
4. successor= INORDER_SUCCESSOR(node)
6. Return NULL// leaf node case
5. copy(node,successor)
6. node.right= BST_DELETE(node.right, successor.key ) 7. End if
7. Else //case1, case 2, you can break it in 2 if’s copy(node,successor)
8. node=getChildNode(node);
8. node.key=successor.key
9. End If
9. Else If key < node.key
9. Copy other data attributes if present but
not child links
10. node.left =BST_DELETE(node.left,key)
11. Else // key > node.key
12. node.right =BST_DELETE(node.right,key)
13. End If
14. return node // updated node
15. End If
16. return node // null

22 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Deletion
 Case 3: If Node has two child

30 36

36 39

23 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Explanation
 node.right= BST_DELETE(node.right, successor.key )
 Why not pass successor along with successor.key?

50 65 So rather than
jumping to successor
node, start from
25 70 25 most immediate
right node, and
10 38 65 90 10 38 systematically delete
successor node
5 13 77 99 5 13

85

 Reason: when function is called with successor as root, it store child of successor to right of current node,
which in this case is null, and sub-tree starting at 70 is removed
24 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025
Deletion
 Case 3: If Node has two child
 Even though it does not affect BST property if you choose successor or predecessor,
but it is good to choose randomly between them to maintain tree balance
 Otherwise.
 Tree will become skewed. One side will become extremely short than other

25 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


BST Time Complexity
 Depends upon height
 Big-Ohh

Best Average Worst

 h=log n h=almost log n h=n


26 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025
Tree Building with Traversal Orders
 Can we build a binary search tree if we are given only one traversal order?
 Pre-Order: 50, 25, 10, 5, 13, 38, 70, 90, 77, 85, 99
 50
Post-Order: 5, 13, 10, 38, 25, 85, 77, 99, 90, 70, 50
 In-Order: 5, 10, 13, 25, 38, 50, 70, 77, 85, 90, 99 25 70

10 38 90

5 13 77 99

85

27 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025


Tree Building with Traversal Orders
 Pre-Order: 50, 25, 10, 5, 13, 38, 70, 90, 77, 85, 99
1. Root node?
 First node
2. Left nodes?
50
 Nodes between root and next maximum
3. Right nodes? {5, 10, 13, 25, 38} {70, 77, 85, 90, 99}
 Nodes starting from that maximum
 Repeat
 For Post-Order, do the opposite.
 What about In-Order?
 Which node is root node?

28 Saba Anwar, Computer Science Department- CIIT Lahore 21/03/2025

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy