16 BST
16 BST
Sungmin Cha
New York University
11.04.2024
Outline
• Notice
– HW3 – Part 1 – Heap
2
Outline
• Notice
– HW3 – Part 1 – Heap
3
Outline
• Notice
– HW3 – Part 1 – Heap
4
Index and Record
• Index and record
– The record contains all the information about the entity
– An index is used to retrieve a record of an entity
Search key: An index that uniquely distinguishes each record without
duplication
5
Binary Search Tree (BST)
• The properties of a BST
1. Each node holds a single key value
2. All key values in each node are unique
3. The key value of any given node is greater than the key values
of all nodes in its left subtree and smaller than the key values of
all nodes in its right subtree
30
Left subtree right subtree
20 40
10 25 35 45
6
Binary Search Tree (BST)
• ADT of BST
7
Searching Operation of BST
• Implementation of search(x) function
– Example: search(25)
1. starting from the root node
30
20 40
10 25 35 45
8
Searching Operation of BST
• Implementation of search(x) function
– Example: search(25)
2. Compare the key value of the root node with the given key value
25 < 30
30
20 40
10 25 35 45
9
Searching Operation of BST
• Implementation of search(x) function
– Example: search(25)
3. Move to the right or left subtree
30
20 40
10 25 35 45
10
Searching Operation of BST
• Implementation of search(x) function
– Example: search(25)
4. Compare the key value of the current node with the given key value
30
25 > 20 20 40
10 25 35 45
11
Searching Operation of BST
• Implementation of search(x) function
– Example: search(25)
5. Move to the right or left subtree
30
20 40
10 25 35 45
12
Searching Operation of BST
• Implementation of search(x) function
– Example: search(25)
6. Compare the key value of the current node with the given key value
30
20 40
10 25 35 45
25 == 25
13
Searching Operation of BST
• Implementation of search(x) function
– Example: search(25)
7. Return a node or null
30
20 40
10 25 35 45
Return!
14
Searching Operation of BST
• Pseudo algorithm of search(x) function
– Implementation using a recursion function
t
x < t.item x > t.item
t.left t.right
The keys in the left subtree of the t node are lower than the t node’s key
The keys in the right subtree of the t node are greater than the t node’s key
15
Insertion Operation of BST
• Implementation of insert(x) function
– Insert a node with key x to a BST
– Remind one of the properties of BST
All key values in each node are unique
16
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
30
20 40
25
17
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
30 root
20 40
25
18
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
10 < 30
30 root
20 40
25
19
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
10 < 30
30
20 t.left 40
25
20
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
30
20 t 40
25
21
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
30
10 < 20 20 t 40
25
22
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
30
10 < 20 20 40
25
t.left (null)
23
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
30
20 40
25
t (null)
24
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
30
20 40
25
t (null)
25
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
30
20 40
10 25
t (null)
26
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
30
20 40
10 25
t (null)
27
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
30
20 40
10 25
Return r (new node)
30
20 Return t 40
10 25
30
20 t.left 40
10 25
Return t
30
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
30 t
20 t.left 40
10 25
31
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?
Return t (root)
30 t
20 40
10 25
32
Outline
• Notice
– HW3 – Part 1 – Heap
33
Delete Operation of BST
• Difference between the insert and delete operations
– The insertion occurs at a leaf node
– However, the deletion can occur throughout the entire tree
34
Delete Operation of BST
• Case 1: r is a leaf node (does not have a child node)
55
15 60
8 28 90
3 18 30
r 48
38 50
33
32 36
35
Delete Operation of BST
• Case 1: r is a leaf node (does not have a child node)
– Simply discard r
55
15 60
8 28 90
3 30
48
38 50
33
32 36
36
Delete Operation of BST
• Case 2: r has one child node
55
15 60
8 28 90
3 18 30
r
48
38 50
33
32 36
37
Delete Operation of BST
• Case 2: r has one child node
– 1) discard r How should the connection be made
to ensure that the entire tree satisfies
the properties of BST?
55
15 60
8 28 90
3 18
48
33
32 36
38
Delete Operation of BST
• Case 2: r has one child node
– 1) discard r
55
15 60
8 28 90
3 18 30
48
32 36
39
Delete Operation of BST
• Case 2: r has one child node
– 1) discard r
55
15 60
8 28 90
3 18
48
32 36
40
Delete Operation of BST
• Case 2: r has one child node
– 2) Make the child node of r become a child node of r’s parent
node
55
15 60
8 28 90
3 18 48
The modified tree now satisfies
38 50
the properties of BST!
33
32 36
41
Delete Operation of BST
• Case 3: r has two child nodes
55
15 60
r
8 28 90
3 18 45
41 48
30 50
38
33
32 36
42
Delete Operation of BST
• Case 3: r has two child nodes
– 1) discard r
55
15 60
8 90
3 18 45
41 48
30 50
38
How should the connection be made
to ensure that the entire tree satisfies
33
the properties of BST?
32 36
43
Delete Operation of BST
• Case 3: r has two child nodes
– 1) discard r
55
15 60
8 90
3 18 45
Can we apply the
41 48 solution in Case 2?
A conflict of nodes
arises here 30 50
38
33
32 36
44
Delete Operation of BST
• Case 3: r has two child nodes
– 1) discard r
55
15 60
8 90
3 18 45
41 48
30 50
38
15 60
8 90
3 18 45
41 48
30 50
38
33
32 36
subtree 46
Delete Operation of BST
• Case 3: r has two child nodes
– 3) Move node s to the position of node r
55
15 60
8 30 90
3 18 45
41 48
50
38
33
32 36
subtree 47
Delete Operation of BST
• Case 3: r has two child nodes
– 3) Move node s to the position of node r
55
15 60
8 30 90
3 18 45
subtree
41 48
50
38
The structure near new node r
33 satisfies the properties
32 36
48
Delete Operation of BST
• Case 3: r has two child nodes
– 3) Move node s to the position of node r
55
15 60
8 30 90
3 18 45
The key value (41) is greater
48
than the key values of all nodes
41
in the right subtree of node s
50
This is identical to the
The connection 38
situation in Case 2!
has been severed
33
32 36
subtree 49
Delete Operation of BST
• Case 3: r has two child nodes
– 4) Make the child node of the original node s become a child
node of its parent node 55
15 60
8 30 90
3 18 45
41 48
38 50
33
32 36
subtree
50
Delete Operation of BST
• Case 3: r has two child nodes
– 4) Make the child node of the original node s become a child
node of its parent node 55
15 60
8 30 90
3 18 45
33
32 36
subtree
51
Delete Operation of BST
• Pseudo code of delete operation
– Conceptual pseudo code
52
Delete Operation of BST
• Pseudo code of delete operation
– Using a recursive function
53
Delete Operation of BST
• Pseudo code of delete operation
– Using a recursive function
54
Delete Operation of BST
• Pseudo code of delete operation
– Using a recursive function
t <- null
t <- t.right
t <- t.left
t <- new t
55
Delete Operation of BST
• Pseudo code of delete operation
– Using a recursive function
t <- null
t <- t.right
t <- t.left
t <- new t
57
Delete Operation of BST
• Time complexity of the delete operation
– Conceptually thinking,
1) Time complexity of searching node x in Case 1, 2 and 3
: the best and average case (O(log n)), worst case (O(n))
2) Time complexity of deleting a node
: Case 1 and 2 - constant time (simple assignment operations),
Case 3 – Time of searching the minimum node (O(log n) or O(n))
+ constant time
3) Time complexity of moving back to the root node
: O(logn) or O(n)
20 40
10 25 35 45
60
Traversal
• Preorder traversal
– A traversal algorithm that visits nodes in the following order
: 1) root, 2) left subtree, 3) right subtree
2 5
3 4
61
Traversal
• Inorder traversal
– A traversal algorithm that visits nodes in the following order
: 1) left subtree, 2) root, 3) right subtree
2 5
1 3
62
Traversal
• Postorder traversal
– A traversal algorithm that visits nodes in the following order
: 1) left subtree, 2) right subtree, 3) root
3 4
1 2
63
Concluding Remarks
• Delete operation of BST
• Traversal of BST
64
Thank you!
E-mail: [email protected]
65