Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Finger search trees. 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2) : Assumes that.

Similar presentations


Presentation on theme: "1 Finger search trees. 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2) : Assumes that."— Presentation transcript:

1 1 Finger search trees

2 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2) : Assumes that all items in L2 are greater than all items in L1. split(x,L) : returns two lists one with all item less than or equal to x and the other with all items greater than x.

3 3 Goal (cont) In addition, we want to speed up operations near the ends of the list. Take a regular search tree and reverse the direction of the pointers on the leftmost and the rightmost spines (paths).

4 4 Finger 2-4 trees 12 31 18151420282140........

5 5 Finger 2-4 trees Start the search in parallel from the leftmost and rightmost nodes on the spines. Search for an element at distance d from one of the endpoints of the list takes O(log d) time. Insertions and deletions still take O(log n) worst case time but O(log d) amortized time (including the search time).

6 6 Finger trees catenation Like for regular trees but the search on the spine of the tall tree for a node of the same height as the small tree starts from the lowest node on the spine.

7 7 Finger trees catenation (cont) Catenation takes O(log n) on the worst-case But amortized O(1 + min{h1, h2}) = O(1 + min{log (n1), log (n2))

8 8 Splitting finger search trees x v Catenate bottom-up trees to the left of the path from v to x and to the right of the path from v to x. Obtain T1 and T2. Delete v as a child from p(v), rebalance as in deletion. Obtain T3. P (v)

9 9 Splitting finger search trees (cont) Make T1 and T2 finger trees. Fix the spine of T3. Return T1 and the tree obtained from the catenation of T2 and T3. x v T3 T2T1

10 10 Split -- analysis Split takes O(log n) worst-case. But O(log d) amortized.

11 11 Homogenous finger search trees Want to be able to get from every element to every other element in time proportional to the logarithm of the distance between them. d O(log min {d, n-d})

12 12 Homogenous finger search trees (cont) Add level links. 12 31 18151420282140........

13 13 Search Start from x and search for y. Say x < y. Keep going up until one of the following conditions holds: 1) You hit the right path of the tree 2) Your right neighbor has a key which is not smaller than y 3) You are on the left path and your neighbor on the right path has a key smaller than y Search down one or two subtrees

14 14 Search (analysis) Suppose the search went up to level h. Consider the node x reached on the way up on level h-1. The leftmost subtree of the right neighbor of x contains only items larger than x and smaller than y. ==> d  2 h-2 The rightmost subtree of the left neighbor of x contains items either smaller than x or larger than y. ==> n-d  2 h-2

15 15 2-pivot split xy Split(x,y)

16 16 2-pivot split xy Like in searching for y from x. We go up concurrently from x and y until reaching a node e which is an ancestor of both x and y; or reaching a pair of adjacent nodes e and f on the same level such that e is an ancestor of x and f is an ancestor of y. ef

17 17 2-pivot split Case 1 (f does not exists): Detach the subtree T rooted at e from its parent. Split T at x into T1 and T2. Split T2 at y into T3 and T4. Catenate T1 and T4 into T5. If the height of T5 is smaller than e then add unary nodes to make it of the same height as e. Replace e with that tree and eliminate unary nodes. If the height of T5 is larger than the height of e split it into two trees replace e with those trees and eliminate the 5-node that may have been created. Return the modified original tree and T3. xy e

18 18 2-pivot split (cont) Case 2 (f exists and is not on the right spine) : Similar to previous case. xy ef

19 19 2-pivot split Case 3 (Wrap around): Detach the subtree T rooted at e from its parent. Fix the resulting tree T’ if the parent of e becomes a 1-node. Split T at x into T1 and T2. Catenate T2 with T’. Work symmetrically with f to obtain T4 of items greater than y. Catenate T1 and T4. xy ef

20 20 2-pivot split (analysis) Recall, eliminating a 1-node or a 5-node takes O(1) amortized time == > 2-way split takes O(log min{d, n-d}) amortized time.


Download ppt "1 Finger search trees. 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2) : Assumes that."

Similar presentations


Ads by Google