1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Coding Skills (Advanced)

59 741 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 59
Dung lượng 3,08 MB

Nội dung

Question Number 6 The subject of these questions is an unusually simple kind of binary tree, defined by these properties:  Terminal nodes contain a string.. Question Number 7 The subje

Trang 2

Coding Skills (Advanced)

Question Number 1

Given the following code snippet:

void InsertNode(tNode** node, int i){

Trang 3

std::cout<<"Enter the node"<<std::endl;

5,10,12,15,78,96,98,99,100,110,120

120,110,100,99,98,96,78,15,12,10,5

98,100,120,110,99,15,78,96,12,5,110

Question Number 2

Given the following code snippet:

void InsertNode(tNode** node, int i){

Trang 4

5,12,10,99,96,78,15,110,120,100,98

5,10,12,15,78,96,99,98,100,110,120

98,100,120,110,15,78,96,99,10,12,5

Question Number 3

Given the following code snippet:

Trang 6

98,15,100,10,78,120,5,12,96,110

What would be the output of the

following code snippet?

Given the following code snippet:

void InsertNode(tNode** node, int i){

if(*node == NULL){

*node = new tNode;

(*node)->pLeft = NULL;

(*node)->data = i;

Trang 8

100,110,120,98,5,10,12,15,78,96,97,99

None of these

Question Number 5

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"

Suppose we want the classes to implement getLeft and getRight to allow external code to navigate the tree What should their return type be?

Object

Node

InternalNode

TerminalNode

Trang 9

Question Number 6

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"

Should InternalNode have simple “setters”, as follows?

void setLeft(Node nd) {

left = nd;

}

void setRight(Node nd) {

right = nd;

}

Yes

Optional—the specifications don’t imply either yes or no

No—the specifications directly forbid it

No—the specifications don’t say one way or another but these implementations are inadequate

Trang 10

Question Number 7

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" Consider this implementation of toString(): abstract class Node { String toString(); } class TerminalNode extends Node { String toString() { return value; }

} class InternalNode extends Node { String toString() { if (null == left) return "[" + right + "]"; else if (null == right) return "[" + left + "]"; else return "[" + left + " " + right + "]"; }

}

Trang 11

Under what conditions will this implementation distinguish (i.e produce different strings for) two trees that have the same strings in the same sequence in their terminal nodes but different internal structures?

Trang 12

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

Trang 14

int height(AVLTree *node)

Trang 15

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

Trang 18

In the process of inserting the above nodes how many times double_rotation_with_left is being called?

5

0

3

2

Coding Skills (Advanced)

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" Which expression would create a representation of the following tree? IN0 / \

/ \

/ \

Trang 19

new InternalNode(new TerminalNode("A")),

new InternalNode(new TerminalNode("D"),

new TerminalNode("B", null),

new TerminalNode("C", null))));

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

Trang 20

 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" Having created a pathTo(String) method we’d like to have the reverse operation, atPath(List) Here is a proposed implementation (Note that it returns a Node, not a String.) abstract class Node { abstract bool isTerminal(); abstract Node atPath(List lst); } class TerminalNode extends Node { boolean isTerminal { return True; } Node atPath(List lst) { if (lst.isEmpty()) return this; return null; }

} class InternalNode extends Node { boolean isTerminal { return False; } Node atPath(List lst) { Node curnode = this; InternalNode ind = null; for (int n = 0; n < lst.size(); n++) { if (curnode.isTerminal()) return null; String dir = lst.get(n); if (!dir.equals("L") && !dir.equals("R")) return null; ind = (InternalNode) curnode; if (dir.equals("L")) curnode = ind.getLeft(); else curnode = ind.getRight(); }

Trang 21

return curnode;

}

} Which of the possible conditions does the code handle correctly? A An empty subtree is encountered before the end of the path B The path leads to a TerminalNode C The path leads to an InternalNode All of them A but neither B nor C A and B but not C B and C but not A 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: +

/ \

/ \

/ \

+ +

/ / \

/ / \

Trang 22

We want to locate the first occurrence of a string in the tree and return a

“path” that shows how to navigate to it from the root The path will be

represented as a list of strings, with “L” indicating that the left child should be followed and “R” that the right child should be followed The list

“L”, “R” would identify the right child of the left child of the root of the tree

Assume there is a List data structure available that provides

 a constructor for creating an empty List

addLast(String) to add a string to the end of the list returning the

(modified) list so that, for example, a series of additions could be

Trang 23

return right.pathTo(str)).addFirst("R");

return null;

}

List pathTo(String str) {

List lst = new List();

if ((left != null) && (null != (lst = left.pathTo(str))))

 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:

Trang 24

One problem with the proposed implementation of equals is that two nodes of different types cannot be equal In the following code which of TerminalNode.equals and

InternalNode.equals correct this problem assuming the argument to each method is not null?

(The relevant parts of the class definitions are included.)

abstract class Node {

abstract boolean isTerminal();

abstract boolean equals(Node);

Neither TerminalNode nor InternalNode

TerminalNode but not InternalNode

InternalNode but not TerminalNode

Both TerminalNode and InternalNode

Question Number 5

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

Trang 25

 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:

Here is a proposed implementation of equals for the tree structure:

abstract class Node {

abstract boolean equals(Node nd);

Trang 26

Both

Question Number 6

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:

The problem with the previous definition of InternalNode.equals is that it calls

nd.getLeft() and nd.getRight(), but only InternalNode defines those methods and ndcould be a TerminalNode The following code fixes that and will compile

abstract class Node {

abstract boolean isTerminal();

abstract boolean equals(Node);

Trang 27

Both methods could ever be called with a null argument with certain trees

TerminalNode.equals Both methods could be called with a null argument with certain trees but not InternalNode

InternalNode.equals Both methods could be called with a null argument with certain trees but not TerminalNode

Neither method could ever be called with a null argument with certain trees

Question Number 7

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:

Trang 28

abstract class Node {

abstract boolean isTerminal();

abstract boolean equals(Node);

InternalNode.equals needs further code to deal with nulls but TerminalNode.equalsdoesn’t

Both TerminalNode.equals and InternalNode.equals need further code to deal with nulls

Trang 29

Question Number 8

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:

The while statement (both the condition and the action) is correct

The return statement (taking into account all the details of the entire statement)

There are problems with both the while and return statements

Both the while and return statements are correct

Ngày đăng: 21/04/2016, 17:59

TỪ KHÓA LIÊN QUAN

w