You are on page 1of 67

http://placementkit.blogspot.

com/

Coding skills
Question 1
Marks: 1
The subject of these questions is an unusually simple kind of binary tree, defined by these
properties:
Terminal nodes contain a string.
Internal nodes have one or two children, called "left" and "right".
Either child of an internal node may be null, but not both.
Internal nodes contain no other information.
By "tree" we simply mean a node and all of its descendants.
A tree rooted at a node having left child A and right child B is a different tree than one rooted at
a node having left child B and right child A.
Here's an example, with plus signs (+) used to indicate internal nodes:
+
/\
/ \
/

\

+

+

/

/\

/

/ \

/

/

"A"

+
/\
/ \

\
"D"

http://placementkit.blogspot.com/

/
"B"

\
"C"

class InternalNode extends Node {
Node left, right;
}
What constructors could InternalNode have according to the specifications for the tree structure
(potentially in addition to others)?

InternalNode()

InternalNode(String)

InternalNode(Node)

Choose one answer.
a. B only
b. A or C but not B
c. none of them are legitimate possibilities
d. C only
Question 2
Marks: 1
Given the following code snippet answer the following question.
struct AVLTree
{
AVLTree * left;
AVLTree * right;

http://placementkit.blogspot.com/

int element;
int height;
};
int MAX(int a, int b){
if(a>=b)
return a;
if(a<b)
return b;
}
int height(AVLTree *node)
{
if (node == NULL)
{
return -1;
}
else
{
return node->height;
}
}
AVLTree * single_rotation_with_left(AVLTree *k2)
{
AVLTree *k1;

} AVLTree * single_rotation_with_right(AVLTree *k2) { AVLTree *k1.http://placementkit. return k1. return single_rotation_with_left(k3). k2->left = k1->right. } . } AVLTree *double_rotation_with_left(AVLTree *k3) { k3->left = single_rotation_with_right(k3->left). k1 = k2->right. k1->height = MAX(height(k1->left).com/ k1 = k2->left. k2->right = k1->left. k2->height = MAX(height(k2->left). return k1. height(k2->left)) + 1. height(k2->right)) + 1. k2->height = MAX(height(k2->left). k1->height = MAX(height(k1->right). k1->left = k2.blogspot. height(k2->right)) + 1. k1->right = k2. height(k2->right)) + 1.

} void insert(int value. if (height((*node)->left) .com/ AVLTree *double_rotation_with_right(AVLTree *k3) { k3->right = single_rotation_with_left(k3->right). return single_rotation_with_right(k3).blogspot. (*node)->left = (*node)->right = NULL. if (*node == NULL) { return. &((*node)->left)).http://placementkit. } (*node)->element = value. AVLTree **node) { if (*node == NULL) { *node = new AVLTree.height((*node)->right) == 2) . (*node)->height = 0. } else if (value < (*node)->element) { insert(value. return.

height((*node)->left) == 2) { if (value > (*node)->right->element) { *node = single_rotation_with_right(*node). . } else { *node = double_rotation_with_left(*node). } } } else if (value > (*node)->element) { insert(value.com/ { if (value < (*node)->left->element) { *node = single_rotation_with_left(*node). if (height((*node)->right) . &((*node)->right)).blogspot. } else { *node = double_rotation_with_right(*node).http://placementkit.

http://placementkit.blogspot.com/

}
}
}

(*node)->height = MAX(height((*node)->left), height((*node)->right)) + 1;
}
Consider an input sequence that is provided as an input to the insert method
20,5,15,9,13,2,6,12,14,15,16,17,18,19
In the process of inserting the above nodes how many times double_rotation_with_left is being
called?
Choose one answer.
a. 0
b. 5
c. 3
d. 2
Question 3
Marks: 1
Following is the array representation of a Binary search tree:

Consider that the binary tree specified above id given as input to the following function.
int numofleaves(tNode *p)
{
if (p==0)
return 0;
if (p->left==0 && p->right ==0)
return 1;
else

http://placementkit.blogspot.com/

return numofleaves(p->right)+numofleaves(p->left);
}
Choose one answer.
a. 10
b. 9
c. 4
d. 8
Question 4
Marks: 1
Following is the array representation of a Binary search tree:

Consider that the binary tree specified above id given as input to the following function.
int func1(tNode *p)
{
if (p!=0)
return func1 (p->left) + func1 (p->right) + 1;
else
return 0;
}
Choose one answer.
a. 10
b. 9
c. 4
d. None of these
Question 5
Marks: 1
Following is the array representation of a Binary search tree:

http://placementkit.blogspot.com/

What would be the output of the following code snippet for the above mentioned binary search
tree?
void func() {
std::queue q;
q.push(root);
while( q.size() != NULL ) {
Node *cur = q.front();
std::cout << cur->data << std::endl;
q.pop();
if (cur->left) q.push(cur->left);
if (cur->right) q.push(cur->right);
}
}
Choose one answer.
a. 5 10 15 20 78 98 99 100 105 110
b. 99 20 100 10 78 105 5 15 98 110
c. 99 20 10 5 15 78 98 100 105 110
d. 5 15 10 98 78 20 110 105 100 99
Question 6
Marks: 1
The subject of these questions is an unusually simple kind of binary tree, defined by these
properties:




Terminal nodes contain a string.
Internal nodes have one or two children, called "left" and "right".
Either child of an internal node may be null, but not both.
Internal nodes contain no other information.
By "tree" we simply mean a node and all of its descendants.

(Java’s String.blogspot. Here's an example.compareTo follows the same comparison rules as described here. with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / "A" + \ "D" /\ / \ / "B" \ "C" Consider the following proposed implementation of TerminalNode. abstract boolean compareTo(Node).com/  A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. } class TerminalNode extends Node { .http://placementkit.) abstract class Node { abstract boolean isTerminal().compareTo.

the code would correctly handle all possible values of nd. and if fixed. b. and if fixed. d. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Either child of an internal node may be null.blogspot. but the code does not correctly handle all possible values of nd. a. Internal nodes contain no other information. There is a compilation error.http://placementkit. There is no compilation error. There is no compilation error. There is a compilation error.isTerminal()) return -1. but not both. . } boolean compareTo(Node nd) { if (!nd. defined by these properties:       Terminal nodes contain a string. boolean isTerminal() { return true. the code would not correctly handle all possible values of nd Question 7 Marks: 1 The subject of these questions is an unusually simple kind of binary tree. does this code work for all possible values of nd? Choose one answer.value). By "tree" we simply mean a node and all of its descendants. c. called "left" and "right". return value. and the code does correctly handle all possible values of nd.compareTo(nd. } }   Is there a compilation error? If there is no compilation error or the compilation error were fixed.com/ String value. Internal nodes have one or two children.

if (0 != result) return result. class InternalNode : extends Node { int compareTo(Node nd) { if (null == nd) return 1.compareTo(ind.com/ Here's an example. . if (nd. // . int result = left.compareTo. . with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / "A" + \ "D" /\ / \ / "B" \ "C" Here is part of a correct implementation of InternalNode.left). InternalNode ind = (InternalNode) nd.blogspot.http://placementkit. .isTerminal()) return 1.

Either child of an internal node may be null. Neither left nor right are null b.left is non-null or right is null and ind. called "left" and "right". with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ .left is non-null Question 8 Marks: 1 The subject of these questions is an unusually simple kind of binary tree. either: • left is non-null or • left is null and ind. Internal nodes have one or two children. but not both. defined by these properties:       Terminal nodes contain a string.http://placementkit. By "tree" we simply mean a node and all of its descendants.com/ return right. right.blogspot.right is non-null c. left is null and either: nd. Here's an example. None of left. ind. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A.compareTo(ind. Internal nodes contain no other information.right). a.right are null d. } } Which of the following set of tests would be both a sufficient and necessary part for the comment for the method to execute without error and return a correct result? (Assume that the tests return a correct result from the function before the above code is executed if that is possible.left. or ind.) Choose one answer.

A only Question 9 Marks: 1 The subject of these questions is an unusually simple kind of binary tree.com/ / / \ / / "A" + \ "D" /\ / \ / "B" \ "C" Under which of the following conditions would InternalNode tree1 be “less than” InternalNode tree2 for the most “natural” interpretation of “less than”? A. Either child of an internal node may be null. C d.left and tree1.left is equal to tree2. defined by these properties:       Terminal nodes contain a string.left B. Internal nodes have one or two children. tree1. . either tree1.left is less than tree2. Internal nodes contain no other information.left or tree1.left is less than tree2.blogspot.right Choose one answer.right C. but not C b. B or C c. but not both. By "tree" we simply mean a node and all of its descendants.right is less than tree2. A or B.right is less than tree2. called "left" and "right".http://placementkit. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. tree1. a.

isTerminal()) return 1.com/ Here's an example. InternalNode ind = (InternalNode) nd.left). if (nd.left) return -1.blogspot.compareTo. class InternalNode : extends Node { int compareTo(Node nd) { if (null == nd) return 1. if (null == left && null != ind. int result = left.http://placementkit. with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / "A" + \ "D" /\ / \ / "B" \ "C" Here is a proposed implementation of InternalNode. .compareTo7195(ind.

left are both null B. B and C can occur and are not handled correctly by the implementation. int element. int MAX(int a.blogspot. AVLTree * right. right and ind. int b){ . A either cannot occur or is handled by the implementation. }.http://placementkit.com/ return result != 0 ? result : right. } } Given the specifications for the tree structure which conditions could occur that are not handled correctly in the above code? A. Only C can occur and is not handled correctly by the implementation b. left and ind.right). left is non-null and ind.compareTo7195(ind. c. Only A can occur and is not handled correctly by the implementation.left is null Choose one answer.right are both null C. Only B can occur and is not handled correctly by the implementation. int height. struct AVLTree { AVLTree * left. a. Question 10 Marks: 1 Given the following code snippet answer the following question. d.

com/ if(a>=b) return a. k1->right = k2. if(a<b) return b. . } else { return node->height. k2->height = MAX(height(k2->left).blogspot.http://placementkit. k2->left = k1->right. } int height(AVLTree *node) { if (node == NULL) { return -1. } } AVLTree * single_rotation_with_left(AVLTree *k2) { AVLTree *k1. height(k2->right)) + 1. k1 = k2->left.

k1 = k2->right. k1->height = MAX(height(k1->right).com/ k1->height = MAX(height(k1->left). height(k2->left)) + 1. return k1. } AVLTree *double_rotation_with_right(AVLTree *k3) { k3->right = single_rotation_with_left(k3->right). return k1.http://placementkit. } AVLTree * single_rotation_with_right(AVLTree *k2) { AVLTree *k1. k1->left = k2.blogspot. } AVLTree *double_rotation_with_left(AVLTree *k3) { k3->left = single_rotation_with_right(k3->left). height(k2->right)) + 1. height(k2->right)) + 1. return single_rotation_with_right(k3). . return single_rotation_with_left(k3). k2->height = MAX(height(k2->left). k2->right = k1->left.

} (*node)->element = value. (*node)->left = (*node)->right = NULL. if (*node == NULL) { return. if (height((*node)->left) .blogspot. .com/ } void insert(int value. } else if (value < (*node)->element) { insert(value. (*node)->height = 0.http://placementkit.height((*node)->right) == 2) { if (value < (*node)->left->element) { *node = single_rotation_with_left(*node). return. AVLTree **node) { if (*node == NULL) { *node = new AVLTree. &((*node)->left)).

if (height((*node)->right) .height((*node)->left) == 2) { if (value > (*node)->right->element) { *node = single_rotation_with_right(*node).com/ } else { *node = double_rotation_with_left(*node). } } } . } } } else if (value > (*node)->element) { insert(value. &((*node)->right)). } else { *node = double_rotation_with_right(*node).http://placementkit.blogspot.

17.18.15.com/ (*node)->height = MAX(height((*node)->left). else return 0. } 1. 5 b.19 Let's give the root node of the resulting tree as input to the following code snippet int func(AVLTree **p) { if (*p!=0) return func (&(*p)->left). 2 Question 11 Marks: 1 Given the following code snippet answer the following question.15. } What would be the return value after execution of the above code snippet? Choose one answer.5. a.2. 3 c. 0 d. struct AVLTree { .12. Consider an input sequence that is provided as an input to the insert method 20.13. height((*node)->right)) + 1.6.14.http://placementkit.blogspot.16.9.

if(a<b) return b.com/ AVLTree * left. int MAX(int a. } int height(AVLTree *node) { if (node == NULL) { return -1. } else { return node->height. int b){ if(a>=b) return a. } } AVLTree * single_rotation_with_left(AVLTree *k2) . }. AVLTree * right.http://placementkit. int element. int height.blogspot.

. } AVLTree * single_rotation_with_right(AVLTree *k2) { AVLTree *k1. k2->height = MAX(height(k2->left). height(k2->left)) + 1. height(k2->right)) + 1. k1->height = MAX(height(k1->left). return k1. k2->right = k1->left. height(k2->right)) + 1. k1->right = k2. return k1. k1 = k2->right. k1->left = k2.com/ { AVLTree *k1. k2->left = k1->right. k2->height = MAX(height(k2->left).blogspot. k1 = k2->left. height(k2->right)) + 1. } AVLTree *double_rotation_with_left(AVLTree *k3) { k3->left = single_rotation_with_right(k3->left). k1->height = MAX(height(k1->right).http://placementkit.

return single_rotation_with_right(k3). (*node)->height = 0.blogspot.com/ return single_rotation_with_left(k3).http://placementkit. AVLTree **node) { if (*node == NULL) { *node = new AVLTree. } AVLTree *double_rotation_with_right(AVLTree *k3) { k3->right = single_rotation_with_left(k3->right). return. } void insert(int value. (*node)->left = (*node)->right = NULL. } (*node)->element = value. } else if (value < (*node)->element) { . if (*node == NULL) { return.

height((*node)->right) == 2) { if (value < (*node)->left->element) { *node = single_rotation_with_left(*node).http://placementkit.blogspot. } else { *node = double_rotation_with_left(*node).height((*node)->left) == 2) { if (value > (*node)->right->element) { *node = single_rotation_with_right(*node). } } } else if (value > (*node)->element) { insert(value.com/ insert(value. &((*node)->right)). } else . if (height((*node)->left) . if (height((*node)->right) . &((*node)->left)).

12.18. } 1.12.5.http://placementkit.15.6.13. } Consider an input sequence that is provided as an input to the insert method 20.com/ { *node = double_rotation_with_right(*node).17.19 Let's give the root node of the resulting tree as input to the following code snippet int func(AVLTree **p) .13.6.5.2.19 Let's give the root node of the resulting tree as input to the following code snippet int func(AVLTree **p) { if (*p!=0) return func (&(*p)->left).16.16.15.17.14.9.9. } } } (*node)->height = MAX(height((*node)->left). Consider an input sequence that is provided as an input to the insert method 20.blogspot.15. else return 0.2.14.18.15. height((*node)->right)) + 1.

6 d. called "left" and "right".com/ { if (*p!=0) return func (&(*p)->left) + 1. } What would be the return value after execution of the above code snippet? Choose one answer. 7 c. else return 0. with plus signs (+) used to indicate internal nodes: . Internal nodes contain no other information. a.http://placementkit. By "tree" we simply mean a node and all of its descendants.blogspot. 3 Question 12 Marks: 1 The subject of these questions is an unusually simple kind of binary tree. defined by these properties: Terminal nodes contain a string. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. 4 b. Internal nodes have one or two children. Either child of an internal node may be null. Here's an example. but not both.

B .com/ + /\ / \ / \ + + / /\ / / \ / / "A" + \ "D" /\ / \ / "B" \ "C" abstract class Node { } What constructors should Node have according to the specifications for the tree structure? Node(Node) Node(String) Choose one answer.blogspot. a. None of the above c.http://placementkit. Both b.

blogspot.com/ d. AVLTree * right. int height. if(a<b) return b. int MAX(int a. } else . struct AVLTree { AVLTree * left. int element. } int height(AVLTree *node) { if (node == NULL) { return -1.http://placementkit. int b){ if(a>=b) return a. A Question 13 Marks: 1 Given the following code snippet answer the following question. }.

height(k2->left)) + 1.com/ { return node->height. k2->left = k1->right. k1->height = MAX(height(k1->right). return k1. .blogspot. k2->right = k1->left. k1 = k2->left. } } AVLTree * single_rotation_with_left(AVLTree *k2) { AVLTree *k1. height(k2->right)) + 1. k1->height = MAX(height(k1->left). k2->height = MAX(height(k2->left). k1->right = k2. } AVLTree * single_rotation_with_right(AVLTree *k2) { AVLTree *k1. k1 = k2->right. k2->height = MAX(height(k2->left).http://placementkit. height(k2->right)) + 1. height(k2->right)) + 1. k1->left = k2.

} AVLTree *double_rotation_with_right(AVLTree *k3) { k3->right = single_rotation_with_left(k3->right).http://placementkit. AVLTree **node) { if (*node == NULL) { *node = new AVLTree. if (*node == NULL) { return. return single_rotation_with_right(k3). (*node)->height = 0. } void insert(int value. return single_rotation_with_left(k3).blogspot. .com/ return k1. } (*node)->element = value. } AVLTree *double_rotation_with_left(AVLTree *k3) { k3->left = single_rotation_with_right(k3->left).

} else { *node = double_rotation_with_left(*node). &((*node)->left)).com/ (*node)->left = (*node)->right = NULL. if (height((*node)->right) .height((*node)->left) == 2) { .http://placementkit. &((*node)->right)). return. } } } else if (value > (*node)->element) { insert(value.blogspot.height((*node)->right) == 2) { if (value < (*node)->left->element) { *node = single_rotation_with_left(*node). if (height((*node)->left) . } else if (value < (*node)->element) { insert(value.

17.6.15.19 Let's give the root node of the resulting tree as input to the following code snippet int func(AVLTree **p) { if (*p!=0) return func (&(*p)->left). } else { *node = double_rotation_with_right(*node).blogspot.com/ if (value > (*node)->right->element) { *node = single_rotation_with_right(*node).14.13.9.http://placementkit. else .15.12. } 1.16.5. } } } (*node)->height = MAX(height((*node)->left).2. Consider an input sequence that is provided as an input to the insert method 20. height((*node)->right)) + 1.18.

16. 9 d. By "tree" we simply mean a node and all of its descendants.com/ return 0.17. . but not both. } Consider an input sequence that is provided as an input to the insert method 20.9. 13 c. a.15. 10 Question 14 Marks: 1 The subject of these questions is an unusually simple kind of binary tree.5. defined by these properties: Terminal nodes contain a string. called "left" and "right".blogspot. Internal nodes have one or two children. Either child of an internal node may be null.http://placementkit.6.15.18. 8 b.12. else return 0.14. } What would be the return value after execution of the above code snippet? Choose one answer.13.19 Let's give the root node of the resulting tree as input to the following code snippet int numofnode(AVLTree **p) { if ((*p!=0)&&((*p)->element != 13)) return numofnode (&(*p)->right) + numofnode (&(*p)->left) + 1. Internal nodes contain no other information.2.

right. right = r. InternalNode(node l. } \ "C" . node r) { left = l. Here's an example.blogspot. with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / "A" + \ "D" /\ / \ / "B" class InternalNode extends Node { Node left.com/ A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A.http://placementkit.

d. c. Either child of an internal node may be null. a. Here's an example.http://placementkit. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. l cannot be null even if r is not null. Question 15 Marks: 1 The subject of these questions is an unusually simple kind of binary tree. Neither can be null. b. Internal nodes contain no other information. but not both. By "tree" we simply mean a node and all of its descendants. defined by these properties: Terminal nodes contain a string. The answer cannot be deduced from the specifications. Either can be null but not both. called "left" and "right".com/ } Which of the constructor’s arguments can be null according the specifications of the tree structure? Choose one answer. with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ . Internal nodes have one or two children.blogspot.

. B and C but not A Question 16 Marks: 1 Given a code snippet answer the following question.blogspot. C only d. b. All of them would be acceptable.com/ / / \ / / "A" + \ "D" /\ / \ / "B" \ "C" What constructors could TerminalNode define consistent with the specifications? TerminalNode() TerminalNode(Node) TerminalNode(String) Choose one answer. A and C but not B c. struct AVLTree { AVLTree * left. a.http://placementkit.

} } AVLTree * single_rotation_with_left(AVLTree *k2)//Func1 { . }.blogspot. if(a<b) return b.http://placementkit. int b){ if(a>=b) return a.com/ AVLTree * right. int MAX(int a. int element. } else { return node->height. } int height(AVLTree *node) { if (node == NULL) { return -1. int height.

return k1.blogspot. k1 = k2->right. . k1->height = MAX(height(k1->right). k2->left = k1->right. return single_rotation_with_left(k3). k2->right = k1->left. height(k2->right)) + 1. height(k2->right)) + 1. k1->height = MAX(height(k1->left). k2->height = MAX(height(k2->left). height(k2->right)) + 1. k1 = k2->left. return k1. k2->height = MAX(height(k2->left). } AVLTree *double_rotation_with_left(AVLTree *k3) { k3->left = single_rotation_with_right(k3->left). k1->right = k2. height(k2->left)) + 1.http://placementkit.com/ AVLTree *k1. k1->left = k2. } AVLTree * single_rotation_with_right(AVLTree *k2)//Func2 { AVLTree *k1.

} else if (value < (*node)->element) { . AVLTree **node) { if (*node == NULL) { *node = new AVLTree. } void insert(int value.http://placementkit.blogspot. return. } (*node)->element = value.com/ } AVLTree *double_rotation_with_right(AVLTree *k3) { k3->right = single_rotation_with_left(k3->right). (*node)->height = 0. return single_rotation_with_right(k3). if (*node == NULL) { return. (*node)->left = (*node)->right = NULL.

height((*node)->left) == 2) { if (value > (*node)->right->element) { *node = single_rotation_with_right(*node).height((*node)->right) == 2) { if (value < (*node)->left->element) { *node = single_rotation_with_left(*node). if (height((*node)->right) . &((*node)->left)). if (height((*node)->left) . &((*node)->right)). } else .com/ insert(value. } else { *node = double_rotation_with_left(*node). } } } else if (value > (*node)->element) { insert(value.blogspot.http://placementkit.

} } . height((*node)->right)) + 1. } else { return searchmin(node->left).com/ { *node = double_rotation_with_right(*node). } else if (node->left == NULL) { return node.blogspot.http://placementkit. } } } (*node)->height = MAX(height((*node)->left). } AVLTree *searchmin(AVLTree *node) { if (node == NULL) { return NULL.

http://placementkit.int i){ while((*node!=NULL)&&((*node)->element != i)){ parent = node. } else { return searchmax(node->right). if((*node)->element >i) *node = (*node)->left. } else if (node->right == NULL) { return node.AVLTree **node. } } void Search(AVLTree **parent.blogspot.com/ AVLTree *searchmax(AVLTree *node) { if (node == NULL) { return NULL. . else *node = (*node)->right.

.com/ } } AVLTree * del(int value.height((*node)->left) >= 2) { if ((*node)->left && (value < (*node)->left->element)) { (*node) = double_rotation_with_right((*node)). &((*node)->left)). AVLTree **node) { AVLTree * x.http://placementkit. } else { (*node) = single_rotation_with_right((*node)). if (*node ==NULL) return NULL. AVLTree *tmp_cell.blogspot. if (height((*node)->right) . if (value < (*node)->element) { (*node)->left = del(value.

height((*node)->right) >= 2) { if ((*node)->right && (value > (*node)->right->element)) { (*node) = double_rotation_with_left((*node)).blogspot. } else { (*node) = single_rotation_with_left((*node)). } } } else if ((*node)->left && (*node)->right) { tmp_cell = searchmin((*node)->right). . if (height((*node)->left) .com/ } } } else if (value > (*node)->element) { (*node)->right = del(value.http://placementkit. &((*node)->right)).

blogspot. else return 0. &((*node)->right)). } return (*node). free(tmp_cell).com/ (*node)->element = tmp_cell->element. } . (*node)->right = del((*node)->element. else if ((*node)->right == NULL) (*node) = (*node)->left.http://placementkit. } else { tmp_cell = (*node). tmp_cell = NULL. } int numofnode(AVLTree **p) { if ((*p!=0)&&((*p)->element != 13)) return numofnode (&(*p)->right) + numofnode (&(*p)->left) + 1. if ((*node)->left == NULL) (*node) = (*node)->right.

6 Question 17 Marks: 1 Given a code snippet answer the following question.96 The resulting tree is given as input to the following code snippet int nol(AVLNode **p) { if (*p==0) return 0.10.5. 7 c.http://placementkit.30.blogspot. AVLTree * right.50.100. if ((*p)->left==0 && (*p)->right ==0) return 1. 14 d. . a.com/ Consider an input sequence that is provided as an input to the insert method 20. else return (nol(&(*p)->right)+nol(&(*p)->left) + 1). struct AVLTree { AVLTree * left. int element.90. 8 b.45. } Which one of the following would be the output of above code snippet? Choose one answer.35.8.99.70.55.

if(a<b) return b. int b){ if(a>=b) return a. } else { return node->height. } int height(AVLTree *node) { if (node == NULL) { return -1. int MAX(int a.http://placementkit.blogspot. . }. } } AVLTree * single_rotation_with_left(AVLTree *k2)//Func1 { AVLTree *k1. k1 = k2->left.com/ int height.

k2->height = MAX(height(k2->left). k2->right = k1->left. } AVLTree *double_rotation_with_left(AVLTree *k3) { k3->left = single_rotation_with_right(k3->left). height(k2->right)) + 1. k2->height = MAX(height(k2->left).com/ k2->left = k1->right. return single_rotation_with_left(k3). } . height(k2->right)) + 1.http://placementkit. height(k2->right)) + 1. return k1. k1->right = k2. k1 = k2->right. k1->height = MAX(height(k1->left). k1->height = MAX(height(k1->right). height(k2->left)) + 1. k1->left = k2.blogspot. } AVLTree * single_rotation_with_right(AVLTree *k2)//Func2 { AVLTree *k1. return k1.

} void insert(int value.http://placementkit. AVLTree **node) { if (*node == NULL) { *node = new AVLTree. if (height((*node)->left) .height((*node)->right) == 2) . return. } else if (value < (*node)->element) { insert(value.com/ AVLTree *double_rotation_with_right(AVLTree *k3) { k3->right = single_rotation_with_left(k3->right). (*node)->left = (*node)->right = NULL. &((*node)->left)). if (*node == NULL) { return. (*node)->height = 0. } (*node)->element = value. return single_rotation_with_right(k3).blogspot.

} } } else if (value > (*node)->element) { insert(value.blogspot. if (height((*node)->right) . &((*node)->right)). } else { *node = double_rotation_with_right(*node). .height((*node)->left) == 2) { if (value > (*node)->right->element) { *node = single_rotation_with_right(*node). } else { *node = double_rotation_with_left(*node).http://placementkit.com/ { if (value < (*node)->left->element) { *node = single_rotation_with_left(*node).

} else { return searchmin(node->left).com/ } } } (*node)->height = MAX(height((*node)->left).http://placementkit. } else if (node->left == NULL) { return node. } } AVLTree *searchmax(AVLTree *node) { . } AVLTree *searchmin(AVLTree *node) { if (node == NULL) { return NULL. height((*node)->right)) + 1.blogspot.

if((*node)->element >i) *node = (*node)->left.com/ if (node == NULL) { return NULL. } else { return searchmax(node->right). } else if (node->right == NULL) { return node.int i){ while((*node!=NULL)&&((*node)->element != i)){ parent = node.blogspot. } } .AVLTree **node. else *node = (*node)->right.http://placementkit. } } void Search(AVLTree **parent.

blogspot.http://placementkit. if (value < (*node)->element) { (*node)->left = del(value. if (*node ==NULL) return NULL. } } . } else { (*node) = single_rotation_with_right((*node)). AVLTree **node) { AVLTree * x. if (height((*node)->right) . &((*node)->left)).com/ AVLTree * del(int value.height((*node)->left) >= 2) { if ((*node)->left && (value < (*node)->left->element)) { (*node) = double_rotation_with_right((*node)). AVLTree *tmp_cell.

(*node)->element = tmp_cell->element. (*node)->right = del((*node)->element. &((*node)->right)).com/ } else if (value > (*node)->element) { (*node)->right = del(value. if (height((*node)->left) . .height((*node)->right) >= 2) { if ((*node)->right && (value > (*node)->right->element)) { (*node) = double_rotation_with_left((*node)). } } } else if ((*node)->left && (*node)->right) { tmp_cell = searchmin((*node)->right).http://placementkit.blogspot. } else { (*node) = single_rotation_with_left((*node)). &((*node)->right)).

} Consider an input sequence that is provided as an input to the insert method 20.5. use the .10. else if ((*node)->right == NULL) (*node) = (*node)->left. After Del gets executed.100.35.45.50.70.96 Give the resulting tree and 45 as input to the method called Del.55. } int numofnode(AVLTree **p) { if ((*p!=0)&&((*p)->element != 13)) return numofnode (&(*p)->right) + numofnode (&(*p)->left) + 1.com/ } else { tmp_cell = (*node).http://placementkit. else return 0.30. } return (*node).blogspot. tmp_cell = NULL.8. if ((*node)->left == NULL) (*node) = (*node)->right. free(tmp_cell).90.99.

int height.55. a. 3 d. int element. What would be the height of the node 45 in the resulting tree? Choose one answer.5. }. int MAX(int a.99. struct AVLTree { AVLTree * left. int b){ if(a>=b) . 4 Question 19 Marks: 1 Given the following code snippet answer the following question.35. 3 c. None of these c. 4 Question 18 Marks: 1 Consider an input sequence that is provided as an input to the insert method 20.45.90.10. 2 d.100. AVLTree * right.70.http://placementkit. 0 b.com/ insert method to insert 45. a.96 What would be the height of the node 55 in the resulting tree? Choose one answer.50. 0 b.8.blogspot.30.

k1->height = MAX(height(k1->left). k2->height = MAX(height(k2->left).blogspot. height(k2->right)) + 1. } int height(AVLTree *node) { if (node == NULL) { return -1. height(k2->right)) + 1. k1->right = k2.com/ return a. } else { return node->height. } } AVLTree * single_rotation_with_left(AVLTree *k2) { AVLTree *k1. if(a<b) return b. .http://placementkit. k2->left = k1->right. k1 = k2->left.

k1 = k2->right. } AVLTree *double_rotation_with_left(AVLTree *k3) { k3->left = single_rotation_with_right(k3->left).http://placementkit. k2->right = k1->left. } AVLTree * single_rotation_with_right(AVLTree *k2) { AVLTree *k1. } AVLTree *double_rotation_with_right(AVLTree *k3) { k3->right = single_rotation_with_left(k3->right). k2->height = MAX(height(k2->left). height(k2->left)) + 1. return single_rotation_with_right(k3). k1->height = MAX(height(k1->right). height(k2->right)) + 1. } .blogspot. return k1. return single_rotation_with_left(k3).com/ return k1. k1->left = k2.

} (*node)->element = value. } . } else if (value < (*node)->element) { insert(value. (*node)->height = 0. if (*node == NULL) { return.http://placementkit. &((*node)->left)).height((*node)->right) == 2) { if (value < (*node)->left->element) { *node = single_rotation_with_left(*node). AVLTree **node) { if (*node == NULL) { *node = new AVLTree. if (height((*node)->left) .blogspot.com/ void insert(int value. (*node)->left = (*node)->right = NULL. return.

http://placementkit. if (height((*node)->right) .blogspot. } } } else if (value > (*node)->element) { insert(value. } } } (*node)->height = MAX(height((*node)->left).height((*node)->left) == 2) { if (value > (*node)->right->element) { *node = single_rotation_with_right(*node).com/ else { *node = double_rotation_with_left(*node). . &((*node)->right)). height((*node)->right)) + 1. } else { *node = double_rotation_with_right(*node).

int MAX(int a.6. 9 c.14.com/ } Consider an input sequence that is provided as an input to the insert method 20. int height. 17 d.5.9. int element.15.13.blogspot.12. 15 Question 20 Marks: 1 Given the following code snippet answer the following question. a.17. }. int b){ if(a>=b) return a.19 Which one of the following would be the root node after inserting 16.18.http://placementkit.16.15. struct AVLTree { AVLTree * left. } int height(AVLTree *node) . if(a<b) return b.2. Choose one answer. AVLTree * right. 16 b.

k1 = k2->left. } } AVLTree * single_rotation_with_left(AVLTree *k2) { AVLTree *k1.http://placementkit. height(k2->right)) + 1. k1->height = MAX(height(k1->left). } AVLTree * single_rotation_with_right(AVLTree *k2) { . } else { return node->height. return k1. k2->left = k1->right. height(k2->right)) + 1.com/ { if (node == NULL) { return -1. k2->height = MAX(height(k2->left). k1->right = k2.blogspot.

} void insert(int value. k1 = k2->right.com/ AVLTree *k1. } AVLTree *double_rotation_with_left(AVLTree *k3) { k3->left = single_rotation_with_right(k3->left). return single_rotation_with_left(k3).blogspot. k2->right = k1->left. AVLTree **node) { if (*node == NULL) { *node = new AVLTree. . return k1. height(k2->right)) + 1. } AVLTree *double_rotation_with_right(AVLTree *k3) { k3->right = single_rotation_with_left(k3->right). k1->height = MAX(height(k1->right). height(k2->left)) + 1.http://placementkit. return single_rotation_with_right(k3). k2->height = MAX(height(k2->left). k1->left = k2.

&((*node)->left)).height((*node)->right) == 2) { if (value < (*node)->left->element) { *node = single_rotation_with_left(*node). } else { *node = double_rotation_with_left(*node).com/ if (*node == NULL) { return.http://placementkit. (*node)->left = (*node)->right = NULL. return. } } .blogspot. (*node)->height = 0. } (*node)->element = value. } else if (value < (*node)->element) { insert(value. if (height((*node)->left) .

} } } (*node)->height = MAX(height((*node)->left).2.6.15.http://placementkit. } Consider an input sequence that is provided as an input to the insert method 20.12. 0 .5. a. height((*node)->right)) + 1.17.19 How many times method single_rotation_with_left is called while inserting 18 Choose one answer.height((*node)->left) == 2) { if (value > (*node)->right->element) { *node = single_rotation_with_right(*node).14. if (height((*node)->right) .com/ } else if (value > (*node)->element) { insert(value.9. } else { *node = double_rotation_with_right(*node). &((*node)->right)).16.15.13.blogspot.18.

None of these d. 2 c.http://placementkit.blogspot. 1 .com/ b.