You are on page 1of 34

A father who had four children wanted to distribute some sweets among them.

He g ave the first child one third of the sweets and then gave the second child half of the remaining sweets. He then gave the third child two thirds of the sweets r emaining after the earlier two children received their share. The fourth child r eceived what was left. If the third child got 8 sweets, then how many sweets wer e there to start with? 24 32 36 42 can be answered by using both the statements together, but not by using either statement alone. 4. Neither of the statements, individually or jointly, provides sufficient data to answer the question. How many students are in a class in which the average height is 150 centimeters? A. If 4 students with an average height of 148 centimeters are replaced by 4 stu dents with an average height of 160 centimeters, the average height increases by 1 centimeter. B. If 2 students of height 145 cms and 160 cms join, the average height of the s tudents in the class increases by a number that is an integer. 1 2 3 4 Question Number 3 Please carefully read the following: The following flowchart represents the process described below. Once you have re ad the description of the process, answer the question below the flowchart. In a certain country called Fictitia, the income tax structure is such that: 1. If you are married your taxable income is reduced by 10% 2. For up to 3 children, you get a tax reduction of $750 for each child 3. If you are over 65 or your income is below $15,000 you pay no tax 4. Between $15,000 and $40,000 you pay 10% tax and between $40,000 and $100,000 you pay 20% tax and above $100,000 you pay 30% tax 5. If your income is more than $250,000, you pay a surcharge of 5% on your overa

ll tax

What is the value of Cell 3? Is TAXABLE_INCOME < $40,000? Is TAXABLE_INCOME > $40,000? Is TAXABLE_INCOME < $250,000? Is TAXABLE_INCOME > $250,000?

The subject of these questions is an unusually simple kind of binary tree, defin ed 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 tre e 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" The specifications state that an InternalNode contains no information, just chil d nodes. Suppose that were changed so an InternalNode contains a String value, j ust like TerminalNode. Assuming that TerminalNode has a String-valued field with getValue and setValue methods, which of the following approaches would bestimpl ement that change? Add a String-valued field and getString and setString methods to InternalNode. Add a String-valued field to InternalNode and getString and setString methods to Node. Add a String-valued field to Node and getString and setString methods to Interna lNode. Move the field and two methods from TerminalNode to Node. / \ \ + / \ \ \ "D"

The subject of these questions is an unusually simple kind of binary tree, defin ed 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 tre e 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" abstract class Node { // ... } class TerminalNode extends Node { String value; TerminalNode(String str) { value = str; } String getValue(); } According to the specifications, should TerminalNode have a setValue(String) met hod? The specifications require a setValue(String) method. The specifications neither require nor forbid a setValue(String) method. The specifications forbid a setValue(String) method. The specifications neither require nor forbid a setValue(String) method, but if one is defined it should be defined in Node, not in TerminalNode. The subject of these questions is an unusually simple kind of binary tree, defin ed 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 tre e 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: / + / \ \ \ "C" / \ \ + / \ \ \ "D"

+ / \ / / + / / / "A" / / + / \ \ / \ "B" "C" The specifications state that an InternalNode contains no information, just chil d nodes. Suppose that were changed so an InternalNode contains a String value, j ust like TerminalNode. Assuming that TerminalNode s String-valued field and getVal ue and setValue methods are moved to Node to implement this change, which of the following other changes are necessary to make this work? Change TerminalNode s constructor to call Node s constructor rather than setting the value directly. Add a String argument to every constructor in InternalNode. Add a call to Node s constructor to every constructor in InternalNode. A and C are necessary, but B is not. B and C are necessary, but A is not. Only A is necessary. They are all desirable, but none of them are technically required. The subject of these questions is an unusually simple kind of binary tree, defin ed 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 tre e 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" / \ \ + / \ \ \ "D"

We want to be able to add new right or left subtrees to a node with the followin g methods: abstract class Node { bool addLeft(Node nd) { return false; } bool addRight(Node nd) { return false; } } where the methods return false to indicate that nd couldn t be added as requested and true otherwise. InternalNode will override these methods. In what situations must InternalNode.addLeft return false? nd is null the InternalNode already has a left subtree A only B only Both The subject of these questions is an unusually simple kind of binary tre e, 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 tre e 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" Suppose InternalNode implements getLeft and getRight as part of its public inter face to allow external code to navigate the tree. Their return type must be Node , because they may return either an internal or a terminal node. Which of the fo llowing are legitimate approaches to implementing the methods for the other clas ses? Note: The specifications do not indicate what the behavior of nd.getLeft() and n d.getRight() should be or whether it is legitimate to call them. For this questi on assume that each possible answer is a reasonable interpretation of the specif ications the question is about the design and implementation of the classes and me thods, not specifically what they do. Node could provide abstract declarations and TerminalNode concrete implementatio ns of getLeft and getRight. Node could provide concrete definitions that return null, in which case Terminal Node doesn't need to provide implementations. / \ \ + / \ \ \ "D"

Node could provide concrete definitions that throw UnsupportedOperationException , in which case TerminalNode doesn't need to provide implementations. Only A A and B, but not C B and C, but not A A, B, and C are all OK. The subject of these questions is an unusually simple kind of binary tree, defin ed 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 tre e 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" Consider this implementation of toString(): abstract class Node { abstract String toString(); } class TerminalNode extends Node { void toString() { return value; } } class InternalNode extends Node { void toString() { return "[" + ((null == left) ? "" : left.toString()) + " , " + ((null == right) ? "" : right.toString()) + "]"; } } Suppose a function Node.nextValue() is provided that returns the next terminal n ode, reading from left to right, regardless of the level at which the terminal n / \ \ + / \ \ \ "D"

ode appears. Suppose we have two trees for which calling nextValue starting at t heir root node produces the same sequence of strings, including having the same length. Under what conditions will the above implementation of toString produce differen t strings for these two trees? Never Only for full trees (ones in which every internal node has two children, except po ssibly the parent of the rightmost terminal node) Always Two trees cannot have the same sequence of strings in their terminal nodes while having a different internal structure. Neither

The subject of these questions is an unusually simple kind of binary tree, defin ed 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 tre e 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" abstract class Node { abstract void remove(); } class TerminalNode extends Node { Node left, right; } class InternalNode extends Node { String value; } We want to implement remove in TerminalNode so that it is removed from the tree and in InternalNode so that the entire subtree starting at InternalNode is remov / + / \ \ \ "C" / \ \ + / \ \ \ "D"

ed from the tree. Which of the following would be required to implement this cha nge? add to TerminalNode a reference to its parent add to InternalNode a reference to its parent add to Node a reference to its parent A B Either A and B together, or C alone, would be required. All three would be required.The subject of these questions is an unusually simpl e 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 tre e 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" Suppose we do add a reference to a Node s parent to Node: abstract class Node { ??? parent; } The implementation of remove would call parent.removeChild(this). Where should r emoveChild(Node child) be implemented, given an appropriate choice for the type of parent in Node? A concrete implementation in Node Abstract in Node and an implementation in TerminalNode and InternalNode Only in InternalNode Abstract in Node and an implementation in InternalNode, but not in TerminalNodeT erminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". / \ \ + / \ \ \ "D"

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 tre e 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" abstract class Node { Node parent; void remove() { parent.removeChild(this); } Consider the following implementation of class InternalNode extends Node { void removeChild(Node nd) throws Exception { // ... } } Assume that all the other methods for constructing and modifying the tree are co rrectly implemented, so that no impossible situation exists when removeChild is ca lled. Consider each of the following conditions at the start of removeChild s code . What must the code do for each of these conditions? both children are null one child is null neither child is null Check for A and C and throw an exception if either occurs, and for B just set th e non-null child to null. A can be ignored, throw an exception if C occurs, and for B just set the non-nul l child to null. A can be ignored, throw an exception if C occurs, and for B set the non-null chi ld to null and call parent.removeChild(this). A can be ignored. For B, set the non-null child to null and call parent.removeCh ild(this). For C, compare the identity of nd with left and right and set the one that matches to null. struct AVLTree / + / \ \ \ "C" / \ \ + / \ \ \ "D"

{ 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 Let's give the root node of the resulting tree as input to the following code sn ippet int func(AVLTree **p) { if (*p!=0) return func (&(*p)->right) + 1; else return 0; } What would be the return value after execution of the above code snippet? 3 6

7 4 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 In the process of inserting the above nodes how many times double_rotation_with_ right is being called 3 0 2 5

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 Let's give the root node of the resulting tree as input to the following code sn ippet int func(AVLTree **p) { if (*p!=0) return func (&(*p)->right) + func (&(*p)->left) + 1; else return 0; } What would be the return value after execution of the above code snippet?

13 14 15 None of these 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 void Func(AVLTree **node){ if(*node!=NULL){ Func(&(*node)->left); AVLTree *temp; temp = (*node)->left; (*node)->left= (*node)->right; (*node)->right = temp; Func(&(*node)->right); } } Which one of the following sequence is the inorder traversal of the resulting tr ee? 50,55,70,90,96,99,100,5,8,10,20,30,35,45

5,8,10,20,30,35,45,50,55,70,90,96,99,100 50,55,70,90,96,99,100,45,5,8,10,20,30,35 45,5,8,10,20,30,35,50,55,70,90,96,99,100

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 void traverse(AVLTree** nd){ if(*nd!=NULL){ traverse(&((*nd)->left)); traverse(&((*nd)->right)); std::cout<<(*nd)->element<<std::endl; } } 5,8,10,20,30,35,45,50,55,70,90,96,99,100 5,8,10,20,30,35,50,55,70,90,96,99,100,45 45,5,8,10,20,30,35,50,55,70,90,96,99,100 None of these

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)->left==0 && (*p)->right ==0) return 1; else return (nol(&(*p)->right)+nol(&(*p)->left)); } Which one of the following would be the output of above code snippet? 8 7 14 None of these 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 void Func(AVLTree **node){ if(*node!=NULL){ Func(&(*node)->left); AVLTree *temp; temp = (*node)->left; (*node)->left= (*node)->right; (*node)->right = temp; } }

Which one of the following sequence is the preorder traversal of the resulting t ree? 50,55,70,90,96,99,100,45,20,30,35,10,8,5 50,55,70,90,96,99,100,20,30,35,10,8,5,45 45, 50,55,70,90,96,99,100,20,30,35,10,8,5 None of these

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 tre e 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" combine operation such that / \ \ + / \ \ \ "D"

We want to add a

node1.combine(node2) modifies node1 in as simple a way as possible to make node1.flatten().append(node2.flatten()) before combining the two nodes equal to node1.flatten() after combining them. Which of the following implementations of InternalNode.combine(Node node2) would work? InternalNode.combine(Node node2) { Node nd = this; while (!nd.isTerminal() && null != right) nd = right; ((InternalNode)nd).right = node2; } InternalNode.combine(Node node2) { Node nd = this; while (! (null == right right.isTerminal())) nd = right; ((InternalNode)nd).right = node2; } InternalNode.combine(Node node2) { Node nd = this; while (! (null == right && null == left) nd = right; if (null == right) ((InternalNode)nd).right = node2; else ((InternalNode)nd).left = node2; } InternalNode.combine(Node node2) { Node nd = this; while (! (null == right right.isTerminal())) nd = right; if (null == right) ((InternalNode)nd).right = node2; else ((InternalNode)nd).right = new InternalNode(right, node2); } Question Number 10

The subject of these questions is an unusually simple kind of binary tree, defin ed 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 tre e 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" Sometimes the internal structure of a tree matters for operations like equality tests. However, sometimes all that matters is the sequence of terminal nodes, re gardless of differences in internal structure. This is often called the fringe o f the tree. A sameFringe method could be implemented simply by calling flatten on two trees and comparing the resulting lists. However, the trees could be very large, and d ifferences might appear very early in the fringes, so this would be an inefficie nt approach. The simplest way to resolve this difficulty is to define an iterator for the tre e. (That s probably a good idea anyway.) next() would be defined to produce the st ring of the next terminal node in the fringe. Which of the following would best support implementation of an iterator with two methods: hasNext() and next()? a stack of nodes a queue of nodes recursion A B C A and C equally well / \ \ + / \ \ \ "D"

A train left Mumbai for Delhi at 8 am and reached Delhi at 2 pm on the same day. Another train left Delhi for Mumbai at 8 am and reached Mumbai at 12 pm on the same day. Assuming both of the trains maintained an even speed throughout and di d not stop anywhere, at what time did the two trains meet?

You might also like