0% found this document useful (0 votes)
14 views65 pages

16 BST

The lecture covers binary search trees (BST), focusing on operations such as search, insert, and delete. It details the properties of BSTs, the implementation of search and insert functions, and the different cases for deleting nodes. The document includes examples and pseudo algorithms to illustrate these concepts.

Uploaded by

thirtythr33spam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views65 pages

16 BST

The lecture covers binary search trees (BST), focusing on operations such as search, insert, and delete. It details the properties of BSTs, the implementation of search and insert functions, and the different cases for deleting nodes. The document includes examples and pseudo algorithms to illustrate these concepts.

Uploaded by

thirtythr33spam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

Data Structures

<Lecture 16: BST 2>

Sungmin Cha
New York University

11.04.2024
Outline
• Notice
– HW3 – Part 1 – Heap

• Review the previous lecture

• Binary search tree


– Delete operation
– Traversal

2
Outline
• Notice
– HW3 – Part 1 – Heap

• Review the previous lecture

• Binary search tree


– Delete operation
– Traversal

3
Outline
• Notice
– HW3 – Part 1 – Heap

• Review the previous lecture

• Binary search tree


– Delete operation
– Traversal

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

– Example of index, record and key

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

– Accessing a BST begins at the root node

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

– The procedure of insert(x) function


1. Perform search(x) starting from the root node
o This is exactly the same as the search() operation
2. Once reach a leaf node, add x as a child of that leaf node

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

Make a new node!

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)

t.left = new node


28
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?

30

20 Return t 40

10 25

t.left = new node


29
Searching Insert of BST
• Pseudo algorithm of insert(x) function
– Implementation using a recursion function
insert(10)?

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

• Review the previous lecture

• Binary search tree


– Delete operation
– Traversal

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

• Three cases of the delete operation (r: a node to delete)


– Case 1: r is a leaf node (does not have a child node)
– Case 2: r has one child node
– Case 3: r has two child nodes

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

The connection has been severed 38 50

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

The key value (30) of node r is 30 << keys in subtree


38 50
smaller than the key values of
all nodes in the right subtree 33

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

28 << keys in subtree


This also holds true for 38 50
the parent node of node r!
33

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

To satisfy the properties of BST,


33
it is necessary to preserve the
structure around node r
32 36
45
Delete Operation of BST
• Case 3: r has two child nodes
– 2) find the minimum element node s in the right subtree
55

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

41 48 The subtree satisfies the


properties of BST!
38 50

33

32 36
subtree

51
Delete Operation of BST
• Pseudo code of delete operation
– Conceptual pseudo code

 This can be implemented using either a non-recursive or a recursive


function
 However, in most tree-related algorithms, implementing it recursively
tends to be more elegant!

52
Delete Operation of BST
• Pseudo code of delete operation
– Using a recursive function

Recursively call the


delete function until
the node x is found

53
Delete Operation of BST
• Pseudo code of delete operation
– Using a recursive function

The node x is found


and the deleteNode
function is called

Recursively call the


delete function until
the node x is found

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

For Case 3, the deleteMinItem


function is called! 56
Delete Operation of BST
• Pseudo code of delete operation
– Using a recursive function

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)

– In conclusion, the time complexity of the delete operation is


 Best and average cases: O(log n)
 Worst case: O(n)
58
Advantage of BST
• The advantage of BST
– In the case of a well-balanced BST
However, does a BST always
30 maintain this structure?

20 40

10 25 35 45

– The time complexity of insert(), delete() and search() operations


is O(log n)
59
Traversal
• There are some cases where it is necessary to access all
the nodes in a BST

• In such cases, we can use Traversal algorithms


– Preorder traversal
– Inorder traversal
– Postorder traversal

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

You might also like