selfAdjustSearchTrees - n"i hmgto Unuemts mSt Lows...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 12
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 14
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 16
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ' n "i . hmgto Unuemts mSt Lows Engineering Self-Adjusting Search Trees Jon Turner Computer Science & Engineering Washington University www.ar1.wustl.edu/~jst E Washington University in St. Louis Engineering Self-Adjusting Binary Trees I Self-adjusting binary search trees restructure search tree after each operation >> requires no explicit balance condition >> m operations take O(m log n) time >> in some contexts provides better overall performance than balanced binary trees - Restructuring operation is called splay >> moves one vertex X to root of the tree by a sequence of rotations >> the rotations reduce depth of each vertex u, by roughly half the original depth of the nearest common ancestor ofx and u E Washington University in St. Louis Engineering Illustration of Splay Steps Splaystepoo grandparent and grandparent and X is leftf/eft 9 X is right-left 9 no grandparent grandChl/d grandchild and x is left child A A” A ii rrotate(z) rrotate(y) rrotate()/) lrotate(z) 0 A 9 0 — Engineering Implementation of Splay E Washington University in St. Louis sset function splay(item x); if X = null 2 return null fi; do p(X) :2 null 2 splaystep(x) od; return X; end; procedure splaystep(item X); Iast step item VIZ} of splay if p(X) = null 2 return fi; y := p(X); if p(y) = null and x = left(y) 2 rrotate(y); return; I p(y) = null and X = right(y) 2 |rotate(y); return; fi' 2 := p(y); each moves if X = /eft(/eft(z)) 2 rrotate(z); rrotate(y) descendants | x = right(right(z)) 2 |rotate(z); |rotate(y) ofX up 1 | x = /eft(right(z)) 2 rrotate(y); |rotate(z) | X = right(left(z)) 2 |rotate(y); rrotate(z) fi; end; :1,- Mshmgton UniverSItymStlouis Engineering Review Questions 1. Consider the middle splay step on page 3. What effect does this have on the depth of subtrees A and B? What effect does it have on the depth of subtrees C and D? 2. Consider a binary search tree with nodes a, b, c, d, e, f, g, h, i, where a is the left child of b, b is the left child of c, c is the left child of d, and so forth (so, i is the root of the tree). Show the result of doing a splay at node a. . For each node in the previous example, write down its depth in the tree, before and after the splay? What is the largest decrease in depth? What is the largest increase? Which nodes experience the largest decrease? What is the largest depth after the splay? $Msh1ngton UanCFSIthSthUIS Engineering Implementing Self-Adjusting BSTs item function access(keytype k, sset s); ifs = null 2 return null fi; do k < key(s) and /eft(s) e null => 5 := /eft(s) | k > key(s) and right(s) 7: null => 5 := right(s) od; 5 := splay(s); time bounded by number of I ey(s) re urn s, splay Steps I k ¢ key(s) => return null; fi; end; [sset, sset] function split(item i, sset s); splay(i); 51,52 == left(i),right(i); p(51),p(52) == null; Ieft(i), right(i) := null; return [51,52]; end; $Msh1ngton UnlverSItymStlouls Engineering procedure insert(item i, sset 5); item X; X := 5; do key(i) < key(x) and /eft(x) 7: null 3 x := /eft(x) | key(i) > key(x) and right(x) :e null 2 x := right(x) od; if key(i) < key(x) => /eft(x) := i; | key(i) > key(x) => right(x) := i; fl, 190') := X; splay(i); end, time bounded by number of splay steps ~v Washmgton Unwersm 1n Stloum Engineering procedure delete(item i, sset 5); item j; if /eft(i) a: null and right(i) 2 null 2 j := left(i); do rightU) ¢ null 2j := rightU) od; swapplaces(i,j); fi; if /eft(i) = null 2 /eft(i) <—> right(i) fi; p(/eft(i)) := p(i); if i = /eft(p(i)) 2 /eft(p(i)) := /eft(i) I i = right(p(i)) 2 right(p(i)) := /eft(i) fi; splay(p(i)); Ieft(i),right(i),p(i) := null; by number 0f time bounded end splay steps :1,- Washmgton UniverSItymStlouis Engineering Analysis of Self-Adjusting BSTs .Objective is to show that sequence of m operations, on trees with total of n nodes takes 0(m log n) time I Use a credit scheme to account for running time >> all operations but join include a splay, so can account for their running time by bounding the time for all splays >> splay time is proportional to number of splay steps, so account for running time of splay by “spending” one credit per step >> allocate up to C lg n credits per splay and perjoin (C to be determined) >> credits not needed to pay for performing an operation are “stored” in the tree and can be used to “pay” for later steps ITO ensure we have enough credits on hand to pay for future operations, maintain the credit invariant >> keep rank(x) credits on hand for each node X, where rank(x)=llg(# of descendants of X)j $Wash1ngton UnwersuymStlouls Engineering - Balanced trees need fewer credits than unbalanced trees, so splays generally “release” credits lLemma 4.2. Splaying a tree with root v at a node u while maintaining credit invariant requires at most 3 (rank(v)—rank(u))+1 new credits Proof. The credits are divided among the different splay steps >>a splay step at node X with parent y and grandparent z is allocated 3 (rank(z)—rank(x)) credits >>a splay step at a node X with a parent y but no grandparent is 3(rank (y)—rank(x))+ 1 Let rank and rank’ be the rank functions before and after the step >> Case 1. X has no grandparent: this is last step, and extra credit pays for it; number of credits needed to maintain invariant is (ran/(’(x) - rank(x)) + (rank’(y) - rank(y)) = rank’(y) - rank(x) s rank(y) — rank(x) which is one third of the available credits In :1,- Washmgton UnwersuymStlouls Engineering >> Case 2. X=left(/eft(z)) or X=right(right(z)): if rank(z)=rank(x)=k we get no new credits for this step, but rank’(z)<k, so we can spend an existing credit while still maintaining the invariant if rank(z)>rank(x), the number of credits needed to maintain the invariant is (rank'(X)-rank(x)) + (rank’(y)-rank(y)) + (rank’(z)-rank(z)) = rank’( y) + rank’(z) - rank(x) - rank(y) s 2(rank(z) — rank(x)) < 3(rank(z) — rank(x)) releasing at least one extra credit to pay for the step >> Case 3. X=Ieft(right(z)) or X=right(/eft(z)); if rank(z)=rank(x)=k we get no new credits for this step, but either rank’(z)<k or rank’(y)<k , so we can spend a credit while maintaining invariant if rank(z)>rank(x), the number of credits needed to maintain the invariant is (rank’(X)—rank(x)) + (rank’(y)—rank(y)) + (rank’(z)—rank(z)) = rank’(y) + rank’(z) — rank(x) — rank(y) s 2(rank(z) - rank(x)) < 3(rank(z) - rank(x)) releasing at least one extra credit to pay for the step I II $Msh1ngton UnwersuymStlouls Engineering I Lemma implies each splay requires s3l|g nj + 1 credits >> number of credits needed for an insert is this number plus the number of new credits needed to maintain the credit invariant, after the new item is inserted but before the splay is done >> the only nodes whose ranks can increase are those on the path from the root to the inserted node that have exactly 2k—1 descendants before the operation (where kE[O..L|g nJ]) >> there can be at most [lg n] + 1 of these, so the total number of credits required for an insert is at most 4 [lg n] + 2 IThe join operations requires at most [lg n] credits IA” other operations require no credits beyond those used by the splay l Theorem 4.1. The total time required for a sequence of m sorted set operations on n vertices, using self- adjusting binary search trees is 0(m log n) 1’) E \X/ashington University in St. Louis Exercises 1. Show the tree that results from performing a splay at node e in the self-adjusting BST shown below. What is the largest increase in depth of any node? What is the largest decrease? — Engineering The new tree appears below The largest increase in depth is 2 (all nodes in the subtree of d see an increase in depth of 2). The largest decrease is for node 9, which goes from depth 4 to 0. I? E Washington University in St. Louis 2. Prove the following statement. Let u be a descendant of x in a self-adjusting BST. If a splay is performed at x, then D(u)sd(u)—(d(x)/2)+1, where d(u) is the depth of u before the splay and D (u) is the depth of u after the splay (recall that the depth of a node in a tree is the length of the path from the root). If we look at the definition of a splay step on slide 3, we observe that each splay step except possibly the last, moves the descendants of the “splay node” at least one step closer to the root. The splay node moves two steps closer. So if d(x)=2k, there are k splay steps that reduce move u at least one step closer to the root, so D(u)sd(u)— ksd(u)—(d(x)/2)+1 The above inequality is also true if d(x) =2k+1. Now, suppose that z is the nearest- common ancestor of x and u and that a splay is performed at X. Show that, — Engineering D(u)sd(u)—(d(z)/2)+3 We ’/I assume that u is not a descendant of x, since that case has already been covered earlier. Now, let’s first consider the effect of the splay on the depth of 2. As long as x remains a descendant of z, the splay steps at x have no effect on the depth of z. The first splay step that makes 2 a descendant of x increases the depth of 2 by up to 2. Subsequent splay steps (except possibly the last) each decrease the depth of z by 1. Therefore, D(z)sd(z)—(d(z)/2)+3 Since u is a descendant of z, the splay steps will affect u in essentially the same way as z. The splay step that makes 2 a descendant of u can increase the depth ofu by 2, but each subsequent step (except possibly the last) decreases its depth by 1. So, D(u)sd(u)—(d(z)/2)+3. 1/! — Engineering E \X/ashington University in St. Louis 3. Consider the self-adjusting BST in problem 1. How many credits are needed to satisfy the credit invariant before the splay at node e? Leaves in the tree require no credits. Nodes with a total of2 or 3 descendants require 1, nodes with 4 to 7 require 2 and nodes with 8 to 15 require 3. So, in the tree from the previous problem, we need a total of 13. How many are needed after the splay? 14 How many credits were needed to pay for the splay? 2 How many new credits are allocated to the splay in the analysis? Node 9 has a rank of 0 initially and the tree root has a rank of 3, so 10 credits are allocated. How many “surplus credits” does this leave us with? 10— ((14-13)+2)=7 I: E \X/ashington University in St. Louis 4.Consider a highly unbalanced BST where each non-root node is the right child of its parent and there are n=15 nodes altogether. How many credits are needed to satisfy the credit invariant? 2 nodes require 1 credit, 4 nodes require 2 credits, 8 require 3. This gives us 2+8+24=34. How many are needed after a splay at the one leaf node? The final tree has x at the root and all other nodes in its left subtree. Each internal node in this left subtree has a leaf as its left child. In this tree one node has a rank of 1, two have a rank of2 and five have a rank of 3. The rest are leaves with rank 0. So the total number of credits needed is 1+4+15=20, a reduction of 13. — Engineering How many credits were needed to pay for the splay? There are 7 splay steps, so 7 credits are needed to pay for the splay. How many new credits are allocated to the splay in the analysis? 10 credits are allocated. How many “surplus credits” does this leave us with? 10— ((20—34)+8)=16 IK ...
View Full Document

This note was uploaded on 03/08/2012 for the course CSE 542 taught by Professor Jonturner during the Spring '12 term at Washington State University .

Page1 / 16

selfAdjustSearchTrees - n"i hmgto Unuemts mSt Lows...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online