Title: CS1102 Tut 7 AVL Trees
1CS1102 Tut 7 AVL Trees
- Max Tan
- tanhuiyi_at_comp.nus.edu.sg
- S15-03-07 Tel65164364http//www.comp.nus.edu.sg
/tanhuiyi
2Groups Assignment
- Question 1 Group 3
- Question 2 Group 4
- Question 3 Group 1
- Question 4 Group 2
- Question 5 Group 3
- Question 6 Group 4
3Groups Assignment
- Question 1 Group 2
- Question 2 Group 3
- Question 3 Group 1
- Question 4 Group 2
- Question 5 Group 3
- Question 6 Group 1
4First 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
5First 15 minutes
- Algorithm for Balance, consider the cases
- Case1 Left.height gt right.height
6First 15 minutes
- Algorithm for Balance, consider the cases
- Case2 Right.height gt left.height
7First 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!
8First 15 minutes
- In this case we simply rotate right about x
- rotateRight(x)
9First 15 minutes
- In this case just rotating right wont solve the
problem! (why? See next slide) - rotateLeft(y)
- rotateRight(x)
10First 15 minutes
11First 15 minutes
- Simply rotate about x
- rotateLeft(x)
12First 15 minutes
- Again simply rotating about x wont solve this
- rotateRight(z)
- rotateLeft(x)
13Question 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.
14Question 1
- After we insert 8, 6, 12, 3, 10, 9, the tree is
as below
Violation!
15Question 1
- Do a rotate right at node 12, and obtain the AVL
tree as below
16Question 1
17Question 1
18Question 1
19Question 1
Violation
CASE INSERT INSIDE
1
3
20Question 1
- Violation Case Insert Inside (2 rotations)
Violation
21Question 1
- Violation Case Insert Inside (2 rotations)
Violation
22Question 2
- 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?
23Question 2
- Can you see the recursion?
H 1
H 2
H 3
H 4
H 5
H6 has1 12 7 20 nodes
24Question 2
- Can you see the recursion?
MinNodes(h) 1 MinNodes(h-1)
MinNodes(h-2)
25Question 2
- How about the number of shapes?
Two ways to permuate 2 shapes
26Question 2
- How about the number of shapes?
What about the permutations of the items in the
subtrees ??? Recursion!
27Question 2
- How about the number of shapes?
NumShapes(Node n) 2 NumShapes(n.left)
NumShapes(n.right)
28Question 3
- 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 3
- Consider cases where rotation is needed
30Question 3
Delete 3
31Question 3
32Question 4
- Show that the height of an AVL tree with 32 nodes
must be exactly 6.
33Question 4
- From the previous question, we have
- n(1) 1
- n(2) 2
- n(h) 1 n(h-1) n(h-2)
- n(3) 4
- n(4) 7
- n(5) 12
- n(6) 20
- so n(7) 1 n(6) n(5) 1201233
- This means the height of an AVL tree with 32
nodes must be less - than 7, i.e. max height is 6.
34Question 4
- Full binary tree of height 5 has 25 1
- 31 nodes, hence a minimum height of a
- AVL tree with 32 nodes is 6.
- Min 6, max 6. Hence, an AVL tree
- with 32 nodes must be of height 6.
35Question 5
- Given an AVL tree with integer items and two
integers low and high (low lt high), write an
algorithm to output the items that lie between
(and including) low and high. What is the
complexity of your algorithm?
36Question 5
- Let range(T, low, high) be the method to return
the values in the BST/AVL tree T that are between
low and high. - The idea is similar to trace a tree in in-order
sequence but print, or traverse left/right
sub-tree only when they have possible answers.
37Question 5
x
Case 1 Low lt x lt high Case 2 X lt Low Case
3 X gt high
38Question 5
Print x How about the left and right subtrees ?
x
lt x lt
So recursively call for the left and right
subtrees!
39Question 5
X not within range, so dont print it
x
X lt
But the right subtree may be within range!
40Question 5
X not within range, so dont print it
x
lt x
But the left subtree may be within range!
41Question 5
- range(T, low, high)
-
- if T.item lt low //only need to search the right
subtree - range(T.right, low, high)
- if T.item ? low and T.item ? high then
- //need to search for both left and right
subtrees and the root is an //answer -
- range(T.left, low, high)
- print T.item
- range(T.right, low, high)
-
- if T.item gt high //only need to
search the left subtree - range(T.left, low, high)
42Question 5
- What is the complexity?
- Observe for any range
- Low 16 high 40
        20              Â
15Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 30
     10             17  Â
               25             40
 5       13    16     18       Â
23Â Â Â Â Â 28Â Â Â Â 35Â Â Â Â Â 50
(case 1)
43Question 5
- What is the complexity?
- Observe for any range
- Low 21 high 40
        20              Â
15Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 30
     10             17  Â
               25             40
 5       13    16     18       Â
23Â Â Â Â Â 28Â Â Â Â 35Â Â Â Â Â 50
(case 2)
44Question 5
- What is the complexity?
- Observe for any range
- Low 7 high 19
        20              Â
15Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 30
     10             17  Â
               25             40
 5       13    16     18       Â
23Â Â Â Â Â 28Â Â Â Â 35Â Â Â Â Â 50
(case 3)
45Question 5
- Observe two forks everytime
- Whats the length of each path ?
- How many elements between the paths?
46Question 5
- Length of each path h log n
- Number of elements bounded number of elements
in range NR
        20              Â
15Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 30
     10             17  Â
               25             40
 5       13    16     18       Â
23Â Â Â Â Â 28Â Â Â Â 35Â Â Â Â Â 50
h
(case 1)
47Question 5
- Total Complexity O(logn NR)
        20              Â
15Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 30
     10             17  Â
               25             40
 5       13    16     18       Â
23Â Â Â Â Â 28Â Â Â Â 35Â Â Â Â Â 50
h
(case 1)
48 49Question 3 extra
- What about insertion? What is the maximum number
of rotations needed when inserting a single item ?
50Question 3
Claim you will never need to rotate this node
h
51Question 3
- Case 2 Left subtree is heavy
Claim you will never need to rotate this node
h
At most 1 difference
52Question 3
- Case 3 Right subtree is heavy
Claim you will never need to rotate this node
h 1
At most 1 difference