data structure & programming

22

Click here to load reader

Upload: gnv-sandeep

Post on 16-Oct-2014

816 views

Category:

Documents


24 download

TRANSCRIPT

Page 1: Data Structure & Programming

Question Number 1The 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" / \ / \ / \ "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)

A or C but not BB onlyC onlynone of them are legitimate possibilities

Question Number 2The 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:

Page 2: Data Structure & Programming

+ / \ / \ / \ + + / / \ / / \ / / \ "A" + "D" / \ / \ / \ "B" "C"abstract class Node {}

What constructors should Node have according to the specifications for the tree structure?

Node(Node)

Node(String)1. A2. B3. Both4. None of the above

Question Number 3The 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" / \ / \ / \ "B" "C"class InternalNode extends Node { Node left, right;

InternalNode(node l, node r) { left = l; right = r; }}

Page 3: Data Structure & Programming

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

1. Either can be null but not both.2. Neither can be null.3. l cannot be null even if r is not null.4. The answer cannot be deduced from the specifications.

Question Number 4The 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" / \ / \ / \ "B" "C"

What constructors could TerminalNode define consistent with the specifications?

TerminalNode()

TerminalNode(Node)

TerminalNode(String)

1. A and C but not B2. B and C but not A3. C only4. All of them would be acceptable.

Page 4: Data Structure & Programming

Question Number 5Given a 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 { return node->height; }}AVLTree * single_rotation_with_left(AVLTree *k2)//Func1{ 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);}

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

Page 5: Data Structure & Programming

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); } } } 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;}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){ if (node == NULL) { return NULL; } else if (node->right == NULL) { return node; } else {

Page 6: Data Structure & Programming

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

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)); } } } 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)); } else { tmp_cell = (*node); if ((*node)->left == NULL) (*node) = (*node)->right; else if ((*node)->right == NULL) (*node) = (*node)->left;

Page 7: Data Structure & Programming

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 method20,50,45,30,35,55,10,100,90,70,5,99,8,96The resulting tree is given as input to the following code snippetint 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?1. 142. 63. 84. 7

Question Number 6Given a 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 { return node->height; }}AVLTree * single_rotation_with_left(AVLTree *k2)//Func1{ AVLTree *k1; k1 = k2->left; k2->left = k1->right;

Page 8: Data Structure & Programming

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

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

Page 9: Data Structure & Programming

*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); }}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; }}

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 {

Page 10: Data Structure & Programming

(*node) = single_rotation_with_right((*node)); } } } 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)); } 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 method20,50,45,30,35,55,10,100,90,70,5,99,8,96Give the resulting tree and 45 as input to the method called Del. After Del gets executed, use the insert method to insert 45.What would be the height of the node 45 in the resulting tree?1. 22. 33. 04. 4

Page 11: Data Structure & Programming

Consider an input sequence that is provided as an input to the insert method20,50,45,30,35,55,10,100,90,70,5,99,8,96What would be the height of the node 55 in the resulting tree?

1. 42. 33. 04. None of these

Question Number 8Given 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 { 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; 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);

Page 12: Data Structure & Programming

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

Consider an input sequence that is provided as an input to the insert method20,5,15,9,13,2,6,12,14,15,16,17,18,19Which one of the following would be the root node after inserting 16.1.1. 92.2. 153. 174. 16

Page 13: Data Structure & Programming

Question Number 9Given 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 { 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; 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; }

Page 14: Data Structure & Programming

(*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); } } } 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 method20,5,15,9,13,2,6,12,14,15,16,17,18,19How many times method single_rotation_with_left is called while inserting 181.1. 12.2. 03. 24. None of these

Question Number 10Given 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)

Page 15: Data Structure & Programming

{ 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){ 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; 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); } }

Page 16: Data Structure & Programming

} 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 method20,5,15,9,13,2,6,12,14,15,16,17,18,19In the process of inserting the above nodes how many times double_rotation_with_left is being called?

1. 22. 53. 04. 3