Anda di halaman 1dari 16

Question Number 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"

\ / \ "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 B B only C only none of them are legitimate possibilities


Question Number 2

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" / + + / / \ / "B" / \ \ "C" / / \ \ + / \ \ \ "D"

abstract class Node { }

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

1. 2. 3. 4.

A B Both None of the above

Question Number 3

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" / + + / / \ / \ \ "C" / / \ \ + / \ \ \ "D"

/ "B" class InternalNode extends Node { Node left, right; InternalNode(node l, node r) { left = l; right = r; }

Which of the constructors arguments can be null according the specifications


of the 1. 2. 3. tree structure?

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

Question Number 4

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"

What constructors could specifications?


TerminalNode() TerminalNode(Node) TerminalNode(String)

\ / \ "B" "C" TerminalNode define

/ \

consistent with the

1. 2. 3. 4.

A and C but not B B and C but not A C only All of them would be acceptable.

Question Number 5

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

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 {

} }

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;

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 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? 1. 14 2. 6 3. 8 4. 7
Question Number 6

Given 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) { 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 { 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; 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 insert method to insert 45. What would be the height of the node 45 in the resulting tree? 1. 2 2 3 . 3. 0 4. 4

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? 1. 4 2. 3 3. 0 4. None of these
Question Number 8

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 { 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); } } } 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
Which one of the following would be the root node after inserting 16. 1. 9 1.

2. 2. 15 3. 17 4. 16

Question Number 9

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

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

(*node)->height = 0; (*node)->left = (*node)->right = NULL; return;

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

2. 0 2. 3. 2 4. None of these
Question Number 10

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)

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

return -1; } else { return node->height; }

} 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 In the process of inserting the above nodes how many times double_rotation_with_left is being called? 1. 2 2. 5 3. 0 4. 3

Anda mungkin juga menyukai