CSCI 251 Ch. 6 Trees

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Consider the following tree. Each question starts from the original tree. Use this text notation for the tree: (20 (12 (11, -), 23 (21, 30))). The - means the child does not exist. What is the tree after removing 12? (20 (- (11, -), 23 (21, 30))) (20 (11, 23 (21, 30)))

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

Consider the following tree. Each question starts from the original tree. Use this text notation for the tree: (20 (12 (11, -), 23 (21, 30))). The - means the child does not exist. What is the tree after removing 21? (20 (12 (11, -), 23 (-, 30))) (20 (12 (11, -), 23))

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

Consider the following tree. Each question starts from the original tree. Use this text notation for the tree: (20 (12 (11, -), 23 (21, 30))). The - means the child does not exist. What is the tree after removing 20? (21 (12 (11, -), 23 (-, 30))) (23 (12 (11, -), 30 (21, -)))

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

A tree with just one node has height _____

0

Assume a trie is built by executing the following code. trieRoot = new TrieNode() TrieInsert(trieRoot, "cat") TrieInsert(trieRoot, "cow") TrieInsert(trieRoot, "crow") If TrieInsert(trieRoot, "cow") is called a second time, _____ new nodes are created. 0 1 4

0

The root node thus has depth ____

0

A new BST is built by inserting nodes in this order: 6 2 8 What is the tree height? (Remember, the root is at height 0)

1

Searching a BST in the worst case requires H + ____ comparisons, meaning O(H) comparisons, where H is the tree height.

1

The following operations are executed on the above tree: BSTInsert(tree, node 70) BSTInsert(tree, node 56) BSTRemove(tree, 67) How many times is BSTRemoveNode called when removing node 67? 1 2 3

1

In searching for 145, what node is visited second?

100

What node is printed first?

11

Where will a new node 11 be inserted? (So two nodes of 11 will exist). 11's left child 11's right child

11's right child

Where will a new node 18 be inserted? 12's right child 11's right child

12's right child

In searching for 145, what node is visited third?

145

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

19

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

2

The following operations are executed on the above tree: BSTInsert(tree, node 70) BSTInsert(tree, node 56) BSTRemove(tree, 67) How many times is BSTInsertRecursive called when inserting node 56? 2 3 5

2

The following operations are executed on the above tree: BSTInsert(tree, node 70) BSTInsert(tree, node 56) BSTRemove(tree, 67) What is the maximum number of calls to BSTRemoveNode when removing one of the tree's nodes? 2 4 5

2 BSTRemoveNode is always called once by BSTRemove. BSTRemoveNode makes at most 1 recursive call in the case of removing an internal node with 2 children. That recursive call is guaranteed not to make additional recursive calls, so the maximum number of times BSTRemoveNode is called is 2.

When searching for key 21, what node is visited first? 20 21

20

When searching for key 21, what node is visited third? 21 30

21

When searching for key 21, what node is visited second? 12 23

23

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

254 The tree basically becomes a list, with every node having just one child.

When inserting a new node with key 35, what node is visited third?

28

How many calls to BSTSearchRecursive are made by calling BSTSearch(tree, 71)? 2 3 4

3

What is the depth of the "Mountains.jpg" file node? 3 4

3

What is the height of this tree? 3 4 14

3

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

3

What is the worst case (largest) number of nodes visited when searching for a key?

3

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

3 The tree has 3 levels. Iteration 1 checks the first level, 2 the second, 3 the third (which assigns current with null because a leaf has no children). Iteration 4 never occurs because the current node is null.

Assume a perfect 7-node BST. How many algorithm loop iterations will occur for an insert? 3 7

3 The tree has 3 levels. Iteration 1 visits the first level. Iteration 2 visits the second level. Iteration 3 visits the third level, whose node has no children, and inserts the new node as that node's child.

In searching for 145, what node is visited first?

300

Complete the tree traversal after node 300's left subtree has been printed. 11 25 100 201

300, 750, 775, 925

Which nodes would be visited when searching for 900? Write nodes in order visited, as: 5, 10

300, 900

Which nodes would be visited when searching for 800? Write nodes in order visited, as: 5, 10, 15

300, 900, 750

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

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

4

Assume a trie is built by executing the following code. trieRoot = new TrieNode() TrieInsert(trieRoot, "cat") TrieInsert(trieRoot, "cow") TrieInsert(trieRoot, "crow") When inserting "cat", _____ new nodes are created. 1 3 4

4

Assume a trie is built by executing the following code. trieRoot = new TrieNode() TrieInsert(trieRoot, "cat") TrieInsert(trieRoot, "cow") TrieInsert(trieRoot, "crow") When inserting "crow", _____ new nodes are created. 1 4 5

4

How many calls to BSTSearchRecursive are made by calling BSTSearch(tree, 49)? 3 4 5

4

How many internal nodes does the tree have?

4

What is the maximum possible number of calls to BSTSearchRecursive when searching the tree? 4 5

5

What is the worst case (largest) number of comparisons given a BST with N nodes?

5

What is the worst case (largest) number of comparisons given a BST with N nodes? Perfect BST with N = 31 31 4 5

5

When searching for key 45, what node is visited first?

52

When inserting a new node with key 35, what node is visited second?

64

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

7

When inserting a new node with key 35, what node is visited first?

7

How many nodes are visited?

8

Assume a perfect 255-node BST. How many algorithm loop iterations will occur for an insert? 8 255

8 The tree has 8 levels. Each iteration visits a level, descending left or right depending on the new and current nodes' keys. A main benefit of a BST is that inserts require only O(logN) iterations to find the proper insert location in a nearly-full N-node tree.

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

8 The tree has 8 levels. Each iteration visits one level.

TrieSearch will visit at most _____ nodes in a single search. 7 9 41

9

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

91

BST insert complexity

A BST with N nodes has at least log2N levels and at most N levels. Therefore, the runtime complexity of insertion is best case O(logN) and worst case O(N).

BST remove complexity

A BST with N nodes has at least log2N levels and at most N levels. Therefore, the runtime complexity of removal is best case O(logN) and worst case O(N).

Which operation would increase the height of the tree? Adding a new file into the user1 directory Adding a new directory into the "Image files" directory Adding a new directory into the "Research" directory

Adding a new directory into the "Research" directory

A binary tree used to store information for binary space partitioning

BSP tree

Starting from the root, the BST inorder traversal algorithm recursively prints the left subtree, the current node, and the right subtree.

BSTPrintInorder(node) { if (node is null) return BSTPrintInorder(node⇢left) Print node BSTPrintInorder(node⇢right) }

BST search algorithm

BSTSearch(tree, key) { cur = tree⇢root while (cur is not null) { if (key == cur⇢key) { return cur // Found } else if (key < cur⇢key) { cur = cur⇢left } else { cur = cur⇢right } } return null // Not found }

A technique of repeatedly separating a region of space into 2 parts and cataloging objects contained within the regions

Binary space partitioning (BSP)

The tree has four leaf nodes: Kumar, Lee, Gong, and _____.

Carvey

Trie insert rules

For each character C: 1. A new child node is added only if the current node does not have a child for C. 2. The current node pointer is assigned with the current node's child for C. 3. After all characters are processed, a terminal node is added and returned.

BST GetHeight rules

Given a node representing a BST subtree, the height can be computed as follows: 1. If the node is null, return -1. 2. Otherwise recursively compute the left and right child subtree heights, and return 1 plus the greater of the 2 child subtrees' heights.

Root node: _____

Jones

Smith's left child: ____

Kumar

The maximum N-node binary tree height is _____ (the - 1 is because the root is at height 0).

N - 1

The following operations are executed on the above tree: BSTInsert(tree, node 70) BSTInsert(tree, node 56) BSTRemove(tree, 67) Where is node 70 inserted? Node 67's left child Node 71's left child Node 71's right child

Node 71's left child

Consider the following tree. Each question starts from the original tree. Use this text notation for the tree: (20 (12 (11, -), 23 (21, 30))). The - means the child does not exist. Removing a node from an N-node nearly-full BST has what computational complexity? O(logN) O(N)

O(logN)

Determine cur's next assignment given the key and current node. key 350, cur = 400 Search terminates and returns null. 400 500

Search terminates and returns null.

What is the parent of the "Text files" node? The "user2" directory node The "Image files" directory node The "Sample1.txt" file node.

The "user2" directory node

If the child to be visited is null, when does the algorithm return null? Immediately Upon exiting the loop

Upon exiting the loop

A node's _______ include the node's parent, the parent's parent, etc., up to the tree's root.

ancestors

An especially useful form of binary tree is a ________ (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. That property enables fast searching for an item

binary search tree

In a _______, each node has up to two children, known as a left child and a right child.

binary tree

A binary tree is ________ if all levels, except possibly the last level, contain all possible nodes and all nodes in the last level are as far left as possible.

complete

Determine the insertion algorithm's next step given the new node's key and the current node. key = 7, currentNode = 29 currentNode⇢left = node currentNode = currentNode⇢right currentNode = currentNode⇢left

currentNode = currentNode⇢left

Determine the insertion algorithm's next step given the new node's key and the current node. key = 600, currentNode = 400 currentNode⇢left = node currentNode = currentNode⇢right currentNode⇢right = node

currentNode = currentNode⇢right

Determine the insertion algorithm's next step given the new node's key and the current node. key = 53, currentNode = 76 currentNode⇢left = node currentNode⇢right = node tree⇢root = node

currentNode⇢left = node

Determine the insertion algorithm's next step given the new node's key and the current node. key = 18, currentNode = 12 currentNode⇢left = node currentNode⇢right = node currentNode = currentNode⇢right

currentNode⇢right = node

The number of edges on the path from the root to the node

depth

The link from a node to a child

edge

A directory in a file system tree is always an internal node. True False

false

A's depth is 1. True False

false

Adding the string "balance" would create a new branch off the root node. True False

false

BSTGetParent always returns a non-null node when searching for a null node. True False

false

BSTInsert will not work if the tree's root is null. True False

false

BSTRemoveNode uses BSTRemoveKey. True False

false

BSTRemoveNode will not work to remove the last node in a tree. True False

false

D, F, and H form level 2. True False

false

The terminal nodes can be removed, and instead the last character of a string can be a leaf. True False

false

950's successor is 150. True False

false 950 is the largest BST item, so has no successor. The BST's largest item is the root's rightmost child: Follow right children until reaching a node with no right child (may be the root itself).

The worst-case time complexity for BSTGetHeight is O(log N), where N is the number of nodes in the tree. True False

false BSTGetHeight is called for every node in the tree, so the time complexity is O(N), not O(log N).

500's successor is 850. True False

false The next larger item is 750. For a node with a right subtree, the successor is the leftmost child of the right subtree.

When traversing down a BSP tree, half the objects are eliminated each level. True False

false The partitioning of space doesn't necessarily put half the objects in each half region. Ex: In the animation, C and D are the two halves split from the world's right half, but C contains 5 of the 7 objects and D the other 2.

Adding the string "balance" would create a new branch off the root node.

false The root already has a child for the character b and the character a is a child of b. The node for character a will be reused if "balance" is inserted.

The terminal nodes can be removed, and instead the last character of a string can be a leaf.

false Without the terminal nodes, knowing that both "cat" and "cats" are two separate strings in the same tree is not possible.

BSTRemoveKey will not properly update parent pointers when a non-root node is being removed. True False

false All cases properly update parent pointers. When removing a non-root node, BSTReplaceChild properly updates parent pointers.

A binary tree is _____ if every node contains 0 or 2 children

full

Identifying special types of binary trees. (full, complete, perfect) Tree 3:

full, complete, perfect

Identifying special types of binary trees (full, complete, perfect) Tree 1:

full, not complete, not perfect

A tree's _______ is the largest depth of any node

height

Recall that a tree's ______ is the maximum edges from the root to any leaf.

height

Trees are commonly used to represent ________ data

hierarchical

Searching a BST algorithm

if (currentNode⇢key == desiredKey) { return currentNode; // The desired node was found } else if (desiredKey < currentNode⇢key) { // Visit left child, repeat } else if (desiredKey > currentNode⇢key) { // Visit right child, repeat }

If the current node matches the key, when does the algorithm return the node? Immediately Upon exiting the loop

immediately

An ________ visits all nodes in a BST from smallest to largest, which is useful for example to print the tree's nodes in sorted order.

inorder traversal

Given a new node, a BST ______ operation inserts the new node in a proper location obeying the BST ordering property.

insert

Given a string, a trie _____ operation creates a path from the root to a terminal node that visits all the string's characters in sequence.

insert

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? Leaf Internal

internal

A node with at least one child

internal node

A tree node with no children

leaf

In a BST, if desired key equals current node's key, return found. If less, descend to _____ child. If greater, descend to _____ child.

left, right

BST ordering property: For three nodes, left child is ______-than-or-equal-to parent, parent is _____-than-or-equal-to right child. For more nodes, all keys in subtrees must satisfy the property, for every node.

less

All nodes with the same depth form a tree _______

level

A binary tree's height can be minimized by keeping all levels full, except possibly the last level. Such an "all-but-last-level-full" binary tree's height is H=⌊______⌋.

log2N

The minimum N-node binary tree height is h=⌊______⌋, achieved when each level is full except possibly the last.

log2N

A major BST benefit is that an N-node binary tree's height may be as small as O(_____), yielding extremely fast searches. Ex: A 10,000 node list may require 10,000 comparisons, but a 10,000 node BST may require only 14 comparisons.

logN

In a BTS, inserting items in nearly-sorted order leads to a nearly-________ tree height.

maximum

Searching a BST is fast if the tree's height is near the ______. Inserting items in random order naturally keeps a BST's height near the _______.

minimum

Does node 200 and the node's subtrees obey the BST ordering property? Yes No

no 201 is in 200's left subtree, but is not ≤ 200.

Does node 750 and the node's subtrees obey the BST ordering property? Yes No

no 800 is in 750's left subtree but is not ≤ 750

Is the tree a binary search tree? Yes No

no Each node and the node's subtrees must obey the BST ordering property.

Identifying special types of binary trees. (full, complete, perfect) Tree 4:

not full, complete, not perfect

Identifying special types of binary trees (full, complete, perfect) Tree 2:

not full, not complete, not perfect

BSTRemoveKey will not work if the key is not in the tree. True False

null

TrieSearch(root, "PINEAPPLE") returns _____. the trie's root node the terminal node for "APPLE" null

null

When searching for key 45, what node is visited third?

null

A node with a child

parent

A binary tree is ________, if all internal nodes have 2 children and all leaf nodes are at the same level.

perfect

A BST node's ________ is the node that comes before in the BST ordering

predecessor

Given a key, a BST ______ operation removes the first-found matching node, restructuring the tree to preserve the BST ordering property.

remove

Given a string, a trie _______ operation removes the string's corresponding terminal node and all non-root ancestors with 0 children.

remove

Where is the new node inserted? Type: left or right

right

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 node inserted into an empty tree will become the tree's _____

root

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

root

Given a key, a ______ algorithm returns the first node found matching that key, or returns null if a matching node is not found.

search

Given a string, a trie _______ operation returns the terminal node corresponding to that string, or null if the string is not in the trie.

search

BST GetHeight algorithm

see image

BST get parent algorithm

see image

BST insert algorithm

see image

BST remove algorithm

see image

BSTInsert algorithm for BSTs with nodes containing parent pointers

see image

BSTRemoveKey and BSTRemoveNode algorithms for BSTs with nodes containing parent pointers

see image

BSTReplaceChild algorithm

see image

BTS Search recursive algorithm

see image

Recursive BST insertion

see image

Recursive BST removal

see image

Trie insert algorithm

see image

Trie remove algorithm

see image

Trie search algorithm

see image

A BST node's ________ is the node that comes after in the BST ordering

successor

A ________ is a node that represents a terminating character, which is the end of a string in the trie.

terminal node

When searching for "PLUM", _____ visited. only the root node is the root and the root's 'P' child node are all nodes in the root's 'P' child subtree are

the root and the root's 'P' child node are

A _______ algorithm visits all nodes in the tree once and performs an operation on each node.

tree traversal

Determine the insertion algorithm's next step given the new node's key and the current node. key = 87, currentNode = null, tree⇢root = null (empty tree) tree⇢root = node currentNode⇢right = node currentNode⇢left = node

tree⇢root = node

A _____ (or prefix tree) is a tree representing a set of strings. Each node represents a single character and has at most one child per distinct alphabet character.

trie

950's predecessor is 900. True False

true

A BST implementation often includes a parent pointer inside each node. True False

true

A file in a file system tree is always a leaf node. True False

true

A tree with just one node has height 0. True False

true

All calls to BSTRemoveNode to remove a non-root node will result in a call to BSTReplaceChild. True False

true

B and C form level 1. True False

true

BST search can be implemented using recursion True False

true

BSTGetHeight returns 0 for a tree with a single node. True False

true

BSTGetParent returns null when the node argument is the tree's root. True False

true

BSTRemoveKey uses BSTRemoveNode. True False

true

BSTRemoveNode may use recursion. True False

true

BSTReplaceChild will not work if the parent pointer is null. True False

true

Certain binary tree structures can affect the speed of operations on the tree True False

true

E's depth is 2. True False

true

In a BST without parent pointers, a search for a node's parent can be implemented recursively. True False

true

The base case for BSTGetParentRecursive is when subtreeRoot is null or is node's parent. True False

true

The tree's height is 4. True False

true

Using a tree data structure to implement a file system requires that each directory node support a variable number of children. True False

true

The first node in the BST ordering is 150. True False

true 150 is the smallest item in the BST. The BST's first node is the leftmost child of the root: Follow left children until reaching a node with no left child (may be the root itself).

150's successor is 250. True False

true 250 is the next-larger item. If a node has a right subtree, the successor is the leftmost child of that subtree.

250's successor is 300. True False

true If a node has no right child, the successor will be the first ancestor having this node in a left subtree. A node's successor need not involve a direct link.

A BSP implementation could choose to split regions in arbitrary locations, instead of right down the middle. True False

true In practice, most BSP implementations partition according to how objects exist in the world. Regions are not always split down the middle, nor are the splits always horizontal or vertical.

Inserting a string that doesn't already exist in the trie requires allocation of at least 1 new node.

true The inserted string may be a prefix of another string in the trie, thus sharing existing nodes for each character, but a terminal node is still required for the new string.

BSTGetHeight would also work if the recursive call on the right child was made before the recursive call on the left child. True False

true The order of the two recursive calls does not matter. As long as the 2 recursive calls are made and the greater of the 2 returned heights is used, BSTGetHeight will properly determine the tree height.

In the animation, if the parts of the screen were in 2 different regions, then all objects from the 2 regions would have to be analyzed when rendering. True False

true The screen isn't guaranteed to be contained within a single BSP region, so analyzing objects from multiple regions may be required.

The base case for BSTGetHeight is when the node argument is null. True False

true When the node argument is null, BSTGetHeight returns -1 and does not make any recursive calls.

BSP can be used in 3-D graphics as well as 2-D. True False

true When used for 3-D rendering, each node still represents a region of space and the objects contained within. 3-D regions can be split in 2 using a plane. One child represents content on one side of the plane and the other child represents content on the other side.

Inserting a string that doesn't already exist in the trie requires allocation of at least 1 new node. True False

true a terminal node is required for the new string

The tree has an internal node, Gupta, with only one child rather than two. Is the tree still a binary tree? Type yes or no.

yes

Does node 150 and the node's subtrees obey the BST ordering property? Yes No

yes 150 ≤ 201. 150 has no left subtree, which is OK.

Would inserting 300 as the right child of 200 obey the BST ordering property (considering only nodes 300, 200, and 500)? Yes No

yes 200 ≤ 300. 200 and 300 are both ≤ 500.

Does node 900 and the node's subtrees obey the BST ordering property? Yes No

yes 850 ≤ 900, and 900 ≤ 950

Is the tree a binary tree? Yes No

yes Each binary tree node may each have up to two children; no ordering is required.

What is the worst case (largest) number of comparisons given a BST with N nodes? Perfect BST with N = 7 ⌊log2N⌋ ⌊log2N⌋+1 N

⌊log2N⌋+1


Set pelajaran terkait

Proquest Research Companion Library Quiz Module 7

View Set

SP302 - Introduction to Social and Developmental Psychology

View Set

Chapter 9_NEW! Mini Sim_Product Life Cycle MARK3300

View Set

PHYS1100: Final Exam Practice Questions

View Set

Hello. Начальный уровень.

View Set

art history correct test question examples

View Set

INFOSYS 110 UoA - Module 2: Systems

View Set