raolect7

Upload: sakura2709

Post on 04-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 RaoLect7

    1/19

    1R. Rao, CSE 326

    CSE 326 Lecture 7: More on Search Trees

    ! Todays Topics:" Lazy Operations" Run Time Analysis of Binary Search Tree Operations" Balanced Search Trees

    # AVL Trees and Rotations

    ! Covered in Chapter 4 of the text

    2R. Rao, CSE 326

    From Last Time: Remove (Delete) Operation! Removing a node containing X:

    1. Find the node containing X2. Replace it with:

    If it has no children, with NULLIf it has 1 child, with that childIf it has 2 children, with the node withthe smallest value in its right subtree,(or largest value in left subtree)

    3. Recursively remove node used in 2 and 3

    ! Worst case : Recursion propagates allthe way to a leaf node time isO(depth of tree)

    10

    5 24

    94

    97

    11

    17

  • 8/13/2019 RaoLect7

    2/19

    3R. Rao, CSE 326

    Laziness in Data Structures

    ! A lazy operation is one that puts off work as much aspossible in the hope that a future operation will make thecurrent operation unnecessary

    D a t a S t r u c t - u r e s

    4R. Rao, CSE 326

    Lazy Deletion! Idea: Mark node as deleted ; no need to reorganize tree

    " Skip marked nodes during Find or Insert" Reorganize tree only when number of marked nodes

    exceeds a percentage of real nodes (e.g. 50%)" Constant time penalty only due to marked nodes depth

    increases only by a constant amount if 50% are markedundeleted nodes (N nodes max N/2 marked)

    ! Modify Insert to make use of marked nodes whenever

    possible e.g. when deleted value is re-inserted! Can also use lazy deletion for Lists

  • 8/13/2019 RaoLect7

    3/19

    5R. Rao, CSE 326

    Run Time Analysis of BST operations

    ! All BST operations (except MakeEmpty) are O(d), where d isthe depth of the accessed node in the tree" MakeEmpty takes O(N) for a tree with N nodes frees all nodes

    ! We know: log N ! d ! N-1 for a binary tree with N nodes" What is the best case tree? What is the worst case tree?

    ! Best Case Running Time of Insert/Remove/etc. = ?

    ! Worst Case Running Time = ?

    ! Average Case Running Time = ?

    6R. Rao, CSE 326

    The best, the worst, and the average! For a binary tree with N nodes, depth d of any node satisfies:

    log N ! d ! N-1

    ! So, best case running time of BST operations is O(log N)

    ! Worst case running time is O(N)

    ! Average case running time = O(average value of d) = O(log N)" Can prove that average depth over all nodes = O(log N) if all

    insertion sequences equally likely." See Chap. 4 in textbook for proof

  • 8/13/2019 RaoLect7

    4/19

    7R. Rao, CSE 326

    Can we do better?

    ! Worst case running time of BST operations is O(N)

    ! E.g. What happens when you Insert elements in ascending(or descending) order ?" Insert 2, 4, 6, 8, 10, 12 into an empty BST

    ! Problem : Lack of balance Tree becomes highly asymmetric

    ! Idea: Can we restore balance by re-arranging tree according todepths of left and right subtrees?"

    Goal: Get depth down from O(N) to O(log N)

    8R. Rao, CSE 326

    Idea #1: Achieving the perfect balance! First try at balancing trees: Perfect balance

    " Re-arrange to get a complete tree afterevery operation

    ! Recall: A tree is complete if there are noholes when scanning from top tobottom, left to right

    ! Problem : Too expensive to re-arrange" E.g. Insert 2 in the example shown

    ! Need a looser constraint

    6

    4 9

    1 5 8

    5

    2 8

    1 4 6 9

    Insert 2 &make treecomplete

  • 8/13/2019 RaoLect7

    5/19

    9R. Rao, CSE 326

    Idea #2: Leave it to the professionals

    ! Many efficient algorithms exist for balancing trees inorder to achieve faster running times for the BSToperations" Adelson-Velskii and Landis (AVL) trees (1962)" Splay trees and other self-adjusting trees (1978)" B-trees and other multiway search trees (1972)

    10R. Rao, CSE 326

    AVL Trees! AVL trees are height-balanced binary

    search trees

    ! Balance factor of a node = height(leftsubtree) - height(right subtree)

    ! An AVL tree can only have balancefactors of 1, 0, or 1 at every node" For every node, heights of left and

    right subtree differ by no more than 1

    " Height of an empty subtree = -1

    ! Implementation : Store currentheights in each node

    6

    4

    1

    8

    7

    21

    1

    9

    6

    4

    1

    8

    7

    -1

    1-(-1) = 21

    (-1)-0 = -1

    9

    Heights

    Balancefactors

    0

    0

    0

    0

    3

  • 8/13/2019 RaoLect7

    6/19

  • 8/13/2019 RaoLect7

    7/19

    13R. Rao, CSE 326

    The good news about AVL Trees

    ! Can prove: Height of an AVL tree of Nnodes is always O(log N)

    ! How? Can show:" Height h ! 1.44 log(N+2)-0.328" Prove using recurrence relation for

    minimum number of nodes S(h) in anAVL tree of height h:S(h) = S(h-1) + S(h-2) + 1

    " Use Fibonacci numbers to get bound

    on S(h) bound on height h" See textbook for details

    6

    4 9

    1 5 8

    0

    10

    00

    0

    0

    1 -1

    0

    6

    4

    17

    90Height =O(log N)

    14R. Rao, CSE 326

    The really good news about AVL Trees! Can prove: Height of an AVL tree of N

    nodes is always O(log N)

    ! All operations (e.g. Find, Removeusing lazy deletion, etc.) on an AVLtree are O(log N)

    ! except Insert" Why is Insert different? 0

    1 -1

    0

    6

    5

    4

    7

    90

    Insert 3

  • 8/13/2019 RaoLect7

    8/19

    15R. Rao, CSE 326

    The bad news about AVL Trees

    0

    1 -1

    0

    6

    5

    4

    7

    90

    Insert 31

    2 -1

    1

    6

    5

    4

    7

    90

    0 3

    No longer an AVL tree(i.e. not balanced anymore)

    16R. Rao, CSE 326

    Restoring Balance in (the life of) an AVL Tree! Problem : Insert may cause balance factor to become 2 or 2

    for some node on the path from insertion point to root node

    ! Idea : After Inserting the new node,1. Back up to root updating heights along the access path2. If Balance Factor = 2 or 2, adjust tree by rotation

    around deepest such node.1

    2 -1

    1

    6

    5

    4

    7

    90

    0 3

  • 8/13/2019 RaoLect7

    9/19

    17R. Rao, CSE 326

    Rotating to restore Balance: A Simple Example

    0

    1 -1

    0

    6

    5

    4

    7

    90

    Insert 3 1

    2 -1

    1

    6

    5

    4

    7

    90

    0 3

    0

    0 -1

    0

    6

    4

    3

    7

    9 05 0

    Rotate

    AVL Not AVL AVL

    18R. Rao, CSE 326

    BF = 1

    BF = 0

    BF = 0 BF = 0

    BF = 1

    BF = 0

    BF = 0BF = 0

    BF = 0

    Tree before insertion(BF = Balance Factor)

    Various Cases of Insertion

  • 8/13/2019 RaoLect7

    10/19

    19R. Rao, CSE 326

    BF = ?

    BF = ?

    BF = 0BF = ?

    BF = 0

    Tree after insertion

    BF = 0

    Outside Case

    BF = ?

    20R. Rao, CSE 326

    BF =

    BF =

    BF = BF =

    BF =

    BF =

    BF =BF =

    BF =

    Tree after insertionBF = 0

    Inside Case

  • 8/13/2019 RaoLect7

    11/19

    21R. Rao, CSE 326

    Let the node that needs rebalancing be " .

    There are 4 cases:Outside Cases (require single rotation ) :

    1. Insertion into left subtree of left child of " .2. Insertion into right subtree of right child of " .

    Inside Cases (require double rotation ) :3. Insertion into right subtree of left child of " .4. Insertion into left subtree of right child of " .

    Rebalancing is performed through four separate rotationalgorithms .

    Insertions in AVL Trees

    22R. Rao, CSE 326

    j

    k

    X Y

    Z

    Consider a validAVL subtree

    Insertions in AVL Trees: Outside Case

  • 8/13/2019 RaoLect7

    12/19

    23R. Rao, CSE 326

    j

    k

    X Y

    Z

    Inserting into Xdestroys the AVLproperty

    Insertions in AVL Trees: Outside Case

    24R. Rao, CSE 326

    j

    k

    X Y

    Z

    Do a right rotation

    Insertions in AVL Trees: Outside Case

  • 8/13/2019 RaoLect7

    13/19

    25R. Rao, CSE 326

    j

    k

    X Y

    Z

    Do a right rotation

    Insertions in AVL Trees: Outside Case

    26R. Rao, CSE 326

    jk

    X Y Z

    Right rotation done!

    Insertions in AVL Trees: Outside Case

    AVL property has been restored!(Left rotation is mirror symmetric)

  • 8/13/2019 RaoLect7

    14/19

    27R. Rao, CSE 326

    j

    k

    X Y

    Z

    Insertions in AVL Trees: Inside Case

    Consider a validAVL subtree

    28R. Rao, CSE 326

    Inserting into Ydestroys theAVL property

    j

    k

    X Y

    Z

    Insertions in AVL Trees: Inside Case

    Does right rotationrestore balance?

  • 8/13/2019 RaoLect7

    15/19

    29R. Rao, CSE 326

    jk

    X

    Y Z

    Right rotationdont do nothinto restore balance

    Insertions in AVL Trees: Inside Case

    30R. Rao, CSE 326

    Consider the structureof subtree Y j

    k

    X Y

    Z

    Insertions in AVL Trees: Inside Case

  • 8/13/2019 RaoLect7

    16/19

    31R. Rao, CSE 326

    j

    k

    X V

    Z

    W

    i

    Y = node i andsubtrees V and W

    Insertions in AVL Trees: Inside Case

    32R. Rao, CSE 326

    j

    k

    X V

    Z

    W

    i

    Insertions in AVL Trees: Inside Case

    Lets try a left-rightdouble rotation . . .

  • 8/13/2019 RaoLect7

    17/19

    33R. Rao, CSE 326

    j

    k

    X V

    Z W

    i

    Steps for Left-RightDouble Rotation

    Insertions in AVL Trees: Inside Case

    1. Left Rotation :Adjust relevantpointers

    34R. Rao, CSE 326

    2. Right Rotation :Adjust relevantpointers

    3. Make i the root jk

    X V Z W

    i

    Balance has been restored!

    (Right-left case is mirror-symmetric)

    Insertions in AVL Trees: Inside CaseSteps for Left-RightDouble Rotation

  • 8/13/2019 RaoLect7

    18/19

    35R. Rao, CSE 326

    AVL Tree Exercise

    ! Insert 8, 1, 0, 2 in that order into following AVL tree:

    4

    3 6

    5 7

    36R. Rao, CSE 326

    Arguments for AVL trees:

    1. Search is O(log N) since AVL trees are always balanced .2. The height balancing adds no more than a constant factor to

    the speed of insertion.

    Arguments against using AVL trees :

    1. Difficult to program & debug; more space for height info.2. Asymptotically faster but can be slow in practice.3. Most large searches are done in database systems on disk and

    use other structures (e.g. B-trees ).4. May be OK to have O(N) for a single operation if total run

    time for many consecutive operations is fast (e.g. Splay trees).

    Pros and Cons of AVL Trees

  • 8/13/2019 RaoLect7

    19/19

    37R. Rao, CSE 326

    Next Class:

    All about Splaying

    To Do:

    Finish Reading Chapter 4