Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

Scribd Upload a Document Search Documents Explore Sign Up | Log In

/ 23 Download this Document for Free

MC0068 Data Structures using (Book ID: B0701 & B0702) clrscr(); char c; int choice; do { cirscr();

printf(\n enter the choice\n); printf(1->push\n); printf(2-pop\n); scant(%d, &choice); if(choice==1) push(); elshif(choice==2) pop(); else printf(\in valid choice); printf(\n do u want to continue:?); scanf(\n%c, &c); } While{(c==y)||(c==Y)); } 3. Show the result of inserting 3, 1, 4, 5, 2, 9, 6, 8 into a a)bottom-up splay tree b) top-down splay tree. Ans:3 Splay Trees We shall describe the algorithm by giving three rewrite rules in the form of pictures. In these pictures, x is the node that was accessed (that will eventually be at the root of the tree). By looking at the local structure of the tree defined by x, xs parent, and xs grandparent we decide which of the following three rules to follow. We continue to apply the rules until x is at the root

of the tree:

MC0068 Data Structures using (Book ID: B0701 & B0702) Notes 1) Each rule has a mirror image variant, which covers all the cases. 2) The zig-zig rule is the one that distinguishes splaying from just rotating x to the root of the tree. 3) Top-down splaying is much more efficient in practice. The Basic Bottom-Up Splay Tree A technique called splaying can be used so a logarithmic amortized bound can be achieved. We use rotations such as weve seen before. The zig case. o Let X be a non-root node on the access path on which we are rotating.

o If the parent of X is the root of the tree, we merely rotate X and the root as shown in Figure 2. Figure 2 Zig Case o This is the same as a normal single rotation. The zig-zag case. o In this case, X and both a parent P and a grandparent G . X is a right child and P

is a left child (or vice versa).

MC0068 Data Structures using (Book ID: B0701 & B0702) o This is the same as a double rotation. o This is shown in Figure 3. The zig-zig case. o This is a different rotation from those we have previously seen. o Here X and P are either both left children or both right children. o The transformation is shown in Figure 4. o This is different from the rotate-to-root. Rotate-to-root rotates between

X and P and then between X and G . The zig-zig splay rotates between P and G and X and P . Figure 4 Zig-zig Case Given the rotations, consider the example in Figure 5, where we are splaying c . Top-Down Splay Trees Bottom-up splay trees are hard to implement.

We look at top-down splay trees that maintain the logarithmic amortized bound. o This is the method recommended by the inventors of splay trees. Basic idea as we descend the tree in our search for some node X , we must take the nodes that are on the access path, and move them and their subtrees out of the way. We must also perform some tree rotations to guarantee the amortized time bound.

MC0068 Data Structures using (Book ID: B0701 & B0702) At any point in the middle of a splay, we have: o The current node X that is the root of its subtree. o Tree L that stores nodes less than X . o

Tree R that stores nodes larger than X . Initially, X is the root of T , and L and R are empty. As we descend the tree two levels at a time, we encounter a pair of nodes. o Depending on whether these nodes are smaller than X or larger than X , they are placed in L

or R along with subtrees that are not on the access path to X . When we finally reach X , we can then attach L and R to the bottom of the middle tree, and as a result X will have been moved to the root. We now just have to show how the nodes are placed in the different tree. This is shown below: o Zig rotation Figure 7 Zig-Zig Figure 8

MC0068 Data Structures using (Book ID: B0701 & B0702)

Zig-Zag Figure

MC0068 Data Structures using (Book ID: B0701 & B0702) 4. Discuss the techniques for allowing a hash file to expand and shrink dynamically. What are the advantages and disadvantages of each? Ans:4 Dynamic Hashing: As the database grows over time, we have three options: 1.Choose hash function based on current file size. Get performance degradation as file grows. 2.Choose has function based on anticipated file size. Space is wasted initially. 3. Periodically re-organize hash structure as file grows. Requires selecting new hash function, recomputing all addresses and generating new bucket assignments. Some hashing techniques allow the hash function to be modified dynamically to accommodate the growth or shrinking of the database. These are called dynamic hash functions. Extendable hashing is one form of dynamic hashing. Extendable hashing splits and coalesces buckets as database size changes.

Figure 11.9: General extendable hash structure. We choose a hash function that is uniform and random that generates values over a relatively large range.

Range is b -bit binary integers (typically b=32). is over 4 billion, so we don't generate that many buckets! Instead we create buckets on demand, and do not use all b bits of the hash initially. At any point we use i bits where . The i bits are used as an offset into a table of bucket addresses. Value of i grows and shrinks with the database. Figure 11.19 shows an extendable hash structure.

MC0068 Data Structures using (Book ID: B0701 & B0702) clrscr(); char c; int choice; do { cirscr(); printf(\n enter the choice\n); printf(1->push\n); printf(2-pop\n); scant(%d, &choice); if(choice==1) push(); elshif(choice==2) pop();

else printf(\in valid choice); printf(\n do u want to continue:?); scanf(\n%c, &c); } While{(c==y)||(c==Y)); } 3. Show the result of inserting 3, 1, 4, 5, 2, 9, 6, 8 into a a)bottom-up splay tree b) top-down splay tree. Ans:3 Splay Trees We shall describe the algorithm by giving three rewrite rules in the form of pictures. In these pictures, x is the node that was accessed (that will eventually be at the root of the tree). By looking at the local structure of the tree defined by x, xs parent, and xs grandparent we decide which of the following three rules to follow. We continue to apply the rules until x is at the root of the tree:

MC0068 Data Structures using (Book ID: B0701 & B0702) Notes 1) Each rule has a mirror image variant, which covers all the cases. 2) The zig-zig rule is the one that distinguishes splaying from just rotating x to the root of the tree.

3) Top-down splaying is much more efficient in practice. The Basic Bottom-Up Splay Tree A technique called splaying can be used so a logarithmic amortized bound can be achieved. We use rotations such as weve seen before. The zig case. o Let X be a non-root node on the access path on which we are rotating. o If the parent of X is the root of the tree, we merely rotate X and the root as shown in Figure 2. Figure 2 Zig Case

o This is the same as a normal single rotation. The zig-zag case. o In this case, X and both a parent P and a grandparent G . X is a right child and P is a left child (or vice versa).

MC0068 Data Structures using (Book ID: B0701 & B0702) o This is the same as a double rotation.

o This is shown in Figure 3. The zig-zig case. o This is a different rotation from those we have previously seen. o Here X and P are either both left children or both right children. o The transformation is shown in Figure 4. o This is different from the rotate-to-root. Rotate-to-root rotates between X and P and then between X and G . The zig-zig splay rotates between

P and G and X and P . Figure 4 Zig-zig Case Given the rotations, consider the example in Figure 5, where we are splaying c . Top-Down Splay Trees Bottom-up splay trees are hard to implement. We look at top-down splay trees that maintain the logarithmic amortized bound. o This is the method recommended by the inventors of splay trees. Basic idea as we descend the tree in our search for some node X , we must take the nodes that are on the access path, and move them and their subtrees out of the way. We

must also perform some tree rotations to guarantee the amortized time bound.

MC0068 Data Structures using (Book ID: B0701 & B0702) At any point in the middle of a splay, we have: o The current node X that is the root of its subtree. o Tree L that stores nodes less than X . o Tree R that stores nodes larger than X . Initially, X

is the root of T , and L and R are empty. As we descend the tree two levels at a time, we encounter a pair of nodes. o Depending on whether these nodes are smaller than X or larger than X , they are placed in L or R along with subtrees that are not on the access path to X . When we finally reach X , we can then attach

L and R to the bottom of the middle tree, and as a result X will have been moved to the root. We now just have to show how the nodes are placed in the different tree. This is shown below: o Zig rotation Figure 7 Zig-Zig Figure 8

MC0068 Data Structures using (Book ID: B0701 & B0702) Zig-Zag Figure

MC0068 Data Structures using (Book ID: B0701 & B0702) 4. Discuss the techniques for allowing a hash file to expand and shrink dynamically. What are the advantages and disadvantages of each? Ans:4 Dynamic Hashing:

As the database grows over time, we have three options: 1.Choose hash function based on current file size. Get performance degradation as file grows. 2.Choose has function based on anticipated file size. Space is wasted initially. 3. Periodically re-organize hash structure as file grows. Requires selecting new hash function, recomputing all addresses and generating new bucket assignments. Some hashing techniques allow the hash function to be modified dynamically to accommodate the growth or shrinking of the database. These are called dynamic hash functions. Extendable hashing is one form of dynamic hashing. Extendable hashing splits and coalesces buckets as database size changes.

Figure 11.9: General extendable hash structure. We choose a hash function that is uniform and random that generates values over a relatively large range. Range is b -bit binary integers (typically b=32). is over 4 billion, so we don't generate that many buckets! Instead we create buckets on demand, and do not use all

b bits of the hash initially. At any point we use i bits where . The i bits are used as an offset into a table of bucket addresses. Value of i grows and shrinks with the database. Figure 11.19 shows an extendable hash structure.

MCOO68 SMU MCA SEM2 2011 Download this Document for Free Print

Mobile Collections Report Document

Info and Rating mcoo68 smu mca sem2 2011

Follow

Nitin Sivach

Share & Embed Related Documents Previous Next p. p. p. p. p. p. p.

p. p. p. p. p. p. p. p. p. p. p.

More from this user Previous Next 35 p. 23 p. 23 p. 28 p. 9 p. 22 p. 31 p. 16 p. 30 p. 38 p.

22 p.

Add a Comment

Upload a Document Search Documents Follow Us! scribd.com/scribd twitter.com/scribd facebook.com/scribd About Press Blog Partners Scribd 101 Web Stuff Support FAQ Developers / API Jobs Terms Copyright Privacy

Copyright 2011 Scribd Inc. Language: English

You might also like