Data Structures Exam4

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

20 12 23 11 21 30 What is the tree after removing 12?

(20 (11, 23 (21, 30)))

20 12 23 11 21 30 What is the tree after removing 21?

(20 (12 (11, -), 23 (-, 30)))

20 12 23 11 21 30 What is the tree after removing 20?

(21 (12 (11, -), 23 (-, 30)))

Determine cur's next assignment given the key and current node. key = 6, cur = 47 47 19 48 6 51 62

19

Suppose node 23 was instead 21, meaning two 21 nodes exist (which is allowed in a BST). When searching for 21, which node will be returned?

Internal

Search for insert location:

: If the left (or right) child is not 0, the algorithm assigns the current node with that child and continues searching for a proper insert location.

950's successor is 150 500 300 800 150 750 900 250 850 950

Fasle

Remove a leaf node:

If X has a parent (so X is not the root), the parent's left or right child (whichever points to X) is assigned with 0. Else, if X was the root, the root pointer is assigned with 0, and the BST is now empty.

Remove an internal node with single child

If X has a parent (so X is not the root), the parent's left or right child (whichever points to X) is assigned with X's single child. Else, if X was the root, the root pointer is assigned with X's single child.

Insert as right child:

If the new node's key is greater than the current node, and the current node's right child is 0, the algorithm assigns the node's right child with the new node.

Insert as left child:

If the new node's key is less than the current node, and the current node's left child is 0, the algorithm assigns that node's left child with the new node.

The first node in the BST ordering is 150. 500 300 800 150 750 900 250 850 950

True

The recursive DFS algorithm is run on the following graph. Assume D is the starting vertex. B D A | \ | \ | \ | C == E DFS begins with the function call RecursiveDFS(D).

True

The recursive DFS algorithm is run on the following graph. Assume D is the starting vertex. B D A | \ | \ | \ | C == E If B is not yet visited, RecursiveDFS(B) will make subsequent calls to RecursiveDFS(C) and RecursiveDFS(E).

True

If the child to be visited is 0, when does the algorithm return 0

Upon exiting the loop

Consider the following tree: 20 12 23 11 21 30 Assume a full 255-node BST. How many algorithm loop iterations will occur for an insert?

8

What is the maximum loop iterations for a full binary tree with 255 nodes?

8

Determine cur's next assignment given the key and current node. key 91, cur = 99 86 37 99 51 91 124

91

In searching for 145, what node is visited third? 300 100 900 25 145 750 925

145

Consider the following tree: 20 12 23 11 21 30 Where will a new node 18 be inserted?

12's right child

Consider the following tree. If node does not exists, enter 0. 52 33 67 14 60 81 When searching for key 45, what node is visited third?

0

A new BST is built by inserting nodes in this order: 6 2 8 6 2 8 What is the tree height?

1 6 2 8

In searching for 145, what node is visited second? 300 100 900 25 145 750 925

100

Consider the following tree. 300 100 775 25 201 750 925 11 What node is printed first?

11

A new BST is built by inserting nodes in this order: 20 12 23 18 30 What is the tree height?

2 20 12 23 16 30

When searching for key 21, what node is visited first? 20 12 23 11 18 21 30

20

When searching for key 21, what node is visited third?' 20 12 23 11 18 21 30

21

When searching for key 21, what node is visited second? 20 12 23 11 18 21 30

23

A new BST is built by inserting 255 nodes in sorted order. What is the tree height?

254

Consider the following tree. 7 1 64 28 77 When inserting a new node with key 35, what node is visited third?

28

Consider the following tree: 20 12 23 11 21 30 Assume a full 7-node BST. How many algorithm loop iterations will occur for an insert?

3

What is the maximum loop iterations for a full binary tree with 7 nodes, if a node matches?

3

What is the maximum loop iterations for a full binary tree with 7 nodes, if no node matches?

3

What is the worst case (largest) number of nodes visited when searching for a key? 300 100 900 25 145 750 925

3

In searching for 145, what node is visited first? 300 100 900 25 145 750 925

300

Consider the following tree. 300 100 775 25 201 750 925 11 Complete the tree traversal after node 300's left subtree has been printed

300 750 775 925

Which nodes would be visited when searching for 900? Write nodes in order visited, as: 5, 10 300 100 900 25 145 750 925

300, 900

Which nodes would be visited when searching for 800? Write nodes in order visited, as: 5, 10, 15 300 100 900 25 145 750 925

300, 900, 750

Consider the following tree. If node does not exists, enter 0. 52 33 67 14 60 81 When searching for key 45, what node is visited second?

33

Determine cur's next assignment given the key and current node. key = 40, cur = 27 27 21 39

39

A new BST is built by inserting nodes in this order: 30 11 23 21 20 What is the tree height?

4 30 11 23 21 20

A new BST is built by inserting nodes in this order: 30 23 21 20 18 What is the tree height?

4 30 23 21 20 16

What is the worst case (largest) number of comparisons given a full BST with n nodes? Given the following tree. 0 0 0 0 0 0 0

5

Consider the following tree. If node does not exists, enter 0. 52 33 67 14 60 81 When searching for key 45, what node is visited first?

52

Consider the following tree. 7 1 64 28 77 When inserting a new node with key 35, what node is visited second?

64

A new BST is built by inserting 255 nodes in sorted order. What is the tree height?

7

Consider the following tree. 7 1 64 28 77 When inserting a new node with key 35, what node is visited first?

7

Consider the following tree. 300 100 775 25 201 750 925 11 How many nodes are visited?

8

Parent

A node with a child is said to be that child's parent.

Internal node

A node with at least one child

Leaf

A tree node with no children

tree level.

All nodes with the same depth form

DFS is run on the following graph. Assume C is the starting vertex. A - B - C / \ / D - E Which vertices are in the stack after the first iteration of the while loop?

B, E

AVL TREEE private BinaryNode rotateRight(BinaryNode nodeN) { /* your code goes here */ }

BinaryNode nodeC = nodeN.getLeftChild(); nodeN.setLeftChild(nodeC.getRightChild()); nodeC.setRightChild(nodeN); return nodeC;

Perform a depth-first search of the graph below. Assume the starting vertex is E. A - B - C | \ | D - E - F Assume DFS traverses the following vertices: E, A, B, C. Which vertex is visited next?

D

If the current node matches the key, when does the algorithm return the node.

Immediately

Perform a depth-first search of the graph below. Assume the starting vertex is E. A - B - C | \ | D - E - F Which vertex is visited first?

E

500's successor is 850. 500 300 800 150 750 900 250 850 950

FALSE

DFS is run on the following graph. Assume C is the starting vertex. A - B - C / \ / D - E DFS terminates once all vertices are added to visitedSet.

False

The recursive DFS algorithm is run on the following graph. Assume D is the starting vertex. B D A | \ | \ | \ | C == E The recursive DFS algorithm uses a queue to determine which vertices to visit

False

public void breadthFirstTraversal(int start) { AQueue queue = new AQueue(); HashMap reached = new HashMap(); int current; for (int i = 0; i < adjacencyMap.CAPACITY; i++) { if (adjacencyMap.keys[i] != "empty") { reached.put(adjacencyMap.keys[i], false); } } queue.enqueue(start); reached.set(start, true); while (!(queue.isEmpty())) { int to; current = queue.dequeue(); /* your code goes here */ for (int i = 0; i < edgeList.getLength(); i++) { // getEntry is one based to = edgeList.getEntry(i+1); Boolean check = (Boolean) reached.get(to); if (!check) { queue.enqueue(to); reached.set(to, true); } } System.out.println("Vertex: " + current); } }

LList edgeList = (LList)adjacencyMap.get(current);

San Fransico -> Dallas <- ^ |/\ -/| | \/| Los Angles -> Tuscon Tucson is adjacent to _____.

Los Angeles

Does node 200 and the node's subtrees obey the BST ordering property? 500 200 750 150 800 900 201 850 950

NO

Does node 750 and the node's subtrees obey the BST ordering property? 500 200 750 150 800 900 201 850 950

NO

250's successor is 300. 500 300 800 150 750 900 250 850 950

True

950's predecessor is 900. 500 300 800 150 750 900 250 850 950

True

A weighted graph

associates a weight with each edge.

DFS is run on the following graph. Assume C is the starting vertex. A - B - C / \ / D - E Which vertices are in visitedSet after the first iteration of the while loop?

c

BinarySearchTree JAVA private void printInOrder(BinaryNode t) { /* your statements here */ }

if (t != null) { printInOrder(t.left); System.out.println(t.element); printInOrder(t.right); }

A directed graph is cyclic

if the graph contains a cycle, and acyclic if the graph does not contain a cycle.

Perform a depth-first search of the graph below. Assume the starting vertex is E. A - B - C | \ | D - E - F The DFS traversal of a graph is unique. Type: Yes or No

no

A vertex's predecessor pointer

points to the previous vertex along the shortest path from the start vertex.

Searching a BST in the worst case

requires h + 1 comparisons, meaning O(h) comparisons, where h is the tree height.

Consider the following tree. 300 100 775 25 201 750 925 11 Using left, current, and right, what ordering will print the BST from largest to smallest? Ex: An inorder traversal uses left current right.

right current left

A -> B -> C \ \ | > > > D < - E A directed edge connects vertices A and D. D is the _____ vertex.

terminating

binary search tree (BST)

which has an ordering property that any node's left subtree keys ≤ the node's key, and the right subtree's keys ≥ the node's key.

What is the worst case (largest) number of comparisons given a full BST with n nodes?' BST with n = 7

⌊log(n)⌋+1

Consider the following tree: 20 12 23 11 21 30 Where will a new node 11 be inserted? (So two nodes of 11 will exist).

11's right child

DFS is run on the following graph. Assume C is the starting vertex. A - B - C / \ / D - E In the second iteration, currentV = B. Which vertices are in the stack after the second iteration of the while loop?

A, C, D, E, E

DFS is run on the following graph. Assume C is the starting vertex. A - B - C / \ / D - E Which vertices are in the stack before the first iteration of the while loop?

C

DFS is run on the following graph. Assume C is the starting vertex. A - B - C / \ / D - E Which vertices are in visitedSet after the second iteration of the while loop?

C, B

private BinaryNode rotateRight(BinaryNode nodeN){ }

BinaryNode nodeC = nodeN.getLeftChild(); nodeN.setLeftChild(nodeC.getRightChild()); nodeC.setRightChild(nodeN); return nodeC;

private BinaryNode rotateLeftRight(BinaryNode nodeN){ }

BinaryNode nodeC = nodeN.getLeftChild(); nodeN.setLeftChild(rotateLeft(nodeC)); return rotateRight(nodeN);

AVL TREEE private BinaryNode rotateLeft(BinaryNode nodeN) { /* your code goes here */ }

BinaryNode nodeC = nodeN.getRightChild(); nodeN.setLeftChild(nodeC.getLeftChild()); nodeC.setLeftChild(nodeN); return nodeC;

private BinaryNode rotateLeft(BinaryNode nodeN){ }

BinaryNode nodeC = nodeN.getRightChild(); nodeN.setRightChild(nodeC.getLeftChild()); nodeC.setLeftChild(nodeN); return nodeC

private BinaryNode rotateRightLeft(BinaryNode nodeN){ }

BinaryNode nodeC = nodeN.getRightChild(); nodeN.setRightChild(rotateRight(nodeC); return rotateLeft(nodeN);

Perform a depth-first search of the graph below. Assume the starting vertex is E. A - B - C | \ | D - E - F Is the following a valid DFS traversal? Type: Yes or No E, D, F, A, B, C

No

20 12 23 11 21 30 Removing a node from an n-node nearly-full BST has what computational complexity

O(log(n))

edge

The link from a node to a child

Consider the following tree. 7 1 64 28 77 Where is the new node with key 35 inserted? Type: left or right

Right

Determine cur's next assignment given the key and current node. key 350, cur = 400 300 200 400 100 500

Search terminates and returns 0.

Root:

The one tree node with no parent (the "top" node).

A -> B -> C \ \ | > > > D < - E The airline routes graph is a digraph.

True

Remove an internal node with two children

This case is the hardest. First, the algorithm locates X's successor (the leftmost child of X's right subtree), and copies the successor to X. Then, the algorithm recursively removes the successor from the right subtree.

Does node 150 and the node's subtrees obey the BST ordering property? 500 200 750 150 800 900 201 850 950

YES

Does node 900 and the node's subtrees obey the BST ordering property? 500 200 750 150 800 900 201 850 950

YES

Is the tree a binary search tree? 500 200 750 150 800 900 201 850 950

YES

Would inserting 300 as the right child of 200 obey the BST ordering property (considering only nodes 300, 200, and 500)? 500 200 750 150 800 900 201 850 950

YES

Perform a depth-first search of the graph below. Assume the starting vertex is E. A - B - C | \ | D - E - F Is the following a valid DFS traversal? Type: Yes or No E, D, A, B, C, F

Yes

BST ordering: D b g a c f

a b c D f g

Bellman-Ford shortest path algorithm,

created by Richard Bellman and Lester Ford, Jr., determines the shortest path from a start vertex to each vertex in a graph.

Determine the insertion algorithm's next step given the new node's key and the current node 29 13 45 key = 7, cur = 29

cur = cur->left

Determine the insertion algorithm's next step given the new node's key and the current node 300 200 400 100 500 key = 600, cur = 400

cur = cur->right

Determine the insertion algorithm's next step given the new node's key and the current node 15 4 88 76 91 key = 53, cur = 76

cur->left = node

Determine the insertion algorithm's next step given the new node's key and the current node 20 12 23 11 21 30 key = 18, cur = 12

cur->right = node

binary tree's height is

h=⌊log(n)⌋

private boolean search(int x, BinaryNode t) { /* your statement here */ }

if (t == null) return false; if (x < t.element) return search(x,t.left); else if (x > t.element) return search(x, t.right); else return true; // Match

private BinaryNode insert(int x, BinaryNode t) { /* your statements here */ }

if (t == null) return new BinaryNode(x, null, null); if (x < t.element) t.left = insert(x, t.left); else if(x > t.element) t.right = insert(x , t.right); else ; // Duplicate; do nothing return t;

private BinaryNode remove(int x, BinaryNode t) { /* your statements here */ }

if (t == null) return t; // Item not found; do nothing if (x < t.element) t.left = remove(x, t.left); else if (x > t.element) t.right = remove(x, t.right); else if (t.left != null && t.right != null) // Two children { t.element = findMin(t.right).element; t.right = remove(t.element, t.right); } else t = (t.left != null) ? t.left : t.right; return t;

A path

is a sequence of directed edges leading from a source (starting) vertex to a destination (ending) vertex.

depth-first search (DFS)

is a traversal that visits a starting vertex, then visits every vertex along each path starting from that vertex to the path's end before backtracking. is a traversal that visits a starting vertex, then visits every vertex along each path starting from that vertex to the path's end before backtracking.

A cycle

is path that starts and ends at the same vertex.

A tree's height

is the largest depth of any node. A tree with just one node has height 0.

Recall that a tree's height

is the maximum edges from the root to any leaf.

A BST node's successor

is the node that comes after in the BST ordering, so in A B C, A's successor is B, and B's successor is C.

A node's depth

is the number of edges on the path from the root to the node. The root node thus has depth 0.

A vertex's distance

is the shortest path distance from the start vertex

public void breadthFirstTraversal(int start) { AQueue queue = new AQueue(); HashMap reached = new HashMap(); int current; for (int i = 0; i < adjacencyMap.CAPACITY; i++) { if (adjacencyMap.keys[i] != "empty") { reached.put(adjacencyMap.keys[i], false); } } /* enter your code here */ while (!(queue.isEmpty())) { int to; current = queue.dequeue(); LList edgeList = (LList) adjacencyMap.get(current); for (int i = 0; i < edgeList.getLength(); i++) { // getEntry is one based to = edgeList.getEntry(i+1); Boolean check = (Boolean) reached.get(to); if (!check) { queue.enqueue(to); reached.set(to, true); } } System.out.println("Vertex: " + current); } }

queue.enqueue(start); reached.set(start, true);

A BST node's predecessor is

the node that comes before in the BST ordering.

Determine the insertion algorithm's next step given the new node's key and the current node key = 87, cur = 0, tree->root = 0 (empty tree)

tree->root = node

A -> B -> C \ \ | > > > D < - E E is a _____ in the directed graph.

vertex


Ensembles d'études connexes

Micro Economics Exam II Textbook Checkpoints/Quizes

View Set

Intermediate Accounting Final Multiple Choice

View Set

Forelesning 2d - Cryptography (Digital Signature)

View Set

Electoral College and Role of the President

View Set

Dwyer MKTG 300: CH 3 - The Marketing Environment

View Set

Level 4: Real Estate Brokerage and Law of Agency

View Set

Chapter 3, Review Quiz, Quiz, Key Terms, etc.

View Set