1 / 27

Foundation of Computing Systems

Foundation of Computing Systems. Lecture 12 Trees: Part VII. Splay Tree. A binary search tree is one of the most important non-linear data structures.

Download Presentation

Foundation of Computing Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Foundation of Computing Systems Lecture 12 Trees: Part VII IT 60101: Lecture #12

  2. Splay Tree • A binary search tree is one of the most important non-linear data structures. • a variety of methods are available to keep the tree balanced and hence to guarantee that operations such as searching, insertion and deletion can be done in O(log2n) time in the worst case. • Height-balancing by AVL-rotations (AVL trees) • Node colouring (red-black trees) • No doubt that these trees are efficient so far searching, insertion and deletion of a node is concerned. • But there are some situations, when the ways of balancing may not guarantee the optimal performance. IT 60101: Lecture #12

  3. Splay Tree • For example, consider the binary search trees T1 and T2 • Both the trees T1and T2 contain the same set of key elements say, k1, k2, k3, k4, k5 in their five nodes such that k1 < k2 < k3 < k4 < k5 IT 60101: Lecture #12

  4. Splay Tree Key Number of Number of Number of accesses comparisons comparisons in treeT1in treeT2 k11 1×2 1×3 k21 1×1 1×2 k310 10×3 10×1 k42 2×2 2×3 k53 3×3 3×2 Total no. of comparisons 46 27 IT 60101: Lecture #12

  5. Splay Tree • Definition A splay tree is a binary search tree and it adjusts its balance dynamically providing lower access time of higher occurrence nodes. IT 60101: Lecture #12

  6. Splay Tree • Operations • Searching • Insertion • Deletion • Splaying IT 60101: Lecture #12

  7. Splaying Operation • The basic idea of the splaying operation in a tree is that after a node is accessed, it is pushed to the root. • Not a simple swap operation (this may violates binary search tree property) • Way out • Restructuring is by a series of AVL rotations IT 60101: Lecture #12

  8. Splaying Operation: Bottom Up AVL Rotations IT 60101: Lecture #12

  9. Splaying Operation: Bottom Up AVL Rotations IT 60101: Lecture #12

  10. Splaying Operation: Bottom Up AVL Rotations IT 60101: Lecture #12

  11. Splaying Operation: Bottom Up AVL Rotations IT 60101: Lecture #12

  12. Splaying Operation: Bottom Up AVL Rotations IT 60101: Lecture #12

  13. Side Effect of Bottom Up AVL Rotations • Although repeated applications of single AVL-rotation work, it possibly deteriorates the situation for the other nodes on (original) their access paths. • As evident in the splaying operation just discussed, we see that the repeated applications of single AVL rotations have the effect of pushing k2 all the way to the root and subsequently making easy the future accessing of k2. • Unfortunately, it has pushed up another node k1 almost as deep as k2. • Another side effect happens to a node like k3 which was earlier closer to root now goes far away from the root • A possible way to avoid the side effects is to follow splaying strategy of both double and single AVL-rotations, but selectively. IT 60101: Lecture #12

  14. Splaying Operations IT 60101: Lecture #12

  15. Splaying Operations IT 60101: Lecture #12

  16. Splaying Operations IT 60101: Lecture #12

  17. Splaying Operations IT 60101: Lecture #12

  18. Splaying Operations IT 60101: Lecture #12

  19. Case 1: Zig or Zag • Case 1:zig or zag • If the parent of the x is the root of the tree then • if x is the left child • perform zig • else (i.e. x is the right child) • perform zag IT 60101: Lecture #12

  20. Case 2: Zig-Zig or Zag-Zag • Case 2:zig-zig or zag-zag • If x has both a parent (p) and a grandparent (g) • If x is the left child of p and p is the left child of g • perform zig-zig • else (i.e. x is the right child of p and p is the right child of g) • perform zag-zag IT 60101: Lecture #12

  21. Case 3: Zig-Zag or Zag-Zig • Case 3:zig-zag or zag-zig • If x has both a parent (p) and a grandparent (g) • If x is the right child of p and p is the left child of g then • perform zig-zag • else (i.e. x is the left child of p and p is the right child of g) • perform zag-zig IT 60101: Lecture #12

  22. Illustration of Splaying Operation IT 60101: Lecture #12

  23. Illustration of Splaying Operation IT 60101: Lecture #12

  24. Illustration of Splaying Operation IT 60101: Lecture #12

  25. Illustration of Splaying Operation IT 60101: Lecture #12

  26. Illustration of Splaying Operation IT 60101: Lecture #12

  27. For detail implementation of splaying operation on binary search trees see the book • Classic Data Structures • Chapter 7 • PHI, 2nd Edn., 17th Reprint IT 60101: Lecture #12

More Related