CS1102 Tut 7 AVL Trees - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

CS1102 Tut 7 AVL Trees

Description:

In this case just rotating right wont solve the problem! ( why? See next ) rotateLeft(y) ... the maximum number of rotations needed when inserting a single ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 53
Provided by: dcsa
Category:
Tags: avl | cs1102 | rotating | 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
  • S15-03-07 Tel65164364http//www.comp.nus.edu.sg
    /tanhuiyi

2
Groups 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

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

4
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

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

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

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

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

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

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

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

13
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.

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

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

16
Question 1
  • delete 12

17
Question 1
  • delete 8

18
Question 1
  • Insert 7 and 8

19
Question 1
  • Violation

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

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

Violation
22
Question 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?

23
Question 2
  • Can you see the recursion?

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

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

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

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

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

29
Question 3
  • Consider cases where rotation is needed

30
Question 3
Delete 3
31
Question 3
32
Question 4
  • Show that the height of an AVL tree with 32 nodes
    must be exactly 6.

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

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

35
Question 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?

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

37
Question 5
  • Consider case-by-case

x
Case 1 Low lt x lt high Case 2 X lt Low Case
3 X gt high
38
Question 5
  • Case 1 Low lt x lt high

Print x How about the left and right subtrees ?
x
lt x lt
So recursively call for the left and right
subtrees!
39
Question 5
  • Case 2 x lt Low

X not within range, so dont print it
x
X lt
But the right subtree may be within range!
40
Question 5
  • Case 3 x gt High

X not within range, so dont print it
x
lt x
But the left subtree may be within range!
41
Question 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)

42
Question 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)
43
Question 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)
44
Question 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)
45
Question 5
  • Observe two forks everytime
  • Whats the length of each path ?
  • How many elements between the paths?

46
Question 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)
47
Question 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
  • Extra materials

49
Question 3 extra
  • What about insertion? What is the maximum number
    of rotations needed when inserting a single item ?

50
Question 3
  • Case 1 Tree is balanced

Claim you will never need to rotate this node
h
51
Question 3
  • Case 2 Left subtree is heavy

Claim you will never need to rotate this node
h
At most 1 difference
52
Question 3
  • Case 3 Right subtree is heavy

Claim you will never need to rotate this node
h 1
At most 1 difference
Write a Comment
User Comments (0)
About PowerShow.com