CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.

download CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.

of 26

  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Embed Size (px)

Transcript of CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.

  • Slide 1
  • CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees
  • Slide 2
  • Today AVL and Splay Trees AVL Trees Double Rotation Splay Trees Animation: http://webpages.ull.es/users/jriera/Docencia/AVL/AVL%20tree%20applet.htm
  • Slide 3
  • AVL Trees Insert Let denote the node that must be rebalanced. Since any node has at most two children and a height imbalance requires that s two subtrees heights differ by two, we see that a violation may occur in four cases: 1. An insertion into the left subtree of the left child of . 2. An insertion into the right subtree of the left child of . 3. An insertion into the left subtree of the right child of . 4. An insertion into the right subtree of the right child of . Cases 1 and 4 are mirror image symmetries with respect to , as are cases 2 and 3.
  • Slide 4
  • Case 2 Single rotation fails to fix Case 2.
  • Slide 5
  • Case 2 Left-right double rotation fixes Case 2.
  • Slide 6
  • Case 3 Right-left double rotation fixes Case 3.
  • Slide 7
  • Example in Class We will insert this sequence into an initially empty AVL tree: 1, 2, , 6, 7, 16, 15, , 9, 8.
  • Slide 8
  • AVL Tree Node Declaration
  • Slide 9
  • AVL Node Height Computation
  • Slide 10
  • AVL Tree Insertion
  • Slide 11
  • Single Rotation
  • Slide 12
  • Double Rotation
  • Slide 13
  • Splay Trees
  • Slide 14
  • Advantages of Splaying It gives a good theoretical bound any M consecutive tree operations starting from an empty tree take at most O(M logN) time. It works well in practice in many applications, when a node is accessed, it is likely to be accessed again in the near future. (What computer component uses this idea?) Splay trees do not require the maintenance of height or balance information, thus saving space and simplifying the code.
  • Slide 15
  • Splaying Operations The following sequence of operations is repeated until the node containing the accessed value (after find or insert) is at the root. Let t point to the node, p point to its parent, and g point to its grandparent.
  • Slide 16
  • Zig-zag and Zig-zig
  • Slide 17
  • Example Insert 1, 2, 3, , N into an initially empty tree. Why does tree look like this? That is, why is the root not 1?
  • Slide 18
  • No Splaying Let us start accessing nodes. Access node 1. How much time does it take? Assume no splaying. Access node 2. How much time does it take?
  • Slide 19
  • Splaying Access node 1. Splay at the node 1. Access node 2. How much time will it take?
  • Slide 20
  • Bigger Example Splay at node 1. How much time will be required to access node 2?
  • Slide 21
  • Bigger Example Splay at node 2. How much time will be required to access node 3?
  • Slide 22
  • Bigger Example Splay at node 3. How much time will be required to access node 4?
  • Slide 23
  • Bigger Example Splay first at node 4, and then at node 5.
  • Slide 24
  • Bigger Example Splay first at node 6, and then at node 7.
  • Slide 25
  • Bigger Example Splay first at node 8, and then at node 9.
  • Slide 26
  • Observations When access paths are long, thus leading to a longer-than- normal search time, the rotations tend to be good for future operations. When accesses are cheap, the rotations are not as good and can be bad. The extreme case is the initial tree formed by the insertions. All the insertions are constant-time operations leading to a bad initial tree. At that point in time, we have a very bad tree, but we have a good running time. Then a couple of horrible accesses leave a nearly balanced tree. The cost is to give back some of the time that has been saved.