Designing Classes and Programs by bH1nxDE0

VIEWS: 0 PAGES: 12

									Balanced Search Trees
     BST: efficient lookup, insertion , deletion
        Average case: O(log n) for all operations since find is
         O(log n) [complexity of insert after find is O(1), why?]
        Worst case is bad, what's big-Oh? What's the tree shape?


     Balanced Search trees
        Use rotations to maintain balance, different
         implementations rotate/rebalance at different times
        AVL tree is conceptually simple, bookkeeping means
         coefficient for big-Oh is higher than other ideas
        Red-black tree harder to code but good performance:
         basis for Java map classes and most C++ map classes


    CPS 100                                                         9.1
Balance trees we won't study
     B-trees are used when data is both in memory and on disk
        File systems, really large data sets
        Rebalancing guarantees good performance both
          asymptotically and in practice. Differences between
          cache, memory, disk are important

     Splay trees rebalance during insertion and during search,
      nodes accessed often more closer to root
        Other nodes can move further from root, consequences?
               • Performance for some nodes gets better, for others …
             No guarantee running time for a single operation, but
              guaranteed good performance for a sequence of
              operations, this is good amortized cost (vector
              push_back)


    CPS 100                                                             9.2
Balanced trees we will study
     Both kinds have worst-case O(log n) time for tree operations
     AVL (Adel’son-Velskii and Landis), 1962
        Nodes are “height-balanced”, subtree heights differ by 1
        Rebalancing requires per-node bookkeeping of height
        http://www.seanet.com/users/arsen/avltree.html


     Red-black tree uses same rotations, but can rebalance in one
      pass, contrast to AVL tree
        In AVL case, insert, calculate balance factors, rebalance
        In Red-black tree can rebalance on the way down, code
         is more complex, but doable
        STL in C++ uses red-black tree for map and set classes
        Standard java.util.TreeMap/TreeSet use red-black



    CPS 100                                                          9.3
Balanced Trees in Practice
     See usetreeset.cpp
        Balanced and unbalanced search trees as basis for sets
        Contrast with red-black trees in STL, see setstl.cpp


     Reasons STL code is faster than AVL code?
        Intrinsically better algorithm, one pass v. two pass
        Makes better use of storage allocation? (conceivably)
        More/better programming?


     Reading code: STL v. Java implementations
        What does tradition tell us?




    CPS 100                                                       9.4
Rotations and balanced trees
     Height-balanced trees
        For every node, left and
         right subtree heights differ
         by at most 1
        After insertion/deletion
         need to rebalance                Are these trees height-
        Every operation leaves tree      balanced?
         in a balanced state: invariant
         property of tree
     Find deepest node that’s
      unbalanced then make sure:
        On path from root to
         inserted/deleted node
        Rebalance at this
         unbalanced point only



    CPS 100                                                     9.5
Rotation to rebalance
           N                   When a node N (root) is
                        N       unbalanced height differs by 2
                                (must be more than one)
                   A              Change N->left->left
               C
                                      • doLeft
 A         B            B   C
                                    Change N->left->right
                                      • doLeftRight
Tree * doLeft(Tree * root)  Change N->right->left
{                                • doRightLeft
                             Change N->right->right
  Tree * newRoot = root->left;
  root->left = newRoot->right; • doRight
  newRoot->right = root;  First/last cases are symmetric
  return newRoot;         Middle cases require two rotations
}                            First of the two puts tree into
                               doLeft or doRight


 CPS 100                                                         9.6
Rotation up close (doLeft)
                                         Why is this called doLeft?
           N                               N will no longer be root,
                              N
                                            new value in left->left
                                            subtree
               C                           Left child becomes new
                      A
                                            root
     A     B              B       C

                              Rotation isn’t “to the left”, but
                                      
                              rather “brings left child up”
   Tree * doLeft(Tree * root)
                                doLeftChildRotate?
   {
     Tree * newRoot = root->left;
     root->left = newRoot->right;
     newRoot->right = root;
     return newRoot;
   }

 CPS 100                                                                9.7
Rotation to rebalance
                    ?????
           N
                                  Suppose we add a new node in
                             N     right subtree of left child of root
                                     Single rotation can’t fix
                C       A            Need to rotate twice
 A         B                 B    First stage is shown at bottom
                                 C
                                     Rotate blue node right
                                            • (its right child takes its place)
                                          This is left child of unbalanced
                                      Tree * doRight(Tree * root)
                                      {
                    C            B2
                            B1          Tree * newRoot = root->right;
A     B1       B2                       root->right = newRoot->left;
                        A
                                        newRoot->left = root;
                                        return newRoot;
                                      }

 CPS 100                                                                          9.8
Double rotation complete
     Calculate where to rotate and
      what case, do the rotations
Tree * doRight(Tree * root)
{
  Tree * newRoot = root->right;
                                                    C
                                                                      B2   C
  root->right = newRoot->left;        A   B1   B2                B1
  newRoot->left = root;
  return newRoot;
                                                         A
}

Tree * doLeft(Tree * root)
{
  Tree * newRoot = root->left;
  root->left = newRoot->right;
  newRoot->right = root;                       B1   B2
  return newRoot;
                                          A                  C
}




    CPS 100                                                                9.9
AVL tree practice
        Insert into AVL tree:                                   18
                                                                                      16
           18 10 16 12 6 3 8 13 14
           After adding 16: doLeftRight                   10
                                                                                10              18
                                  doLeft
                                                                      16
         18                   18
                                             16

10   doRight             16            10             18        10         18
         16         10
                                                           6         12

              After 3, doLeft on 16
                                                                                     10
                   16
                                        10
              10         18                                                6                   16
                                   6             16
         6         12
                              3             12        18              3         8         12         18
     3



 CPS 100                                                                                                  9.10
AVL practice: continued, and finished
     After adding 13, ok
     After adding14, not ok                              10
        doRight at 12
                                                  6                 16

                      10                  3           8        12         18

              6                 16

         3        8        12        18                        10

                                13                    6                  16

                                     14       3           8         13         18

                                                               12        14



    CPS 100                                                                         9.11
Rodney Brooks
     Flesh and Machines: “We are
      machines, as are our spouses,
      our children, and our dogs... I
      believe myself and my
      children all to be mere
      machines. But this is not how
      I treat them. I treat them in a
      very special way, and I
      interact with them on an
      entirely different level. They
      have my unconditional love,
      the furthest one might be able
      to get from rational analysis.”
     Director of MIT AI Lab



    CPS 100                             9.12

								
To top