CS1102 Tut 7 AVL Trees - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

CS1102 Tut 7 AVL Trees

Description:

D. Delete 3. Question 2. Question 3 ... How many different shapes of a minimal AVL tree of height h can have? Question 3. H = 1 ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 49
Provided by: soc128
Category:
Tags: avl | cs1102 | shapes | trees | tut

less

Transcript and Presenter's Notes

Title: CS1102 Tut 7 AVL Trees


1
CS1102 Tut 7 AVL Trees
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • COM1-01-09 Tel65164364http//www.comp.nus.edu.s
    g/tanhuiyi

2
Group Assignments
  • Group 1 Q2
  • Group 2 Q3
  • Group 3 Q4
  • Group 4 Q1

3
First 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

4
First 15 minutes
  • Algorithm for Balance, consider the cases
  • Case1 Left.height gt right.height

5
First 15 minutes
  • Algorithm for Balance, consider the cases
  • Case2 Right.height gt left.height

6
First 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!

7
First 15 minutes
  • In this case we simply rotate right about x
  • rotateRight(x)

8
First 15 minutes
  • In this case just rotating right wont solve the
    problem! (why? See next slide)
  • rotateLeft(y)
  • rotateRight(x)

9
First 15 minutes
10
First 15 minutes
  • Simply rotate about x
  • rotateLeft(x)

11
First 15 minutes
  • Again simply rotating about x wont solve this
  • rotateRight(z)
  • rotateLeft(x)

12
Question 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.

13
Question 1
  • After we insert 8, 6, 12, 3, 10, 9, the tree is
    as below

Violation!
14
Question 1
  • Do a rotate right at node 12, and obtain the AVL
    tree as below

15
Question 1
  • delete 12

16
Question 1
  • delete 8

17
Question 1
  • Insert 7 and 8

18
Question 1
  • Violation

Violation
CASE INSERT INSIDE
1
3
19
Question 1
  • Violation Case Insert Inside (2 rotations)

Violation
20
Question 1
  • Violation Case Insert Inside (2 rotations)

Violation
21
Question 2
22
Question 2
  • Step 1 83 is removed, Violation at 95, so rotate
    left at 95

23
Question 2
  • Step 1 83 is removed, Violation at 95, so rotate
    left at 95 (result)

24
Question 2
  • Step 2 Equivalent to insert inside, so we need
    to rotate left about 34 first

25
Question 2
  • Step 2 Equivalent to insert inside, so we need
    to rotate left about 34 first (result)

26
Question 2
  • Step 3 Violation at 67, so rotate right about 67

27
Question 2
  • Step 3 Violation at 67, so rotate right about 67
    (result)

28
Question 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.

29
Question 2
  • Consider cases where rotation is needed

30
Question 2
Delete 3
31
Question 2
32
Question 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?

33
Question 3
  • Can you see the recursion?

H 1
H 2
H 3
H 4
H 5
H6 has1 12 7 20 nodes
34
Question 3
  • Can you see the recursion?

MinNodes(h) 1 MinNodes(h-1)
MinNodes(h-2)
35
Question 3
  • How about the number of shapes?

Two ways to permuate 2 shapes
36
Question 3
  • How about the number of shapes?

What about the permutations of the items in the
subtrees ??? Recursion!
37
Question 3
  • How about the number of shapes?

NumShapes(Node n) 2 NumShapes(n.left)
NumShapes(n.right)
38
Question 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.

39
Question 4
67
rangeCount(curr, 48, 95)
48
96
34
55
95
98
25
42
46
12
40
Question 4
Within the range, search both subtrees
67
rangeCount(curr, 48, 95)
48
96
34
55
95
98
25
42
46
12
41
Question 4
67
Within the range, search both subtrees
48
96
rangeCount(curr, 48, 95)
34
55
95
98
25
42
46
12
42
Question 4
67
Within the range, search both subtrees
48
96
rangeCount(curr, 48, 95)
34
55
95
98
25
42
46
12
43
Question 4
67
Smaller then 48, search right subtree
48
96
rangeCount(curr, 48, 95)
34
55
95
98
25
42
46
12
44
Question 4
67
Smaller then 48, search right subtree
48
96
rangeCount(curr, 48, 95)
34
55
95
98
25
42
46
12
45
Question 4
67
Within range, return size
48
96
rangeCount(curr, 48, 95)
34
55
95
98
25
42
46
12
46
Question 4
67
Within range, return size
48
96
rangeCount(curr, 48, 95)
34
55
95
98
25
42
46
12
47
Question 4
Larger then 95, search left subtree
67
rangeCount(curr, 48, 95)
48
96
34
55
95
98
25
42
46
12
48
Question 4
Larger then 95, search left subtree
67
rangeCount(curr, 48, 95)
48
96
34
55
95
98
25
42
46
12
Write a Comment
User Comments (0)
About PowerShow.com