Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII."— Presentation transcript:

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

2 26.08.09IT 60101: Lecture #122 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(log 2 n) 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.

3 26.08.09IT 60101: Lecture #123 Splay Tree For example, consider the binary search trees T1 and T2 Both the trees T 1 and T 2 contain the same set of key elements say, k 1, k 2, k 3, k4, k 5 in their five nodes such that k 1 < k 2 < k 3 < k 4 < k 5

4 26.08.09IT 60101: Lecture #124 Splay Tree Key Number of Number of Number of accesses comparisons comparisons in tree T 1 in tree T 2 k 1 11×2 1×3 k 2 11×1 1×2 k 3 1010×3 10×1 k 4 22×2 2×3 k 5 33×3 3×2 Total no. of comparisons 46 27

5 26.08.09IT 60101: Lecture #125 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.

6 26.08.09IT 60101: Lecture #126 Splay Tree Operations Searching Insertion Deletion Splaying

7 26.08.09IT 60101: Lecture #127 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

8 26.08.09IT 60101: Lecture #128 Splaying Operation: Bottom Up AVL Rotations

9 26.08.09IT 60101: Lecture #129 Splaying Operation: Bottom Up AVL Rotations

10 26.08.09IT 60101: Lecture #1210 Splaying Operation: Bottom Up AVL Rotations

11 26.08.09IT 60101: Lecture #1211 Splaying Operation: Bottom Up AVL Rotations

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

13 26.08.09IT 60101: Lecture #1213 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 k 2 all the way to the root and subsequently making easy the future accessing of k 2. Unfortunately, it has pushed up another node k 1 almost as deep as k 2. Another side effect happens to a node like k 3 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.

14 26.08.09IT 60101: Lecture #1214 Splaying Operations

15 26.08.09IT 60101: Lecture #1215 Splaying Operations

16 26.08.09IT 60101: Lecture #1216 Splaying Operations

17 26.08.09IT 60101: Lecture #1217 Splaying Operations

18 26.08.09IT 60101: Lecture #1218 Splaying Operations

19 26.08.09IT 60101: Lecture #1219 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

20 26.08.09IT 60101: Lecture #1220 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

21 26.08.09IT 60101: Lecture #1221 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

22 26.08.09IT 60101: Lecture #1222 Illustration of Splaying Operation

23 26.08.09IT 60101: Lecture #1223 Illustration of Splaying Operation

24 26.08.09IT 60101: Lecture #1224 Illustration of Splaying Operation

25 26.08.09IT 60101: Lecture #1225 Illustration of Splaying Operation

26 26.08.09IT 60101: Lecture #1226 Illustration of Splaying Operation

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


Download ppt "26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII."

Similar presentations


Ads by Google