ms0068 2 sem

download ms0068 2 sem

of 9

Transcript of ms0068 2 sem

  • 8/3/2019 ms0068 2 sem

    1/9

    2. Illustrate the C program to represents the Stack Implementation on POP and PUSH

    operation.

    Ans: 2

    #include#include#define Max 5Int Staff [Max], top=-1;Void display(){If ((top==-1 || (top==0)){Printf(\n stack is full\n);}Else{

    Printf(\n stack elements are\n);For(int i=top-1;i>=0;i--)}}void push(){int ele;char ch;it(top-=-1)top=0;do{If (top>=5){Printf(\n stack is full);Break;}Else{Clrscr();Printf(\n enter the element to be insearted\n);Scanf(%d, &ele);Staff(top++)=eledisplay();}Printf(\n Do u want to add more elements:?\n);Scanf(\n%c, &ch);}While ((ch==y ||(ch==Y));}void pop(){If ((top==-1)||(top==0)

  • 8/3/2019 ms0068 2 sem

    2/9

    {Printf(\nstack is under flow\n);}Else{Printf(%d is deleted fro stack\n,Staff(--top]);

    display();}}

    Void main(){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();elseprintf(\in valid choice);printf(\n do u want to continue:?);scanf(\n%c, &c);

    }While{(c==y)||(c==Y));}

  • 8/3/2019 ms0068 2 sem

    3/9

    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). Bylooking 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:

    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 ofthe 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 beachieved.

    We use rotations such as weve seen before.

    Thezigcase.

    LetXbe a non-root node on the access path on which we are rotating.

    If the parent ofX is the root of the tree, we merely rotate Xand the root as shown in

    Figure 2.

    http://train-srv.manipalu.com/wpress/wp-content/uploads/2009/06/clip-image00288.jpg
  • 8/3/2019 ms0068 2 sem

    4/9

    Figure 2 Zig Case

    This is the same as a normal single rotation.

    Thezig-zagcase.

    In this case,Xand both a parentPand a grandparent G.Xis a right child andPis a left

    child (or vice versa).

    This is the same as a double rotation.

    This is shown in Figure 3.

    Thezig-zigcase.

    This is a different rotation from those we have previously seen.

    HereXandPare either both left children or both right children.

    The transformation is shown in Figure 4.

    This is different from the rotate-to-root. Rotate-to-root rotates between XandPand then

    betweenXand G. The zig-zig splay rotates betweenPand G andXandP.

  • 8/3/2019 ms0068 2 sem

    5/9

    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.

    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.

    At any point in the middle of a splay, we have: The current node X that is the root of its subtree.

    Tree L that stores nodes less than X.

    Tree R that stores nodes larger than X.

    Initially, X is the root ofT, and L and R are empty.

    As we descend the tree two levels at a time, we encounter a pair of nodes.

    Depending on whether these nodes are smaller than X or larger than X, they are placed in

    L orR 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 shownbelow:

  • 8/3/2019 ms0068 2 sem

    6/9

    Zig-Zig Figure

    Zig-Zag Figure

  • 8/3/2019 ms0068 2 sem

    7/9

    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 accommodatethe 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 asdatabase 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 ibits where . The ibits are used as an offset into a table of bucket addresses. Value ofigrows and shrinks with the database. Figure 11.19 shows an extendable hash structure.

  • 8/3/2019 ms0068 2 sem

    8/9

    Note that the iappearing over the bucket address table tells how many bits are requiredto determine the correct bucket.

    It may be the case that several entries point to the same bucket. All such entries will have a common hash prefix, but the length of this prefix may be less

    than i. So we give each bucket an integer giving the length of the common hash prefix.

    This is shown in Figure 11.9 (textbook 11.19) as .

    Number of bucket entries pointing to bucket j is then .

    To find the bucket containing search key value :

    Compute .

    Take the first ihigh order bits of . Look at the corresponding table entry for this i-bit string. Follow the bucket pointer in the table entry.

    We now look at insertions in an extendable hashing scheme.

    Follow the same procedure for lookup, ending up in some bucket j. If there is room in the bucket, insert information and insert record in the file. If the bucket is full, we must split the bucket, and redistribute the records. If bucket is split we may need to increase the number of bits we use in the hash.

    Two cases exist:

    1. If , then only one entry in the bucket address table points to bucket j.

    Then we need to increase the size of the bucket address table so that we can includepointers to the two buckets that result from splitting bucket j.

    We increment iby one, thus considering more of the hash, and doubling the size of thebucket address table.

    Each entry is replaced by two entries, each containing original value. Now two entries in bucket address table point to bucket j. We allocate a new bucket z, and set the second pointer to point to z.

    Set and to i. Rehash all records in bucket jwhich are put in eitherjorz. Now insert new record. It is remotely possible, but unlikely, that the new hash will still put all of the records in

    one bucket. If so, split again and increment iagain.

    2. If , then more than one entry in the bucket address table points to bucket j.

    Then we can split bucket jwithout increasing the size of the bucket address table(why?).

  • 8/3/2019 ms0068 2 sem

    9/9

    Note that all entries that point to bucket jcorrespond to hash prefixes that have the same

    value on the leftmost bits.

    We allocate a new bucket z, and set and to the original value plus 1. Now adjust entries in the bucket address table that previously pointed to bucket j. Leave the first half pointing to bucket j, and make the rest point to bucket z. Rehash each record in bucket jas before. Reattempt new insert.

    Note that in both cases we only need to rehash records in bucket j.Deletion of records is similar. Buckets may have to be coalesced, and bucket address table mayhave to be halved.Insertion is illustrated for the example deposit file of Figure 11.20.

    32-bit hash values on bname are shown in Figure 11.21. An initial empty hash structure is shown in Figure 11.22. We insert records one by one. We (unrealistically) assume that a bucket can only hold 2 records, in order to illustrate

    both situations described. As we insert the Perry ridge and Round Hill records, this first bucket becomes full. When we insert the next record (Downtown), we must split the bucket.

    Since , we need to increase the number of bits we use from the hash. We now use 1 bit, allowing us buckets. This makes us double the size of the bucket address table to two entries. We split the bucket, placing the records whose search key hash begins with 1 in the new

    bucket, and those with a 0 in the old bucket (Figure 11.23). Next we attempt to insert the Redwood record, and find it hashes to 1.

    That bucket is full, and . So we must split that bucket, increasing the number of bits we must use to 2. This necessitates doubling the bucket address table again to four entries (Figure 11.24). We rehash the entries in the old bucket. We continue on for the deposit records of Figure 11.20, obtaining the extendable hash

    structure of Figure 11.25.

    Advantages:

    Extendable hashing provides performance that does not degrade as the file grows. Minimal space overhead - no buckets need be reserved for future use. Bucket address

    table only contains one pointer for each hash value of current prefix length.

    Disadvantages:

    Extra level of indirection in the bucket address table.