Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf ·...

32
Lecture Notes CS6301 Programming & Data Structures II Unit IV Page 1 UNIT IV-ADVANCED NON-LINEAR DATA STRUCTURES AVL trees B-Trees Red-Black trees Splay trees - Binomial Heaps Fibonacci Heaps Disjoint Sets Amortized Analysis accounting method potential method aggregate analysis. Define Tree. List its features. o A tree is a collection of one or more nodes such that: o A specially designated node r called the root. o Remaining nodes are partitioned into n 0 subtrees T 1 , T 2 ..., T n each of whose roots are connected by a directed edge to r o Every node except the root has one parent. o Children of the same parent are siblings. o Nodes that have no children are called leaf or terminal node. o A path from node n1 to nk is defined as a sequence of nodes n1, n2, . . . , nk such that ni is the parent of n i+1 for 1 i < k. In a tree there is exactly one path from the root to each node. o Height of a tree is equal to the height of the root. List the properties of a binary tree. o A binary tree is a tree in which no node can have more than two children namely left child and right child. The maximum number of nodes of depth k is 2 k -1 o In a proper binary tree every node has zero or two children. o In a perfect binary tree all leaves are at the same depth. o A binary tree may is complete if all leaves are at depth n or n-1 for some n. o Three modes of binary tree traversal are: o Preorder traversal o Inorder traversal o Postorder traversal Briefly explain binary search tree. o A binary tree is a binary search tree if for every node X, in the tree: o values of all keys in the left subtree are smaller than the key value in X, o values of all the keys in the right subtree are larger than the key value in X. Smartzworld.com Smartworld.asia jntuworldupdates.org Specworld.in

Transcript of Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf ·...

Page 1: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 1

UNIT IV-ADVANCED NON-LINEAR DATA STRUCTURES

AVL trees – B-Trees – Red-Black trees – Splay trees - Binomial Heaps – Fibonacci Heaps –

Disjoint Sets – Amortized Analysis – accounting method – potential method – aggregate

analysis.

Define Tree. List its features.

o A tree is a collection of one or more nodes such that:

o A specially designated node r called the root.

o Remaining nodes are partitioned into n • 0 subtrees T1, T2..., Tn each of whoseroots are connected by a directed edge to r

o Every node except the root has one parent.

o Children of the same parent are siblings.

o Nodes that have no children are called leaf or terminal node.

o A path from node n1 to nk is defined as a sequence of nodes n1, n2, . . . , nk such that ni

is the parent of ni+1 for 1 ” i < k. In a tree there is exactly one path from the root to

each node.

o Height of a tree is equal to the height of the root.

List the properties of a binary tree.

o A binary tree is a tree in which no node can have more than two children namely left

child and right child. The maximum number of nodes of depth k is 2k-1

o In a proper binary tree every node has zero or two children.

o In a perfect binary tree all leaves are at the same depth.o A binary tree may is complete if all leaves are at depth n or n-1 for some n.

o Three modes of binary tree traversal are:

o Preorder traversal

o Inorder traversalo Postorder traversal

Briefly explain binary search tree. o A binary tree is a binary search tree if for every node X, in the tree:

o values of all keys in the left subtree are smaller than the key value in X,

o values of all the keys in the right subtree are larger than the key value in X.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 2: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 2

o Find operation returns a pointer to the node in tree T that has key X, or NULL if

there is no such node.

o To insert X into tree T, proceed down the tree as with a Find. If X is found, do

nothing. Otherwise, insert X at the last spot on the path traversed.

o For deletion, once the node to be deleted is found, the possibilities to be examined

are leaf node, node with one child and node with two child.

Explain insert operation on AVL Tree using an example.

o An AVL tree is a binary search tree with a balance condition. It is named after its

founder Adelson-Velskii and Landis

o An AVL tree is identical to a binary search tree, except that for every node in the tree,

the height of the left and right subtrees can differ by at most 1.

o Balancing Factor = Height of Left subtree – Height of Right subtree

o For root and leaf node, it is 0.

o It should be either 0 or 1 for any node in an AVL tree.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 3: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 3

o All the tree operations can be performed in O(log N) time, except insertion.

o Inserting a node could violate the AVL tree property. The four cases are:

1) An insertion into the left subtree of the left child

2) An insertion into the right subtree of the left child

3) An insertion into the left subtree of the right child

4) An insertion into the right subtree of the right child

o If so, then it is restored with a simple transformation to the tree, known as a rotation.

o Cases 1 and 4 are fixed by a single rotation of the tree.

o Cases 2 and 3 are handled by double rotation.

o A rotation involves only a few pointer changes, and changes the structure of the tree

while preserving the search tree property.

Single Rotation

AVL Tree before insertion AVL Tree after single rotation

o When insert to left of X happens (case 1), node k2 violates the AVL balance propert y

because its left subtree is two levels deeper than its right subtree.

o To rebalance, X is moved up one level and Z one level down, i.e., the nodes are

rearranged into an equivalent tree.

o Thus k1 becomes the new root. According to binary search tree property since k2>k1, k2 becomes right child of k1 in the new tree.

o X and Z remain as the left child of k1 and right child of k2. Subtree Y is placed as k2

left child in the new tree to satisfy the ordering requirements.

o The new height of the entire subtree is the same as the original subtree. The above

transformation is known as single rotation.

o In figure given below, when insert to right of Z happens (case 4), node k1 violates the

AVL balance property. This is fixed by a similar single rotation

Double Rotation o The single rotation does not fix the problem if insertion is done either onto right

subtree of the left child or onto left subtree of the right child.

o When insertion is done to right of A (case 3), imbalance is fixed using double rotation

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 4: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 4

o When insertion is done to left of D (case 2), imbalance is fixed using double rotation

Illustrate insertion of keys 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8 onto an empty AVL

tree. Show the various rotation involved.

o Insert key 1 into empty AVL tree.

o Insert key 2 to the right of 1, since 2 > 1.

o Problem occurs when inserting key 3, because AVL property is violated at the root.

Single rotation is performed to fix the problem.

o Inserting key 4 causes no problems, but insertion of 5 creates a violation at node 3,

which is fixed by a single rotation.

o Inserting 6 causes a balance problem for the root, since its left subtree is of height 0,

and its right subtree would be height 2. Therefore a single rotation is done.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 5: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 5

o Next key 7 is inserted that results in imbalance at node 5, thereby another rotation.

o Inserting 15 does not destroy the balance property.

o Inserting 14 causes a height imbalance at node 7. The right-left double rotation is

used. Here, k3 is the node 7, k1 is the node 15 and k2 is node 14. Subtrees are empty.

o Inserting 13 requires a double rotation. In this case, k3 is the node 6, k1 is node 14 and

k2 is the node 7. Subtree A is the tree rooted at node 5, subtree B is the empty, subtree

C is the tree at node 13 and subtree D is the node with 15.

o Inserting 12 creates an imbalance at the root. A single rotation will restore the tree.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 6: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 6

o Insertion of 11 requires another single rotation.

o Inserting 10, forces a single rotation, and the same is true for insertion of 9.

o Finally, 8 is inserted to produce the final tree.

Give the pseudocode for insertion into an AVL tree.

AvlTree Insert(ElementType X, AvlTree T)

{

if(T == NULL)

{

T = malloc(sizeof(struct AvlNode));

T->Element = X; T->Height = 0;

T->Left = T->Right = NULL;

}

else

if(X < T->Element)

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 7: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 7

{

T->Left = Insert(X, T->Left);

if(Height(T->Left) - Height(T->Right) == 2)

if(X < T->Left->Element)

T = SingleRotateWithLeft(T);

else

}

T = DoubleRotateWithLeft(T);

else if(X > T->Element)

{

T->Right = Insert(X, T->Right);

if(Height(T->Right) - Height(T->Left) == 2)

if(X > T->Right->Element)

T = SingleRotateWithRight(T);

else

}

T = DoubleRotateWithRight(T);

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 8: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 8

T->Height=Max(Height(T->Left), Height(T->Right))+1;

return T;

}

// Single Rotation

static Position SingleRotateWithLeft(Position K2)

{

Position K1;

K1 = K2->Left;

K2->Left = K1->Right;

K1->Right = K2;

K2->Height=Max(Height(K2->Left),Height(K2->Right))+1;

K1->Height=Max(Height(K1->Left), K2->Height) + 1;

return K1;

}

static Position SingleRotateWithRight(Position K1)

{

Position K2;

K2 = K1->Right;

K1->Right = K2->Left;

K2->Left = K1;

K1->Height=Max(Height(K1->Left),Height(K1->Right))+1;

K2->Height = Max(Height(K2->Right), K1->Height) + 1;

return K2;

}

// Double Rotation

static Position DoubleRotateWithLeft(Position K3)

{

K3->Left = SingleRotateWithRight(K3->Left);

return SingleRotateWithLeft(K3);

}

static Position DoubleRotateWithRight(Position K1)

{

K1->Right = SingleRotateWithLeft(K1->Right);

return SingleRotateWithRight(K1);

}

Explain access using Splay Trees with an example.

o Splay trees are unbalanced binary trees.

o A Splay tree guarantees for M consecutive tree operations take at most O(M log N).

o After a node is accessed, it is pushed to the root by a series of AVL tree rotations.

Thus whenever an object is accessed, it becomes the new root.

o If a node is deep and there are many nodes on the path that are also relatively deep, by

restructuring (splaying) future accesses is cheaper on all these nodes.

o In many applications when a node is accessed, it is likely to be accessed again in the

near future. Using splay trees, consecutive access for the same node is of no cost.

o Splay trees do not require the maintenance of height or balance information, thus

saving space and simplifies the code.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 9: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 9

Splaying o The strategy is to rotate bottom up along the access path.

o Let X be a (non-root) node on the access path.

o If the parent of X is root of the tree, then rotate X and the root. This is the last rotation

along access path.

o Otherwise, X has both a parent (P) and a grandparent (G), with two cases.

o If X is a right child and P is a left child (or vice versa). If so perform a double

rotation. This is known as zig-zag case.

o Otherwise, X and P are either both left or right children. In that case,

transform tree on the left to tree on the right. This is known as zig-zig case.

Before Splaying Zig-Zag rotation

Before Splaying Zig-Zig rotation

Example o Nodes with keys 1, 2, … 7 are inserted into initial empty tree.

o Splaying at the node with key 1 is shown below. Node with key 1 is pushed to the

root, through a series of zig-zig rotations.

o Access of node with key 1 takes N units, access on node with key 2 takes only N/2

units instead of another N units.

o Access on the node with item 2 will bring nodes to within N/4 of the root, and this is

repeated until the depth becomes roughly log N.

o Splaying not only moves the accessed node to the root, but also has the effect of

roughly halving the depth of most nodes on the access path.

o When access paths are long, leading to a longer-than-normal search time, the rotations

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 10: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 10

tend to be good. When accesses are cheap, rotations are bad.

o Recursive implementation does not work for splay trees and goes through two passes.

The first pass goes down the tree and the second goes back up, performing rotations.

Thus the path needs to be saved.

o Analysis of splay trees is difficult, because it must take into account the ever-

changing structure of the tree.

o Splay trees are simpler to program than AVL trees, since there are fewer cases to

consider and no information to maintain.

How is deletion done in splay trees? o When a node is accessed for deletion, it becomes the new root. Thus the root would

be deleted.

o If it is deleted, then two subtrees results namely TL and TR. The new root is

determined as follows:

o Find the largest element in TL, then this element is rotated to root of TL.

o TR is made the right child of TL.

What are B-Trees? List its properties.

o B-tree or a balanced M-ary tree is not a binary tree. It allows M-way branching.

o As number of branches increases, depth of the tree decreases. The height is reduced to

logM N.

o A B-tree of order M is a tree that has the following properties:

1. Root is either a leaf or has between 2 and M children.

2. Non-leaf nodes (except the root) have between [M/2] and M children.

3. Non-leaf nodes store upto M-1 keys to facilitate searching. Key i represents the

smallest key in subtree i+1.

4. Leaf nodes are of same depth and have between [L/2] and L children for any L.

5. Data is stored only at the leaves.

o Each intermediate node contains pointers P1, P2, . . . , Pm to children, and values k1, k2,

. . . , km-1, represent the smallest key found in the subtrees.

o Some pointers might be NULL, if the corresponding ki is undefined.

o For every node, all keys in subtree P1 are less than the keys in subtree P2, and so on.

o A B-tree of order 4 is popularly known as a 2-3-4 tree, and a B-tree of order 3 is

known as 2-3 tree.

o B-Trees are widely used for database implementation.

B-tree of order 4

Explain insertion in B-Trees using an example. o A Find operation on data item X, starts at the root and do recursively until leaf node.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 11: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 11

o If X less than key Ki, then traverse the left subtree.

o If X greater than or equal to key Ki, then traverse the right subtree.

o To Insert X, follow the path as though performing a Find. If X does not exist:

o Find leads to a leaf node where X is to be inserted.

o If number of elements in leaf is less than L, then insert X and reorganize datain the leaf node.

o If leaf node has L data items, then split the leaf nodes, with each containing at

least L/2 data items. The parent gains a child.

ƒ If the parent has less than M keys, then adjust the

pointers.

ƒ If parent has M keys, then split the parent each having (M+1)/2 keys.

x Check whether this increase is accepted at the upper level.

x Repeat this until a parent is found with less than M children.

x If the root is split, then create a new root with two children

o Initial 2-3 B-tree is shown below (M = L = 3). Non-leaf nodes are represented as

ellipses, leaves are shown as boxes, which contain the keys.

o To insert a node with key 18, a Find operation is done.

o Since 18 < 22, left subtree of the root is explored.o Since 18 > 16, right subtree of pointer 16 is explored

o It leads to leaf which has two elements.o Since a leaf can have maximum 3 elements, 18 is added to the leaf.

o Next insert 1.

o Since 1 < 22, traverse left subtree. Since 1 < 16, traverse left subtree.

o It leads to a leaf which already has 3 elements (max L = 3). Insertion at that

leaf violates the property of 2-3 B-Tree.

o Therefore it is solved by splitting leaf into two nodes each with two data items

and adjusting the pointers in the parent as shown.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 12: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 12

o Next insert 19.

o The path leads to right leaf node of pointer 16, which already has 3 elements.

o Therefore leaf node is split into two leaf nodes as shown

o Thus the internal node with four children, but only three per node is allowed.

o Therefore the intermediate node is split into two nodes, each with two children

as shown.

o Next insert element 28,

o It leads to a leaf with four children as shown.

o Splitting the intermediate pointer node, leads to 4 children at intermediate

level. This once again violates the B-Tree property.

o Therefore the root is split into two nodes and a new root is created. Thus

height of the tree increases.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 13: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 13

o When the parent is split, update values of the keys and also the parent’s parent, thus

incurring additional disk writes.

o Splitting nodes is time consuming, but it is a relatively rare occurrence for large B-

Trees.

Explain deletion in B-Trees using an example. o Deletion can be performed b y finding the data item to be deleted and then removing it

o If the leaf it was in had only L/2 data items, then after deletion the leaf has less thanL/2 data items. This violates the B-Tree property.

o It is solved by adopting a neighboring data item, if the neighbor is not itself at its

minimum.

o Otherwise, combine with the neighbor to form a full leaf. This leads to a child loss for

the parent.

o If the parent falls below the minimum, then proceed as above, until it reaches the root.

o If the root loses its second child, then the root is also deleted and the tree becomes one

level shallower.

o Thus B-Tree could lose height as a result of deletion.

Example

o Consider the above B-Tree of order 5, wherein each leaf node should have at least

three data items.

o To remove data 99, perform Find operation for 99.

o It leads to a leaf having only three data items, i.e., minimum number of

elements.

o Deleting 99 leads to the leaf having only two children. This violates the B-

Tree property.

o Since the leaf has only two items and its neighbor is already at its minimum of

three, combine the items into a new leaf of ¿ve items.

o But now, its parent loses a child and has only two children, which is violation

of B-Tree property.

o This is solved by adopting from a neighbor, because the neighbor has four

children. Thus both have three children as shown.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 14: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 14

Give an analysis of operations of B-Tree and its usage. o The depth of a B-tree is at most log [M/2] N. At each node on the path, perform O(log

M) work to determine which branch to take (using a binary search),

o An Insert or Delete require O(M) work to fix up all the information at the node.

o The worst-case running time for each of the Insert and Delete operations is thus O(M

logM N), but a find takes only O(log N).

o As M gets larger, the insertion and deletion times increase. If main memory speed is a

concern, higher order B-trees are not an advantageo Real use of B-trees lies in

database systems, where the tree is kept on a physical disk instead of main memory.

o Accessing a disk is typically several orders of magnitude slower than any main

memory operation.

o If a B-tree of order M is used, then the number of disk accesses is O(logM N).

o Although each disk access carries the overhead of O(log M) to determine the direction

to branch, the time to perform this computation is typically much smaller than the

time to read a block of memory and can thus be considered inconsequential.

What is an equivalence relation? Give an example.

o An equivalence relation is a relation R that satis¿es three properties:

o Reflexive: a R a, for all a א S.

o Symmetric: a R b if and only if b R a.

o Transitive: a R b and b R c implies that a R c.

o Electrical connectivity is an example.

Define disjoint set.

o Disjoint set is a collection of sets S1, S2,…, Sn, such that:

o All relations except reflexive are false

o Elements in each set are different, i.e., Si ŀ Sj = o Initially each set contains only one element

o Each set is identified by a member of the set, called representative.

List the operations on Disjoint Set.

o Two operations on disjoint sets are find and union.

o Find operation returns representative of the set

o Union operation merges two set into a new set, Sk = Si Sj by destroying the originals

and preserving the disjointness of all sets. o Since find is applied before union, the algorithm is known as union/find algorithm.

Give the basic data structure of disjoint set.

o Each set is explicitly represented as a tree. Therefore disjoint set is a collection of

trees known as forest.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 15: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 15

o Name / representative of the set is the root node for that set.

o Implicitly disjoint set is represented as an array.

o Each entry s[i] in the array represents parent for element i

o If i is a root, then s[i] = í1

-1 -1 -1 -1 -1 -1 -1 -1

0 1 2 3 4 5 6 7

Explain simple union / find algorithm on sets using an example.

o To perform union of two sets, merge the two trees by making the parent link of one

tree’s root link to the root node of the other tree.

o After union of sets x and y, root of union is x.

Example o union(4, 5) results in merger of sets 4 and 5 as a single tree with root as 4.

o union(6, 7) results in merger of sets 6 and 7 as a single tree with root as 6.

o union(4, 6) results in merger of sets 4 and 6 as a single tree with root as 4.

o Array representation after the last union is shown below.

o A find(x) on element x is performed by returning the root of the tree containing x.

o Simple union/find algorithm takes quadratic running time, hence inefficient.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 16: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Srividya College of Engg & Tech Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 16

Pseudocode

// union routine

void unionSets(int root1, int root2)

{

s[root2] = root1;

}

// find routine

int find(int x)

{

if(s[x] < 0)

return x;

else

} return

find(s[x])

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 17: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 17

Explain smart union algorithms on disjoint sets. o Improvements to simple union algorithm on disjoint sets are:

o Union-by-sizeo Union-by-height

1. union-by-size

o In union-by-size, the smaller tree is made subtree of the larger tree.

o Initially all array entries are -1.

o Array entry of each root is negative of the size of its tree.

o Easy to implement and requires no extra space,

o If unions are done by size, the depth of any node is never exceeds log N.

o Running time for find operation is O(log N) and sequence of M operations is O(M).

2. union-by-height

o Height is maintained instead of size for each tree.

o In union-by-height shallow tree is made subtree of the deeper tree.

o Height of a tree increases only when two equally deep trees are joined.

o Initially all array entries are -1.

o Array entry of each root is negative of height, minus an additional 1.

o Depth of all trees is at most O(log N).

Example

o Union (3,4) results in the same forest for both by size and height.

o Array entries for union-by-size is:

o Array entries for union-by-height is:

Pseudocode

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 18: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 18

// union-by-height

void unionSets(int root1, int root2)

{

if(s[root2] < s[root1]) // root2 is deeper

s[root1] = root2;

else

{

if(s[root1] == s[root2])

--s[root1];

s[root2] = root1;

}

}

What is path compression on disjoint sets? Explain find operation using path compression.

o Worst case of all union/find algorithm results in O(M log N).

o Running time is better by improvising the find operation, known as path compression.

o Path compression for node x results in every node on the path from x to the root has

its parent changed to root.

o Effect of path compression after find(14) results in the following tree.

o nodes 12 and 13 are one position closer to the root

o nodes 14 and 15 are two positions closer.

o After the root of the set is found recursively, x’s parent link references it. This occurs

recursively to every node on the path to the root. The pseudocode is:

// find with path compression

int find(int x)

{

if(s[x] < 0)

return x;

else

}

return s[x] = find(s[x]);

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 19: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 19

o Sequence of M operations requires at most O(M log N) time.

o Path compression is compatible with union-by-size but not with union-by-height.o Hence instead of height, estimated height known as rank is stored. The resulting

algorithm is known as union-by-rank.

Define binary heap. What is heap-order property? o A heap is a complete binary tree.

o It is represented easily using an array.

o A heap-order property means the heap tree possess the following properties:

o Root contains the smallest element

o Each element should be smaller than the descendant, i.e., parent(X) < X

Briefly explain insert and delete operations on binary heap with an example.

insert o To insert an element X into the heap, we create a hole in the next available location.

o If X can be placed in the hole without violating heap order, insertion is done.o Otherwise percolate up the hole and slide the parent down. Repeat the process until X

can be placed in the hole.

o For example, to insert 14, a hole is created.

o Since 31 > 14, 31 is slide down and hole is moved up o

Since 21 > 14, 21 is slide down and hole is moved up o Eventually 14 is placed in the hole

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 20: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 20

deleteMin

o Minimum element of the tree, i.e., root is removed. This creates a hole at the root. The

heap becomes one element smaller.

o Slide up smaller of hole's children as root and the hole is percolate down. Repeat the

process until hole becomes a leaf element.

o Last element is placed in the hole, and hole is eventually removed from the tree.

o For example, deleteMin, removes the root 13. A hole is created. o

Since 14 < 16, 14 becomes the root and hole slides down o

Since 19 < 21, 19 moves up and hole slides down

o Since 26 < 65, 26 moves up and hole becomes the leaf.

o Last element 31 is placed in the hole.

List some operations supported by heap.

o insertʊinserts an element from the heap

o deleteMinʊremoves root element from the heap

o decreaseKeyʊlowers the value of the item at position p by a positive amount

o increaseKeyʊincreases the value of the item at position p by a positive amount

o removeʊ removes the node at position p from the heap

What is binomial heap? Give an example o Binomial heap is a collection of heap-ordered trees, also known as Binomial Queue.

o Each of the heap-ordered trees is of a constrained form known as a binomial tree.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 21: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 21

o A binomial tree of height 0 is a one-node tree

o Binomial tree, Bk, contains 2k

number of nodes with height k. It is formed by

making one Bk-1 tree as subtree of another Bk-1 tree.

Binomial Heap H = <110> = {B1, B2 }

Explain merge operation on binomial heap using an example. o Place all binomial trees of distinct height amongst both heaps onto the merged heap.

o Merge binomial trees in both heaps that are of same height b y making tree with larger

root as subtree of the tree with smaller root.

o Place the resultant tree onto merged heap.

o If the merged heap contains trees of same height then repeat the merge.o Eventually the merged heap is a collection of binomial trees of distinct height.

o Merge operation takes O(log N) time in the worst case.

Example o Consider merging of two binomial heaps H1 and H2 onto new heap H3.

Binomial Heap H1 Binomial Heap H2

o Since H1 has no binomial tree of height 0 and H2 has one, it is placed onto H3.

o Both H1 and H2 have binomial trees of height 1, tree with root 16 is made subtree of

tree with root 14. The merged tree is placed onto H3.

o Merged heap H3, at this stage looks like:

Merged Heap H3

o Heaps H1, H2 and H3 have trees of height 2. Trees with root 12 and 14 (first and

second minimum) are merged. This results in a tree of height 3 and placed in H3.

o Tree with root 23 in H2 of height 2 is placed in H3. Merge process is complete.

Final Merged Heap H3

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 22: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 22

Explain insert operation on binomial heap using an example.

o Insertion is a special case of merging.

o Create a single node tree.

o Perform merge on an existing tree, if it results in a binomial tree.

o Repeat merge if heap contains binomial tree of same height.

o Worst-case time of insert operation is O(log N).

Example

o Consider the process of inserting keys 1 to 7 as shown.

o 1 is inserted into the empty heap.

o 2 is inserted as subtree of 1

o 3 is inserted as a single tree

o 4 is inserted as a subtree of 3. Two B1 trees exist. Therefore they are merged.

o 5 is inserted as a single tree.

o 6 is inserted as a subtree of 5.

o 7 is inserted as a single tree.

After 1 is inserted After 2 is inserted After 3 is inserted After 4 is inserted

After 5 is inserted After 6 is inserted After 7 is inserted

Explain deleteMin operation on binomial heap using an example.

o In the given heap H, binomial tree with the smallest root, say Bk is found.

o Create heap H' by removing tree Bk from heap H.

o Create heap H'' by considering subtrees of tree with root Bk by removing Bk.

o Merge heaps H' and H''.

o deleteMin operation takes O(log N) time.

Example o Perform deleteMin on the following heap.

o Tree with minimum root is 12.

o Heap H' is formed by removing entire tree with root 12 from H.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 23: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 23

Binomial Heap H'

o Heap H'' consists of binomial trees after removing root from tree with root 12.

Binomial Heap H''

o Merge heaps H' and H'' to form the final heap is shown below.

Final Merged Heap

Give the linked list representation for binomial heap.

o Each node in a binomial tree will contain the data, pointer to first child and right

sibling.

o Children in a binomial tree are arranged in decreasing rank.

o Linked list representation for the above binomial heap is

List the properties of fibonacci heap. Give an example. o A fibonacci heap is a collection of heap-ordered trees. Each tree is rooted but

unordered.

o Each node x, has pointers p[x] to its parent and child[x] to one of its children.

o Children of a node are linked together in a doubly-linked circular list.

o Each node also has degree degree[x], indicating the number of children it has.

o Each node also has a boolean field mark[x], indicating whether it has lost a child since the

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 24: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 24

last time it was made the child of another node

o Entire heap is accessed by a pointer min, which points to the minimum-key root

List the operations of fibonacci trees. o findMinʊAccess element pointed by min pointer.

o insertʊinsert a node as a singleton tree onto heap

o deleteMinʊRemove min element and form a new heap minimum after consolidation

o decreaseKeyʊDecrease key value and create a new tree if heap property is violated

o unionʊmerger of two fibonacci heaps and update min pointer if required.

Explain insert operation on fibonacci heap with an example.

o To insert node with key value x:

o Create a new singleton tree with key x

o Add it to root listo Update min pointer if required

o Increment the total number of nodes in the Heap

Insert 21

Explain deletemin operation on fibonacci heap with an example. o Find minimum of heap is easy, it is obtained from min pointer.

o Remove element pointed by min from the heap.o Meld its children onto root list

o Update min pointero Consolidate trees, so that no two roots have the same rank, i.e., make tree with larger

root as subtree of the tree with smaller root.

o If multiple trees are of same rank, then consolidate trees whose roots are first and

second minimum and repeat the process for trees of that rank.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 25: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 25

o To perform deleteMin on the heap, 3 pointed by min is the element to be deleted.

o Its child subtrees are melded and made part of root list.

o min pointer is updated to pointed the new minimum amongst root list.

o In the newly formed heap, there are trees of the same rank, i.e., height. Hence trees of

same rank are consolidated and min pointer is updated, until all are of distinct rank.

o Trees with root 23 and 17 are of same rank, i.e., 0. Hence they are consolidated as a

single tree. Now there is only one tree of rank 0.

o Trees with root 7 and 17 are of rank 1. Hence they are consolidated.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 26: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 26

o Trees with root 7 and 24 are of rank 2. Hence they are consolidated.

o Next trees with root 18 and 41 are of rank 1. Hence they are consolidated.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 27: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 27

o Now all trees have distinct rank. The consolidation process is over.

Explain decreaseKey operation on fibonacci heap using an example. o Decrease value of node to given value x. If heap tree order is not violated then stop.

o Otherwise, cut tree rooted at x and meld into root list. If x is marked, then unmark it.

o Perform a cascading cut on x’s parent y, if parent is marked. Cut y, recursively on

node y’s parent and so on until a root, or an unmarked node is found.

o Update min if required.

o For example, decrease the value of node from 46 to 29. The heap-order property is

maintained. Hence the process is over.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 28: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 28

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 29: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Srividya College of Engg & Tech Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 29

o Next, decrease key from 29 to 15.

o Results in violation of heap-order property.

o Hence subtree 29 is cut from the tree and added to root list with 29 as root.

o Parent 24 is marked.

o Next, decrease key from 35 to 5

o Results in violation of heap-order property.

o Node with key 5 is cut and added to root list as a tree with root as 5.

o Pointer min is updated to point to tree with root 5.

o It's parent 26 is marked already. Hence cascading cut is done recursively.

ƒ Node with key 26 is cut and added to root list as a separate tree.

ƒ Parent of 26, i.e node 24 is marked already. Hence it is also cut

ƒ Parent of 24 is root, therefore recursive process is stopped.

How is union of fibonacci heaps done?

o Roots are also maintained as circular doubly linked list.

o Heaps are linked and min pointer updated if necessary

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 30: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

. Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 30

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 31: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 31

What is the use of mark field in fibonacci heap? o Each node contains a boolean field mark[x].

o Indicates whether node x has lost a child since the last time x was made the child of

another one

o Newly created nodes are unmarked

o A node x becomes unmarked whenever it is made the child of another node.

Give value for variables maintained for the given fibonacci heap.

o marks( H) = 3 (number of marked nodes in heap H)

o trees(H) = 5 (number of trees in heap H)

o rank(H) = 3 (max rank of any node in heap H)o n = 14 (number of nodes in heap)

o min = 3 (tree with minimum key root)

o Potential of Heap ) (H) = trees(H) + 2 × marks(H) = 11

What is Red-Black tree? List its properties.

o Red-black tree is an alternative to AVL tree.

o A red-black tree is a binary search tree with the following coloring properties:

o Every node is colored either red or black.

o Root is black.o If a node is red, then both of its children must be black.

o Every path from a node to a null pointer must contain the same number ofblack nodes.

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in

Page 32: Srividya Cllege of Engg & Tech Question Bankstudyparadise.in/admin/upload/tech/cplus/Unit-4.pdf · o A Splay tree guarantees for M consecutive tree operations take at most O(M log

Srividya College of Engg & Tech Lecture Notes

CS6301 Programming & Data Structures II Unit IV Page 32

Red-Black tree (red nodes are double circled)

Smartzworld.com Smartworld.asia

jntuworldupdates.org Specworld.in