Advanced Trees Part II - PowerPoint PPT Presentation

1 / 85
About This Presentation
Title:

Advanced Trees Part II

Description:

An example of an AVL tree where the heights are shown next to the nodes: ... Removal begins as in a binary search tree, which means the node removed will ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 86
Provided by: BMorr1
Category:
Tags: advanced | heights | in | part | the | trees

less

Transcript and Presenter's Notes

Title: Advanced Trees Part II


1
Advanced TreesPart II
  • Briana B. Morrison
  • Adapted from Alan Eugenio
  • William J. Collins

2
Topics
  • AVL Trees
  • Part III
  • 2-3-4 Search Trees
  • Red Black Trees

3
(No Transcript)
4
(No Transcript)
5
(No Transcript)
6
(No Transcript)
7
AVL Tree Definition
  • AVL trees are balanced.
  • An AVL Tree is a binary search tree such that for
    every internal node v of T, the heights of the
    children of v can differ by at most 1.

An example of an AVL tree where the heights are
shown next to the nodes
8
Balance Factor
  • The balance factor is defined as
  • The height of the left subtree minus the height
    of the right subtree
  • The tree is balanced if the absolute value of the
    balance factor is lt1
  • ht(TL) ht(TR) lt 1

9
(No Transcript)
10
(No Transcript)
11
20 HEIGHT OF LEFT SUBTREE ? HEIGHT OF
RIGHT SUBTREE ?
12
(No Transcript)
13
NOPE! THE HEIGHT OF THE LEFT SUBTREE IS 1 AND
THE HEIGHT OF THE RIGHT SUBTREE IS 1.
14
(No Transcript)
15
(No Transcript)
16
(No Transcript)
17
NOPE THE LEFT AND RIGHT SUBTREES ARE NOT AVL
TREES, SO THE TREE IS NOT AN AVL TREE.
18
NOPE! THE HEIGHT OF THE LEFT SUBTREE IS 2 AND
THE HEIGHT OF THE RIGHT SUBTREE IS 0.
19
(No Transcript)
20
(No Transcript)
21
(No Transcript)
22
(No Transcript)
23
(No Transcript)
24
(No Transcript)
25
(No Transcript)
26
(No Transcript)
27
(No Transcript)
28
(No Transcript)
29
(No Transcript)
30
(No Transcript)
31
(No Transcript)
32
(No Transcript)
33
Height of an AVL Tree
  • Fact The height of an AVL tree storing n keys is
    O(log n).
  • Proof Let us bound n(h) the minimum number of
    internal nodes of an AVL tree of height h.
  • We easily see that n(1) 1 and n(2) 2
  • For n gt 2, an AVL tree of height h contains the
    root node, one AVL subtree of height n-1 and
    another of height n-2.
  • That is, n(h) 1 n(h-1) n(h-2)
  • Knowing n(h-1) gt n(h-2), we get n(h) gt 2n(h-2).
    So
  • n(h) gt 2n(h-2), n(h) gt 4n(h-4), n(h) gt 8n(n-6),
    (by induction),
  • n(h) gt 2in(h-2i)
  • Solving the base case we get n(h) gt 2 h/2-1
  • Taking logarithms h lt 2log n(h) 2
  • Thus the height of an AVL tree is O(log n)

34
(No Transcript)
35
(No Transcript)
36
(No Transcript)
37
(No Transcript)
38
(No Transcript)
39
(No Transcript)
40
(No Transcript)
41
(No Transcript)
42
(No Transcript)
43
(No Transcript)
44
(No Transcript)
45
(No Transcript)
46
  • ITEMS NOT IN THE SUBTREE OF THE ITEM ROTATED
    ABOUT ARE UNAFFECTED BY THE ROTATION.
  • A ROTATION TAKES CONSTANT TIME.
  • BEFORE AND AFTER A ROTATION, THE TREE IS STILL A
    BINARY SEARCH TREE.
  • THE CODE FOR A LEFT ROTATION IS SYMMETRIC TO THE
    CODE FOR A RIGHT ROTATION SIMPLY SWAP left AND
    right.

47
  • Now lets look at some examples of inserting into
    an AVL tree to get an idea of how all this
    rotation stuff works

48
Example Insert numbers 1-7 into AVL
49
(No Transcript)
50
(No Transcript)
51
(No Transcript)
52
(No Transcript)
53
(No Transcript)
54
(No Transcript)
55
(No Transcript)
56
(No Transcript)
57
Now add 16 and 15
58
(No Transcript)
59
(No Transcript)
60
Now an Example with State Abbr.
Insert DE
61
(No Transcript)
62
Insert OH
63
(No Transcript)
64
Insert MA
65
(No Transcript)
66
(No Transcript)
67
Insert IL and MI
68
Insert IN
69
(No Transcript)
70
(No Transcript)
71
Insert NY
72
(No Transcript)
73
(No Transcript)
74
Insert VT
75
(No Transcript)
76
(No Transcript)
77
?
78
Insertion in an AVL Tree
  • Insertion is as in a binary search tree
  • Always done by expanding an external node.
  • Example

cz
ay
bx
w
before insertion
after insertion
79
Restructuring (as Single Rotations)
  • Single Rotations

80
Restructuring (as Double Rotations)
  • double rotations

81
Removal in an AVL Tree
  • Removal begins as in a binary search tree, which
    means the node removed will become an empty
    external node. Its parent, w, may cause an
    imbalance.
  • Example

44
17
62
78
50
88
48
54
before deletion of 32
after deletion
82
Rebalancing after a Removal
  • Let z be the first unbalanced node encountered
    while travelling up the tree from w. Also, let y
    be the child of z with the larger height, and let
    x be the child of y with the larger height.
  • We perform restructure(x) to restore balance at
    z.
  • As this restructuring may upset the balance of
    another node higher in the tree, we must continue
    checking for balance until the root of T is
    reached

62
44
az
44
78
17
62
w
by
17
50
88
78
50
cx
48
54
88
48
54
83
Analysis of AVL Trees
  • The number of recursive calls to insert a new
    node can be as large as the height of the tree.
  • At most one (single or double) rotation will be
    done per insertion.
  • A rotation improves the balance of the tree, so
    later insertions are less likely to require
    rotations.
  • It is very difficult to find the height of the
    average AVL tree, but the worst case is much
    easier. The worst-case behavior of AVL trees is
    essentially no worse than the behavior of random
    search trees.

84
  • Empirical evidence suggests that the average
    behavior of AVL trees is much better than that of
    random trees, almost as good as that which could
    be obtained from a perfectly balanced tree.
  • Algorithms for manipulating AVL trees are
    guaranteed to take no more than about 44 percent
    more time than the optimum. In practice, AVL
    trees do much better than this on average,
    perhaps as small as lg n 0.25.

85
Running Times for AVL Trees
  • a single restructure is O(1)
  • using a linked-structure binary tree
  • find is O(log n)
  • height of tree is O(log n), no restructures
    needed
  • insert is O(log n)
  • initial find is O(log n)
  • Restructuring up the tree, maintaining heights is
    O(log n)
  • remove is O(log n)
  • initial find is O(log n)
  • Restructuring up the tree, maintaining heights is
    O(log n)
Write a Comment
User Comments (0)
About PowerShow.com