Title: CS1102 Tut 7 AVL Trees
1CS1102 Tut 7 AVL Trees
- Max Tan
- tanhuiyi_at_comp.nus.edu.sg
- COM1-01-09 Tel65164364http//www.comp.nus.edu.s
g/tanhuiyi
2Group Assignments
- Group 1 Q2
- Group 2 Q3
- Group 3 Q4
- Group 4 Q1
3First 15 minutes
- Write an AVL ADT
- What are the public and private methods you need
? - Public operations Add, Delete, Get, Size
- What do you need to do after you add?
- Balance
- When balancing, you need to rotateLeft and
rotateRight - To balance, you need to know the height of the
left and right subtrees
4First 15 minutes
- Algorithm for Balance, consider the cases
- Case1 Left.height gt right.height
5First 15 minutes
- Algorithm for Balance, consider the cases
- Case2 Right.height gt left.height
6First 15 minutes
- So, break it up into two cases and consider them
seperately - Write a rotateLeft and a rotateRight method to
further simplify the problem!
7First 15 minutes
- In this case we simply rotate right about x
- rotateRight(x)
8First 15 minutes
- In this case just rotating right wont solve the
problem! (why? See next slide) - rotateLeft(y)
- rotateRight(x)
9First 15 minutes
10First 15 minutes
- Simply rotate about x
- rotateLeft(x)
11First 15 minutes
- Again simply rotating about x wont solve this
- rotateRight(z)
- rotateLeft(x)
12Question 1
- Draw the AVL tree after performing each of the
following operation consecutively on an initially
empty binary search tree insert 8, insert 6,
insert 12, insert 3, insert 10, insert 9, delete
12, delete 8, insert 7, and insert 8.
13Question 1
- After we insert 8, 6, 12, 3, 10, 9, the tree is
as below
Violation!
14Question 1
- Do a rotate right at node 12, and obtain the AVL
tree as below
15Question 1
16Question 1
17Question 1
18Question 1
Violation
CASE INSERT INSIDE
1
3
19Question 1
- Violation Case Insert Inside (2 rotations)
Violation
20Question 1
- Violation Case Insert Inside (2 rotations)
Violation
21Question 2
22Question 2
- Step 1 83 is removed, Violation at 95, so rotate
left at 95
23Question 2
- Step 1 83 is removed, Violation at 95, so rotate
left at 95 (result)
24Question 2
- Step 2 Equivalent to insert inside, so we need
to rotate left about 34 first
25Question 2
- Step 2 Equivalent to insert inside, so we need
to rotate left about 34 first (result)
26Question 2
- Step 3 Violation at 67, so rotate right about 67
27Question 2
- Step 3 Violation at 67, so rotate right about 67
(result)
28Question 2
- An AVL tree may no longer be balanced when we
delete an item from it. How many sub-trees will
be re-balanced in the worst case when an item is
deleted from an AVL tree? Give an example to
support your argument.
29Question 2
- Consider cases where rotation is needed
30Question 2
Delete 3
31Question 2
32Question 3
- Construct minimal AVL trees of height equals to
1, 2, 3, 4, and 5. What is the number of nodes in
a minimal AVL tree of height 6? How many
different shapes of a minimal AVL tree of height
h can have?
33Question 3
- Can you see the recursion?
H 1
H 2
H 3
H 4
H 5
H6 has1 12 7 20 nodes
34Question 3
- Can you see the recursion?
MinNodes(h) 1 MinNodes(h-1)
MinNodes(h-2)
35Question 3
- How about the number of shapes?
Two ways to permuate 2 shapes
36Question 3
- How about the number of shapes?
What about the permutations of the items in the
subtrees ??? Recursion!
37Question 3
- How about the number of shapes?
NumShapes(Node n) 2 NumShapes(n.left)
NumShapes(n.right)
38Question 4
- Given an AVL tree with n integer items and two
integers a and b, where a and b can be any
integers with a lt b, - (a) Implement an algorithm to count the number of
nodes within the range a, b - (b) What is the time complexity of your
algorithm. Explain your answer.
39Question 4
67
rangeCount(curr, 48, 95)
48
96
34
55
95
98
25
42
46
12
40Question 4
Within the range, search both subtrees
67
rangeCount(curr, 48, 95)
48
96
34
55
95
98
25
42
46
12
41Question 4
67
Within the range, search both subtrees
48
96
rangeCount(curr, 48, 95)
34
55
95
98
25
42
46
12
42Question 4
67
Within the range, search both subtrees
48
96
rangeCount(curr, 48, 95)
34
55
95
98
25
42
46
12
43Question 4
67
Smaller then 48, search right subtree
48
96
rangeCount(curr, 48, 95)
34
55
95
98
25
42
46
12
44Question 4
67
Smaller then 48, search right subtree
48
96
rangeCount(curr, 48, 95)
34
55
95
98
25
42
46
12
45Question 4
67
Within range, return size
48
96
rangeCount(curr, 48, 95)
34
55
95
98
25
42
46
12
46Question 4
67
Within range, return size
48
96
rangeCount(curr, 48, 95)
34
55
95
98
25
42
46
12
47Question 4
Larger then 95, search left subtree
67
rangeCount(curr, 48, 95)
48
96
34
55
95
98
25
42
46
12
48Question 4
Larger then 95, search left subtree
67
rangeCount(curr, 48, 95)
48
96
34
55
95
98
25
42
46
12