0% found this document useful (0 votes)
17 views88 pages

2024 2 Binary Search Trees

Uploaded by

hyobin20041107
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)
17 views88 pages

2024 2 Binary Search Trees

Uploaded by

hyobin20041107
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/ 88

Data Structures:

Binary Search Trees, Height-Balanced Trees,


AVL Tree

Won Kim
(Lecture by HyoungSun Choi)
Spring 2024
Binary Search

2
Binary Search
 Prerequisite
 An ordered list
 O(log2 n)

3
Algorithm
 Take the midpoint of the sorted list.
 If the search key matches the key at the midpoint,
search ends in success.
 If the list contains only one key and the search key
does not match it, search ends in failure.
 If the search key is < the key at the midpoint, take
the list to the left of the midpoint, and repeat from
the start.
 If the search key is > the key at the midpoint, take
the list to the right of the midpoint, and repeat from
the start.

4
Binary Search: Examples

7 9 2 5 15 3 14 1 8 11 12 4 13 6 10

5
Binary Search: Example

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
search for 13

6
Binary Search: Solution
take the midpoint of the list and compare with 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
take the list after 8
9 10 11 12 13 14 15
take the midpoint of the list and compare with 13

9 10 11 12 13 14 15
take the list after 12
13 14 15

7
Binary Search: Solution (cont.)

take the list after 12


13 14 15
take the midpoint of the list and compare with 13

13 14 15
take the list before 14
13
take the midpoint of the list and compare with 13

13

8
Binary Search: Examples

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

search for 5

1 3 4 5 7 8 9 10 12 13 14 16 20 21 25

search for 4
search for 22

9
Binary Search: Examples
int binarySearch(int arr[], int size, int target) {
1 2 3 4 5 int low = 0;
int high = size - 1;

while (low <= high) {


int mid = low + (high - low) / 2;
search for 3, 1, 5, 9 // target<mid
if (arr[mid] > target) {
high = mid - 1;
}
// target>mid
else if (arr[mid] < target) {
low = mid + 1;
1 2 3 4 }

else {
return mid;
}
search for 1, 3, 2, 4, 9 }

// if target is not in the list


return -1;
}

10
Binary Search Trees

11
Binary Tree (no order)

150

490 100

50 90 160 75

120 12
Binary Search Tree (ordered)

100

75 150

50 90 120 490

160 13
Summary Definition of a Tree
 A tree is a linked list of data, where a data
item is linked to other data items by a cer-
tain relationship.
 A search for a data item on a tree proceeds
from one data item to another data item
that satisfies a certain relationship.
 The hierarchy of a tree is a convenient visu-
alization of the relationships among the data
items.

14
Properties
 Each Node Has a Unique Key (Data)
 The Key > The Key of Any Node in the Left
Subtree
 The Key < The Key of Any Node in the
Right Subtree

Smaller to the Left, Larger to the Right

15
Searching a Binary Search Tree
 Compare the Key to Be Searched with the
Key of the Root Node.
 If the Search Key = the Root Key, Success.
 If the Search Key < the Root Key, Search the
Left Subtree.
 If the Search Key > the Root Key, Search the
Right Subtree.
 If No Match, Failure.

16
Example search 120, search 400

100

75 150

50 90 120 490

160 17
Caution (1/2): complex “key”, large num-
ber of nodes
 In Textbooks
 “key”: a small number or short string
 e.g., 250, “Hong Gil Dong”
 “number of nodes”: 5-15
 “number of levels”: 3-5
 In Real Software
 “key”: a structure, array, array of structures
 e.g., [xxxxxxxxx, “Hong Gil Dong”, 3xxxxx,
“Suwon”]
 “number of nodes”: 100, 1000, 10000, 1000000,

18
Caution (2/2): large number of levels
300 level 1

175 350 level 2

213 345 level 128


19
Inserting into a Binary Search Tree
 Search the Key to Be Inserted.
 If the Search Fails, Insert the Key Where the
Search Failed.

20
Example insert 80

100

75 150

50 90 120 490

160 21
Example: Result

100

75 150

50 90 120 490

80 160 22
Deleting from a Binary Search Tree
 Search the Key to Be Deleted.
 If the Search Fails, No Deletion.
 If the Search Succeeds
 If the key is a leaf node, just delete it.
 If the key is an interior node with one child node,
delete the key, and simply promote the child
node.
 If the key is an interior node with two child
nodes, delete the key, and
 Promote the largest node in its left subtree or
 Promote the smallest node in its right subtree.
23
Example (1/3) delete 50

100

75 150

50 90 120 490
(leaf node)

160 24
Example (1/3): Result

100

75 150

90 120 490

160 25
Example (2/3) delete 75

100

75 150
(interior node
with one child
node)
90 120 490

160 26
Example (2/3): Result

100

90 150

120 490

160 27
Example (3/3) delete 150

100
(interior node with
two child nodes)

90 150

120 490

option 1:
promote the largest
160
node on the left subtree 28
Example (3/3): Result (1/2)

100

90 120

490

160 29
Example (3/3) delete 150

100
(interior node with
two child nodes)

90 150

120 490

option 2:
promote the smallest
160
node on the right subtree 30
Example (3/3): Result (2/2)

100

90 160

120 490

31
Exercise: Search for 330 – At Which Node Does
the Search Fail?

300

175 350

50 290 320 500

213 345 32
Exercise: Insert 330

300

175 350

50 290 320 500

213 345
330 33
Exercise: Delete 300

300

175 350

50 290 320 500

213 345 34
Exercise: Delete 100

100

75 150

50 120 490

35
Solution option 1: replace the key with the largest key on the
left subtree

100

75 150

50 120 490
75

delete 75 75 150

50 120 490

36
Solution option 2: replace the key with the smallest key on
the right subtree

100

75 150

50 120 490 120

75 150

50 120 490

delete 120
37
Performance of Binary Search Trees
 Search Time
 Insertion Time
 Deletion Time
Degenerate(Skewed) A Full or Complete Binary Tree
Binary Tree

38
Big O (Big Oh, Landau, Asymptotic) Notation
(1/3)

 (e.g.) T(n) = 2n2 + 4n + 120


 As n grows, the n2 term dominates
 T(n) = O(n2)
 “Big O of n2” = O(n2)
 “Order of n2” time complexity”

 Think of “Big” n (1,000; 1,000,000;…)

39
Big O (Big Oh, Landau, Asymptotic) Notation
(2/3)

 O(1) : (constant and small) finding a match


right away in a search, deletion of the largest
key from a max heap
 O(log2 n): (logarithmic) search of a height-bal-
anced binary search tree, binary search of a
sorted list
 O(n) : search of a binary search tree
 O(n) : (linear) search of an array, queue, linked
list
40
Big O (Big Oh, Landau, Asymptotic) Notation
(3/3)

 O(n log2 n) : (loglinear) heap sort


 O(n2) : (quadratic) insertion sort

 O(n!) : (factorial) traveling salesman problem


 2O(n) : (exponential) general traveling salesman
problem

41
Source of the Performance Problem of Binary
Search Trees

 Height of the Binary Search Tree


 The average case: log2n = h, where n is the total
number of nodes
 The worst case: n
 Need to Maintain the Search Tree Height-
Balanced.
 AVL tree, red-black tree, AA tree,…
 Search, insertion, deletion each takes O(log2n)
 Overhead for insertion and deletion
42
A Height-Balanced Tree

43
Height-Balanced Tree
 AVL Tree, (Red-Black Tree)
 (2-3 Tree, T-Tree, B-Tree, B+ Tree)

44
AVL Tree
 Invented by G.M. Adelson-Velskii and E.M.
Landis (1962)
 A Height-Balanced Binary Search Tree
 “not perfectly balanced”
 Balance Factor of Each Node Must Be 0, +1,
or -1.
 (Balance Factor = hL – hR, where hL is the height
of left subtree and hR is the height of the right
subtree.)

45
An AVL Tree
bf=2-3=-1
250

bf=1-0=1 bf=1-2=-1
175 374

bf=1-1=0
123 297 450

422 501

46
Not an AVL Tree
bf=2-4=-2
250

bf=1-0=1 bf=1-3=-2
175 374

bf=2-1=1
123 297 450

422 bf=0-1=-1 501

436 47
AVL Tree Rotations
 If the Balance Factor of a Node Becomes 2
or -2, Upon Insertion or Deletion of a Node
 The tree must be re-balanced by performing
“tree rotations” around the node whose balance
factor becomes 2 or -2.
 Two Objectives
 Maintain AVL tree height balanced.
 Maintain AVL tree as a binary search tree.

48
AVL Tree Rotations
 4 Types of Rotations
 form a chain of 3 nodes down from the node for
a single rotation (right or left), or
 form a chain of 3 or 4 nodes down from the node
for a double rotation (right and left, or left and
right)

49
AVL Tree Rotations (1/4)

LL: Single Rotation (Right)

bf=2
250
L
bf=1 bf=0
175 175
L
bf=0 bf=0 bf=0
123 123 250

50
Implementing the LL Rotation
bf=2
250
L
bf=1 bf=0
175 175
L
bf=0 bf=0 bf=0
123 123 250

change node 175’s right child pointer from NULL to node 250
change the left child pointer of the parent of node 250
from node 250 to node 175
change the balance factor in the affected nodes
51
Implementing the LL Rotation

bf=2
250
175

bf=0
175
123 250

bf=0
123 200
200

52
Exercise: Re-Balance the AVL Tree

300

bf=2
bf=1 250
195 475

bf=1
195
133 250 700

bf=0 bf=0
133

53
AVL Tree Rotations (2/4)

RR: Single Rotation (Left)


X

bf=-2
123
X
R
bf=-1 bf=0
175 175
R
bf=0 bf=0 bf=0
250 123 250

54
AVL Tree Rotations (2/4)

RR: Single Rotation (Left)

bf=-2 bf=0
123 175

R
bf=-1 bf=0
bf=0 250
175 123

R
bf=0 bf=0
250 X
X

55
Exercise: Re-Balance the AVL Tree

300
300

250 475 bf=-2


250 600

175 475 650


175 700

700

56
AVL Tree Rotations (3/4)

RL: Double Rotation (Right and Left)


bf=-2 bf=-2
374 374

Right
bf=1 bf=-1 bf=0
450 422 422
Left
bf=0 bf=0 bf=0 bf=0
422 450
450 374

57
Exercise: Re-Balance the AVL Tree

300300
300

bf=-2
bf=-2 250250 475475
265 475

265270
250 270

265 270

58
AVL Tree Rotations (4/4)

LR: Double Rotation (Left and Right)


bf=2 bf=2
450 450

bf=-1 bf=1 bf=0


374 422 422
R
bf=0 bf=0 bf=0 bf=0
422 374 450
374

59
Exercise: Rebalance the AVL Tree

250
250
250

222 474
222 474
222 422

150 422
150 346
150 346 474

346
422

60
Exercise: Result

250

222 422

150 346 474

61
Rotation of an Entire Subtree

62
<Notation>

450 450

374 501 374


T1

512 555

63
AVL Tree Rotations: General (1/4)
LL: Single Rotation (Right)
bf=2
450

bf=1 th=h bf=1


374 374
T3
th=h+1 th=h th=h+1 th=h
450
T1 T2 T1 T2
th=h
T3
64
AVL Tree Rotations: General (1/4) cont’d

LL: Single Rotation (Right)

bf=0
374 374

th=h+1 bf=0
450 450
T1 T2 T1
th=h th=h

T3 T2 T3

65
AVL Tree Rotations: General (2/4)
RR: Single Rotation (Left)
bf=-2
374

th=h bf=-1
450 450
T1
th=h th=h+1
374
T2 T3 T2 T3

T1
66
AVL Tree Rotations: General (2/4) cont’d
RR: Single Rotation (Left)

bf=0
450 450

bf=0 th=h+1
374 374
T2 T3 T3
th=h th=h

T1 T1 T2

67
AVL Tree Rotations: General (3/4)

RL: Double Rotation (Right and Left)


bf=-2
450 450

th=h bf=1
501 473
T1 T1
th=h

473 501
T4 T2 T3

th=h th=h
T4
T2 T3
68
AVL Tree Rotations: General (3/4) cont’d

RL: Double Rotation (Right and Left)


450 450

473 473
T1 T1

501 501
T2 T3 T2

T4 T3 T4
69
AVL Tree Rotations: General (3/4) cont’d

RL: Double Rotation (Right and Left)


450

473
473
T1

450 501
T2 501 T2

T1 T3 T4
T3 T4
70
AVL Tree Rotations: General (3/4) cont’d

RL: Double Rotation (Right and Left)

bf=0 473
473

450
450 501 501
T2

T3 T4 T1 T2 T3 T4
T1
th=h th=h th=h th=h
71
AVL Tree Rotations: General (4/4)

LR: Double Rotation (Right and Left)


bf=2
501

bf=-1
450
T1

473 th=h
T4

th=h
T2 T3
th=h th=h 72
AVL Tree Rotations: General (4/4)

LR: Double Rotation (Right and Left)


bf=2
501
bf=0
473
bf=-1
450
T1 450
501

473 th=h
T4
T4 T2 T3 T1
th=h
T2 T3 th=h th=h th=h th=h
th=h th=h 73
Example: Build an AVL Tree (1/12)

March

insert May

74
Example: Build an AVL Tree (2/12)

March
“alphabetical order”

May

insert November

75
Exercise: Build an AVL Tree (3/12)

bf=-2
March

May

November

76
Exercise: Build an AVL Tree (4/12)

May RR rotation

March November

insert August

77
Example: Build an AVL Tree (5/12)

bf=1 May

March November

insert April
August

78
Exercise: Build an AVL Tree (6/12)

bf=2 May

bf=2 March November

August

April

79
Example: Build an AVL Tree (7/12)

bf=1 May

bf=0 August November

insert January
April March

LL rotation

80
Exercise: Build an AVL Tree (8/12)

May
bf=2 May

March November
bf=-1 August November

August
April March bf=1

April January
January

81
Example: Build an AVL Tree (9/12)

March

August May
insert December
insert July
April January November
insert February
LR rotation

82
Example: Build an AVL Tree (10/12)
March

August May

April January November

December July

February 83
Exercise: Build an AVL Tree (11/12)
March March
bf=2

May August May


bf=-2 August

November December November


April January April

July January
December

July

February February 84
Exercise: Build an AVL Tree (12/12)
March

December May

August January November

April February July

RL rotation 85
WHW 2-1: AVL Programs
 https://www.thecrazyprogrammer.com/2014/03/c-
program-for-avl-tree-implementation.html
 (30 points) Hand trace the Above program in doing
the following sequence of 5 inserts:
 (for the purpose of easier conceptualization, as-

sume the node key is a string, and node address


is an integer, as shown below)
 fish (address: 100), dog (addr: 120), cat (addr:

130), mouse (addr: 140), lion (addr: 150)


 “hand tracing a program” means following the

program line by line (as if you are the computer),


plugging in the values for all the variables, and
the function calls and returns.
86
WHW 2-2: (10 points)
AVL Tree Insert and Delete
 Insert
 Cat Dog Bat Fish Chicken Cow Tiger Eagle
Lion Snake Bird Owl Mouse
 Delete
 Cow Snake Owl Cat Mouse Eagle Bird

87
End of Lecture

88

You might also like