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;
http://placementkit.blogspot.com/

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), height(k2->right)) + 1;

return k1;

AVLTree * single_rotation_with_right(AVLTree *k2)

AVLTree *k1;

k1 = k2->right;

k2->right = k1->left;

k1->left = k2;

k2->height = MAX(height(k2->left), height(k2->right)) + 1;

k1->height = MAX(height(k1->right), height(k2->left)) + 1;

return k1;

AVLTree *double_rotation_with_left(AVLTree *k3)

k3->left = single_rotation_with_right(k3->left);

return single_rotation_with_left(k3);

}
http://placementkit.blogspot.com/

AVLTree *double_rotation_with_right(AVLTree *k3)

k3->right = single_rotation_with_left(k3->right);

return single_rotation_with_right(k3);

void insert(int value, AVLTree **node)

if (*node == NULL)

*node = new AVLTree;

if (*node == NULL)

return;

(*node)->element = value;

(*node)->height = 0;

(*node)->left = (*node)->right = NULL;

return;

else if (value < (*node)->element)

insert(value, &((*node)->left));

if (height((*node)->left) - height((*node)->right) == 2)
http://placementkit.blogspot.com/

if (value < (*node)->left->element)

*node = single_rotation_with_left(*node);

else

*node = double_rotation_with_left(*node);

else if (value > (*node)->element)

insert(value, &((*node)->right));

if (height((*node)->right) - height((*node)->left) == 2)

if (value > (*node)->right->element)

*node = single_rotation_with_right(*node);

else

*node = double_rotation_with_right(*node);
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.
http://placementkit.blogspot.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.

Here's an example, with plus signs (+) used to indicate internal nodes:

/\

/ \

/ \

+ +

/ /\

/ / \

/ / \

"A" + "D"

/\

/ \

/ \

"B" "C"

Consider the following proposed implementation of TerminalNode.compareTo. (Java’s


String.compareTo follows the same comparison rules as described here.)

abstract class Node {

abstract boolean isTerminal();

abstract boolean compareTo(Node);

class TerminalNode extends Node {


http://placementkit.blogspot.com/

String value;

boolean isTerminal() {

return true;

boolean compareTo(Node nd) {

if (!nd.isTerminal()) return -1;

return value.compareTo(nd.value);

 Is there a compilation error?


 If there is no compilation error or the compilation error were fixed, does this code work
for all possible values of nd?

Choose one answer.


a. There is no compilation error, and the code does correctly handle all possible values of
nd.
b. There is a compilation error, and if fixed, the code would correctly handle all possible
values of nd.
c. There is no compilation error, but the code does not correctly handle all possible values
of nd.
d. There is a compilation error, and if fixed, 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, 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.
http://placementkit.blogspot.com/

Here's an example, with plus signs (+) used to indicate internal nodes:

/\

/ \

/ \

+ +

/ /\

/ / \

/ / \

"A" + "D"

/\

/ \

/ \

"B" "C"

Here is part of a correct implementation of InternalNode.compareTo.

class InternalNode : extends Node {

int compareTo(Node nd) {

if (null == nd) return 1;

if (nd.isTerminal()) return 1;

InternalNode ind = (InternalNode) nd;

// . . .

int result = left.compareTo(ind.left);

if (0 != result) return result;


http://placementkit.blogspot.com/

return right.compareTo(ind.right);

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

Choose one answer.


a. Neither left nor right are null
b. left is null and either: nd.left is non-null or right is null and ind.right is non-null
c. None of left, right, ind.left, or ind.right are null
d. either: • left is non-null or • left is null and ind.left is non-null
Question 8
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:

/\

/ \

/ \

+ +

/ /\
http://placementkit.blogspot.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. tree1.left is less than tree2.left

B. tree1.left is equal to tree2.left and tree1.right is less than tree2.right

C. either tree1.left is less than tree2.left or tree1.right is less than tree2.right

Choose one answer.


a. A or B, but not C
b. B or C
c. C
d. A only
Question 9
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.
http://placementkit.blogspot.com/

Here's an example, with plus signs (+) used to indicate internal nodes:

/\

/ \

/ \

+ +

/ /\

/ / \

/ / \

"A" + "D"

/\

/ \

/ \

"B" "C"

Here is a proposed implementation of InternalNode.compareTo.

class InternalNode : extends Node {

int compareTo(Node nd) {

if (null == nd) return 1;

if (nd.isTerminal()) return 1;

InternalNode ind = (InternalNode) nd;

if (null == left && null != ind.left)

return -1;

int result = left.compareTo7195(ind.left);


http://placementkit.blogspot.com/

return result != 0 ? result : right.compareTo7195(ind.right);

Given the specifications for the tree structure which conditions could occur that are not handled
correctly in the above code?

A. left and ind.left are both null

B. right and ind.right are both null

C. left is non-null and ind.left is null

Choose one answer.


a. Only C can occur and is not handled correctly by the implementation
b. Only B can occur and is not handled correctly by the implementation.
c. Only A can occur and is not handled correctly by the implementation.
d. B and C can occur and are not handled correctly by the implementation; A either cannot
occur or is handled by the implementation.
Question 10
Marks: 1

Given the following code snippet answer the following question.

struct AVLTree

AVLTree * left;

AVLTree * right;

int element;

int height;

};

int MAX(int a, int b){


http://placementkit.blogspot.com/

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;

k1 = k2->left;

k2->left = k1->right;

k1->right = k2;

k2->height = MAX(height(k2->left), height(k2->right)) + 1;


http://placementkit.blogspot.com/

k1->height = MAX(height(k1->left), height(k2->right)) + 1;

return k1;

AVLTree * single_rotation_with_right(AVLTree *k2)

AVLTree *k1;

k1 = k2->right;

k2->right = k1->left;

k1->left = k2;

k2->height = MAX(height(k2->left), height(k2->right)) + 1;

k1->height = MAX(height(k1->right), height(k2->left)) + 1;

return k1;

AVLTree *double_rotation_with_left(AVLTree *k3)

k3->left = single_rotation_with_right(k3->left);

return single_rotation_with_left(k3);

AVLTree *double_rotation_with_right(AVLTree *k3)

k3->right = single_rotation_with_left(k3->right);

return single_rotation_with_right(k3);
http://placementkit.blogspot.com/

void insert(int value, AVLTree **node)

if (*node == NULL)

*node = new AVLTree;

if (*node == NULL)

return;

(*node)->element = value;

(*node)->height = 0;

(*node)->left = (*node)->right = NULL;

return;

else if (value < (*node)->element)

insert(value, &((*node)->left));

if (height((*node)->left) - height((*node)->right) == 2)

if (value < (*node)->left->element)

*node = single_rotation_with_left(*node);
http://placementkit.blogspot.com/

else

*node = double_rotation_with_left(*node);

else if (value > (*node)->element)

insert(value, &((*node)->right));

if (height((*node)->right) - height((*node)->left) == 2)

if (value > (*node)->right->element)

*node = single_rotation_with_right(*node);

else

*node = double_rotation_with_right(*node);

}
http://placementkit.blogspot.com/

(*node)->height = MAX(height((*node)->left), height((*node)->right)) + 1;

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

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

return 0;

What would be the return value after execution of the above code snippet?

Choose one answer.


a. 5
b. 3
c. 0
d. 2
Question 11
Marks: 1

Given the following code snippet answer the following question.

struct AVLTree

{
http://placementkit.blogspot.com/

AVLTree * left;

AVLTree * right;

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)


http://placementkit.blogspot.com/

AVLTree *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), height(k2->right)) + 1;

return k1;

AVLTree * single_rotation_with_right(AVLTree *k2)

AVLTree *k1;

k1 = k2->right;

k2->right = k1->left;

k1->left = k2;

k2->height = MAX(height(k2->left), height(k2->right)) + 1;

k1->height = MAX(height(k1->right), height(k2->left)) + 1;

return k1;

AVLTree *double_rotation_with_left(AVLTree *k3)

k3->left = single_rotation_with_right(k3->left);
http://placementkit.blogspot.com/

return single_rotation_with_left(k3);

AVLTree *double_rotation_with_right(AVLTree *k3)

k3->right = single_rotation_with_left(k3->right);

return single_rotation_with_right(k3);

void insert(int value, AVLTree **node)

if (*node == NULL)

*node = new AVLTree;

if (*node == NULL)

return;

(*node)->element = value;

(*node)->height = 0;

(*node)->left = (*node)->right = NULL;

return;

else if (value < (*node)->element)

{
http://placementkit.blogspot.com/

insert(value, &((*node)->left));

if (height((*node)->left) - height((*node)->right) == 2)

if (value < (*node)->left->element)

*node = single_rotation_with_left(*node);

else

*node = double_rotation_with_left(*node);

else if (value > (*node)->element)

insert(value, &((*node)->right));

if (height((*node)->right) - height((*node)->left) == 2)

if (value > (*node)->right->element)

*node = single_rotation_with_right(*node);

else
http://placementkit.blogspot.com/

*node = double_rotation_with_right(*node);

(*node)->height = MAX(height((*node)->left), height((*node)->right)) + 1;

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

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

return 0;

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
Let's give the root node of the resulting tree as input to the following code snippet

int func(AVLTree **p)


http://placementkit.blogspot.com/

if (*p!=0)

return func (&(*p)->left) + 1;

else

return 0;

What would be the return value after execution of the above code snippet?

Choose one answer.


a. 4
b. 7
c. 6
d. 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.

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:
http://placementkit.blogspot.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.


a. Both
b. None of the above
c. B
http://placementkit.blogspot.com/

d. A
Question 13
Marks: 1

Given the following code snippet answer the following question.

struct AVLTree

AVLTree * left;

AVLTree * right;

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
http://placementkit.blogspot.com/

return node->height;

AVLTree * single_rotation_with_left(AVLTree *k2)

AVLTree *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), height(k2->right)) + 1;

return k1;

AVLTree * single_rotation_with_right(AVLTree *k2)

AVLTree *k1;

k1 = k2->right;

k2->right = k1->left;

k1->left = k2;

k2->height = MAX(height(k2->left), height(k2->right)) + 1;

k1->height = MAX(height(k1->right), height(k2->left)) + 1;


http://placementkit.blogspot.com/

return k1;

AVLTree *double_rotation_with_left(AVLTree *k3)

k3->left = single_rotation_with_right(k3->left);

return single_rotation_with_left(k3);

AVLTree *double_rotation_with_right(AVLTree *k3)

k3->right = single_rotation_with_left(k3->right);

return single_rotation_with_right(k3);

void insert(int value, AVLTree **node)

if (*node == NULL)

*node = new AVLTree;

if (*node == NULL)

return;

(*node)->element = value;

(*node)->height = 0;
http://placementkit.blogspot.com/

(*node)->left = (*node)->right = NULL;

return;

else if (value < (*node)->element)

insert(value, &((*node)->left));

if (height((*node)->left) - height((*node)->right) == 2)

if (value < (*node)->left->element)

*node = single_rotation_with_left(*node);

else

*node = double_rotation_with_left(*node);

else if (value > (*node)->element)

insert(value, &((*node)->right));

if (height((*node)->right) - height((*node)->left) == 2)

{
http://placementkit.blogspot.com/

if (value > (*node)->right->element)

*node = single_rotation_with_right(*node);

else

*node = double_rotation_with_right(*node);

(*node)->height = MAX(height((*node)->left), height((*node)->right)) + 1;

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

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
http://placementkit.blogspot.com/

return 0;

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
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;

else

return 0;

What would be the return value after execution of the above code snippet?

Choose one answer.


a. 8
b. 13
c. 9
d. 10
Question 14
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.


http://placementkit.blogspot.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.

Here's an example, with plus signs (+) used to indicate internal nodes:

/\

/ \

/ \

+ +

/ /\

/ / \

/ / \

"A" + "D"

/\

/ \

/ \

"B" "C"

class InternalNode extends Node {

Node left, right;

InternalNode(node l, node r) {

left = l;

right = r;

}
http://placementkit.blogspot.com/

Which of the constructor’s arguments can be null according the specifications of the tree
structure?

Choose one answer.


a. Neither can be null.
b. l cannot be null even if r is not null.
c. Either can be null but not both.
d. The answer cannot be deduced from the specifications.
Question 15
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:

/\

/ \

/ \

+ +

/ /\
http://placementkit.blogspot.com/

/ / \

/ / \

"A" + "D"

/\

/ \

/ \

"B" "C"

What constructors could TerminalNode define consistent with the specifications?

TerminalNode()

TerminalNode(Node)

TerminalNode(String)

Choose one answer.


a. All of them would be acceptable.
b. A and C but not B
c. C only
d. B and C but not A
Question 16
Marks: 1

Given a code snippet answer the following question.

struct AVLTree

AVLTree * left;
http://placementkit.blogspot.com/

AVLTree * right;

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)//Func1

{
http://placementkit.blogspot.com/

AVLTree *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), height(k2->right)) + 1;

return k1;

AVLTree * single_rotation_with_right(AVLTree *k2)//Func2

AVLTree *k1;

k1 = k2->right;

k2->right = k1->left;

k1->left = k2;

k2->height = MAX(height(k2->left), height(k2->right)) + 1;

k1->height = MAX(height(k1->right), height(k2->left)) + 1;

return k1;

AVLTree *double_rotation_with_left(AVLTree *k3)

k3->left = single_rotation_with_right(k3->left);

return single_rotation_with_left(k3);
http://placementkit.blogspot.com/

AVLTree *double_rotation_with_right(AVLTree *k3)

k3->right = single_rotation_with_left(k3->right);

return single_rotation_with_right(k3);

void insert(int value, AVLTree **node)

if (*node == NULL)

*node = new AVLTree;

if (*node == NULL)

return;

(*node)->element = value;

(*node)->height = 0;

(*node)->left = (*node)->right = NULL;

return;

else if (value < (*node)->element)

{
http://placementkit.blogspot.com/

insert(value, &((*node)->left));

if (height((*node)->left) - height((*node)->right) == 2)

if (value < (*node)->left->element)

*node = single_rotation_with_left(*node);

else

*node = double_rotation_with_left(*node);

else if (value > (*node)->element)

insert(value, &((*node)->right));

if (height((*node)->right) - height((*node)->left) == 2)

if (value > (*node)->right->element)

*node = single_rotation_with_right(*node);

else
http://placementkit.blogspot.com/

*node = double_rotation_with_right(*node);

(*node)->height = MAX(height((*node)->left), height((*node)->right)) + 1;

AVLTree *searchmin(AVLTree *node)

if (node == NULL)

return NULL;

else if (node->left == NULL)

return node;

else

return searchmin(node->left);

}
http://placementkit.blogspot.com/

AVLTree *searchmax(AVLTree *node)

if (node == NULL)

return NULL;

else if (node->right == NULL)

return node;

else

return searchmax(node->right);

void Search(AVLTree **parent,AVLTree **node,int i){

while((*node!=NULL)&&((*node)->element != i)){

parent = node;

if((*node)->element >i)

*node = (*node)->left;

else

*node = (*node)->right;
http://placementkit.blogspot.com/

AVLTree * del(int value, AVLTree **node)

AVLTree * x;

AVLTree *tmp_cell;

if (*node ==NULL) return NULL;

if (value < (*node)->element)

(*node)->left = del(value, &((*node)->left));

if (height((*node)->right) - height((*node)->left) >= 2)

if ((*node)->left && (value < (*node)->left->element))

(*node) = double_rotation_with_right((*node));

else

(*node) = single_rotation_with_right((*node));
http://placementkit.blogspot.com/

else if (value > (*node)->element)

(*node)->right = del(value, &((*node)->right));

if (height((*node)->left) - height((*node)->right) >= 2)

if ((*node)->right && (value > (*node)->right->element))

(*node) = double_rotation_with_left((*node));

else

(*node) = single_rotation_with_left((*node));

else if ((*node)->left && (*node)->right)

tmp_cell = searchmin((*node)->right);
http://placementkit.blogspot.com/

(*node)->element = tmp_cell->element;

(*node)->right = del((*node)->element, &((*node)->right));

else

tmp_cell = (*node);

if ((*node)->left == NULL)

(*node) = (*node)->right;

else if ((*node)->right == NULL)

(*node) = (*node)->left;

free(tmp_cell);

tmp_cell = NULL;

return (*node);

int numofnode(AVLTree **p)

if ((*p!=0)&&((*p)->element != 13))

return numofnode (&(*p)->right) + numofnode (&(*p)->left) + 1;

else

return 0;

}
http://placementkit.blogspot.com/

Consider an input sequence that is provided as an input to the insert method


20,50,45,30,35,55,10,100,90,70,5,99,8,96
The resulting tree is given as input to the following code snippet

int nol(AVLNode **p)

if (*p==0)

return 0;

if ((*p)->left==0 && (*p)->right ==0)

return 1;

else

return (nol(&(*p)->right)+nol(&(*p)->left) + 1);

Which one of the following would be the output of above code snippet?

Choose one answer.


a. 8
b. 7
c. 14
d. 6
Question 17
Marks: 1

Given a code snippet answer the following question.

struct AVLTree

AVLTree * left;

AVLTree * right;

int element;
http://placementkit.blogspot.com/

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)//Func1

AVLTree *k1;

k1 = k2->left;
http://placementkit.blogspot.com/

k2->left = k1->right;

k1->right = k2;

k2->height = MAX(height(k2->left), height(k2->right)) + 1;

k1->height = MAX(height(k1->left), height(k2->right)) + 1;

return k1;

AVLTree * single_rotation_with_right(AVLTree *k2)//Func2

AVLTree *k1;

k1 = k2->right;

k2->right = k1->left;

k1->left = k2;

k2->height = MAX(height(k2->left), height(k2->right)) + 1;

k1->height = MAX(height(k1->right), height(k2->left)) + 1;

return k1;

AVLTree *double_rotation_with_left(AVLTree *k3)

k3->left = single_rotation_with_right(k3->left);

return single_rotation_with_left(k3);

}
http://placementkit.blogspot.com/

AVLTree *double_rotation_with_right(AVLTree *k3)

k3->right = single_rotation_with_left(k3->right);

return single_rotation_with_right(k3);

void insert(int value, AVLTree **node)

if (*node == NULL)

*node = new AVLTree;

if (*node == NULL)

return;

(*node)->element = value;

(*node)->height = 0;

(*node)->left = (*node)->right = NULL;

return;

else if (value < (*node)->element)

insert(value, &((*node)->left));

if (height((*node)->left) - height((*node)->right) == 2)
http://placementkit.blogspot.com/

if (value < (*node)->left->element)

*node = single_rotation_with_left(*node);

else

*node = double_rotation_with_left(*node);

else if (value > (*node)->element)

insert(value, &((*node)->right));

if (height((*node)->right) - height((*node)->left) == 2)

if (value > (*node)->right->element)

*node = single_rotation_with_right(*node);

else

*node = double_rotation_with_right(*node);
http://placementkit.blogspot.com/

(*node)->height = MAX(height((*node)->left), height((*node)->right)) + 1;

AVLTree *searchmin(AVLTree *node)

if (node == NULL)

return NULL;

else if (node->left == NULL)

return node;

else

return searchmin(node->left);

AVLTree *searchmax(AVLTree *node)

{
http://placementkit.blogspot.com/

if (node == NULL)

return NULL;

else if (node->right == NULL)

return node;

else

return searchmax(node->right);

void Search(AVLTree **parent,AVLTree **node,int i){

while((*node!=NULL)&&((*node)->element != i)){

parent = node;

if((*node)->element >i)

*node = (*node)->left;

else

*node = (*node)->right;

}
http://placementkit.blogspot.com/

AVLTree * del(int value, AVLTree **node)

AVLTree * x;

AVLTree *tmp_cell;

if (*node ==NULL) return NULL;

if (value < (*node)->element)

(*node)->left = del(value, &((*node)->left));

if (height((*node)->right) - height((*node)->left) >= 2)

if ((*node)->left && (value < (*node)->left->element))

(*node) = double_rotation_with_right((*node));

else

(*node) = single_rotation_with_right((*node));

}
http://placementkit.blogspot.com/

else if (value > (*node)->element)

(*node)->right = del(value, &((*node)->right));

if (height((*node)->left) - height((*node)->right) >= 2)

if ((*node)->right && (value > (*node)->right->element))

(*node) = double_rotation_with_left((*node));

else

(*node) = single_rotation_with_left((*node));

else if ((*node)->left && (*node)->right)

tmp_cell = searchmin((*node)->right);

(*node)->element = tmp_cell->element;

(*node)->right = del((*node)->element, &((*node)->right));


http://placementkit.blogspot.com/

else

tmp_cell = (*node);

if ((*node)->left == NULL)

(*node) = (*node)->right;

else if ((*node)->right == NULL)

(*node) = (*node)->left;

free(tmp_cell);

tmp_cell = NULL;

return (*node);

int numofnode(AVLTree **p)

if ((*p!=0)&&((*p)->element != 13))

return numofnode (&(*p)->right) + numofnode (&(*p)->left) + 1;

else

return 0;

Consider an input sequence that is provided as an input to the insert method


20,50,45,30,35,55,10,100,90,70,5,99,8,96
Give the resulting tree and 45 as input to the method called Del. After Del gets executed, use the
http://placementkit.blogspot.com/

insert method to insert 45.


What would be the height of the node 45 in the resulting tree?

Choose one answer.


a. 0
b. 3
c. 2
d. 4
Question 18
Marks: 1

Consider an input sequence that is provided as an input to the insert method


20,50,45,30,35,55,10,100,90,70,5,99,8,96
What would be the height of the node 55 in the resulting tree?

Choose one answer.


a. 0
b. None of these
c. 3
d. 4
Question 19
Marks: 1

Given the following code snippet answer the following question.

struct AVLTree

AVLTree * left;

AVLTree * right;

int element;

int height;

};

int MAX(int a, int b){

if(a>=b)
http://placementkit.blogspot.com/

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;

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), height(k2->right)) + 1;


http://placementkit.blogspot.com/

return k1;

AVLTree * single_rotation_with_right(AVLTree *k2)

AVLTree *k1;

k1 = k2->right;

k2->right = k1->left;

k1->left = k2;

k2->height = MAX(height(k2->left), height(k2->right)) + 1;

k1->height = MAX(height(k1->right), height(k2->left)) + 1;

return k1;

AVLTree *double_rotation_with_left(AVLTree *k3)

k3->left = single_rotation_with_right(k3->left);

return single_rotation_with_left(k3);

AVLTree *double_rotation_with_right(AVLTree *k3)

k3->right = single_rotation_with_left(k3->right);

return single_rotation_with_right(k3);

}
http://placementkit.blogspot.com/

void insert(int value, AVLTree **node)

if (*node == NULL)

*node = new AVLTree;

if (*node == NULL)

return;

(*node)->element = value;

(*node)->height = 0;

(*node)->left = (*node)->right = NULL;

return;

else if (value < (*node)->element)

insert(value, &((*node)->left));

if (height((*node)->left) - height((*node)->right) == 2)

if (value < (*node)->left->element)

*node = single_rotation_with_left(*node);

}
http://placementkit.blogspot.com/

else

*node = double_rotation_with_left(*node);

else if (value > (*node)->element)

insert(value, &((*node)->right));

if (height((*node)->right) - height((*node)->left) == 2)

if (value > (*node)->right->element)

*node = single_rotation_with_right(*node);

else

*node = double_rotation_with_right(*node);

(*node)->height = MAX(height((*node)->left), height((*node)->right)) + 1;


http://placementkit.blogspot.com/

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

Which one of the following would be the root node after inserting 16.

Choose one answer.


a. 16
b. 9
c. 17
d. 15
Question 20
Marks: 1

Given the following code snippet answer the following question.

struct AVLTree

AVLTree * left;

AVLTree * right;

int element;

int height;

};

int MAX(int a, int b){

if(a>=b)

return a;

if(a<b)

return b;

int height(AVLTree *node)


http://placementkit.blogspot.com/

if (node == NULL)

return -1;

else

return node->height;

AVLTree * single_rotation_with_left(AVLTree *k2)

AVLTree *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), height(k2->right)) + 1;

return k1;

AVLTree * single_rotation_with_right(AVLTree *k2)

{
http://placementkit.blogspot.com/

AVLTree *k1;

k1 = k2->right;

k2->right = k1->left;

k1->left = k2;

k2->height = MAX(height(k2->left), height(k2->right)) + 1;

k1->height = MAX(height(k1->right), height(k2->left)) + 1;

return k1;

AVLTree *double_rotation_with_left(AVLTree *k3)

k3->left = single_rotation_with_right(k3->left);

return single_rotation_with_left(k3);

AVLTree *double_rotation_with_right(AVLTree *k3)

k3->right = single_rotation_with_left(k3->right);

return single_rotation_with_right(k3);

void insert(int value, AVLTree **node)

if (*node == NULL)

*node = new AVLTree;


http://placementkit.blogspot.com/

if (*node == NULL)

return;

(*node)->element = value;

(*node)->height = 0;

(*node)->left = (*node)->right = NULL;

return;

else if (value < (*node)->element)

insert(value, &((*node)->left));

if (height((*node)->left) - height((*node)->right) == 2)

if (value < (*node)->left->element)

*node = single_rotation_with_left(*node);

else

*node = double_rotation_with_left(*node);

}
http://placementkit.blogspot.com/

else if (value > (*node)->element)

insert(value, &((*node)->right));

if (height((*node)->right) - height((*node)->left) == 2)

if (value > (*node)->right->element)

*node = single_rotation_with_right(*node);

else

*node = double_rotation_with_right(*node);

(*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

How many times method single_rotation_with_left is called while inserting 18

Choose one answer.


a. 0
http://placementkit.blogspot.com/

b. 2
c. None of these
d. 1

You might also like