Trees - Chapter 5 Flashcards

1
Q

In a list, each node has up to one successor. In a blank, each node has up to two children, known as a left child and a right child.

A

binary tree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Blank: A tree node with no children.

A

Leaf

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Blank: A node with at least one child.

A

Internal node

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Blank A node with a child is said to be that child’s blank.

A

Parent:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A node’s blank include the node’s parent, the parent’s parent, etc., up to the tree’s root.

A

ancestors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Blank: The one tree node with no parent (the “top” node).

A

Root

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The link from a node to a child is called an blank.

A

edge

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

A node’s blank is the number of edges on the path from the root to the node

A

depth

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The root node thus has depth blank.

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

All nodes with the same depth form a tree blank

A

level

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

A tree’s blank is the largest depth of any node.

A

height

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

A tree with just one node has height blank

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

full

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

A binary tree is blank 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.

A

complete

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

perfect

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Trees are commonly used to represent blank

A

hierarchical data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

A blank can represent files and directories in a file system, since a file system is a hierarchy.

A

tree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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

A

Binary space partitioning (BSP)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

A blank is a binary tree used to store information for binary space partitioning.

A

BSP tree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Each node in a BSP tree contains information about a blank and which objects are contained in the region.

A

region of space

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

In graphics applications, a BSP tree can be used to store all objects in a blank. The BSP tree can then be used to efficiently determine which objects must be rendered on screen.

A

multidimensional world

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Most of the tree data structures discussed in this book serve to store a collection of values. Numerous tree types exist to store blank in a structured way that allows for fast searching, inserting, and removing of values.

A

data collections

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

An especially useful form of binary tree is a blank, 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

A

binary search tree (BST)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

To search nodes means to find a node with a desired key, if such a node exists. A BST may yield faster searches than a blank Searching a BST starts by visiting the root node (which is the first currentNode below):

A

list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Searching a BST in the worst case requires H + 1 comparisons, meaning blank comparisons, where H is the tree height.

A

O(H)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

A major BST benefit is that an N-node binary tree’s height may be as small as blank, yielding extremely fast searches.

A

O(logN)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

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 blank

A

H = floor(log2 N)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

BST defines an ordering among nodes, from smallest to largest. A BST node’s blank 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

successor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

A BST node’s blank is the node that comes before in the BST ordering.

A

predecessor

30
Q

If a node has a right subtree, the node’s successor is that right subtree’s blank:

A

leftmost child

31
Q

Starting from the right subtree’s root, follow left children until reaching a node with no left child (may be that subtree’s root itself). If a node doesn’t have a right subtree, the node’s successor is the first blank having this node in a left subtree.

A

ancestor

32
Q

A simple BST blank checks the current node (initially the tree’s root), returning that node as a match, else assigning the current node with the left (if key is less) or right (if key is greater) child and repeating. If such a child is null, the algorithm returns null (matching node not found).

A

search algorithm

33
Q

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

A

insert

34
Q

Blank: If the new node’s key is less than the current node, and the current node’s left child is null, the algorithm assigns that node’s left child with the new node.

A

Insert as left child

35
Q

Blank: If the new node’s key is greater than or equal to the current node, and the current node’s right child is null, the algorithm assigns the node’s right child with the new node.

A

Insert as right child

36
Q

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

A

Search for insert location

37
Q

The BST insert algorithm traverses the tree from the root to a leaf node to find the insertion location. One node is visited per level. A BST with N nodes has at least blank levels and at most blank levels.

A

log2N
N

38
Q

For a BST, the runtime complexity of insertion is best case blank
and worst case blank

A

O(logN)
O(N)

39
Q

The space complexity of insertion for a BST is blank because only a single pointer is used to traverse the tree to find the insertion location.

A

O(1)

40
Q

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

A

remove

41
Q

Blank: 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 null. Else, if X was the root, the root pointer is assigned with null, and the BST is now empty.

A

Remove a leaf node

42
Q

Blank: 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.

A

Remove an internal node with single child

43
Q

Blank: 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.

A

Remove an internal node with two children

44
Q

STRemove(tree, key) {
parent = null
currentNode = tree⇢root
while (currentNode != null) {
// Check if currentNode has an equal key
if (currentNode⇢key == key) {
if (currentNode⇢left is null && currentNode⇢right is null) {
// Remove leaf

        if (parent is null) { // Node is root
           tree⇢root = null
        }
        else if (parent⇢left == currentNode) {
           parent⇢left = null
        }
        else {
           parent⇢right = null
        }
        return true // Node found and removed
     }
     else if (currentNode⇢right is null) {
        // Remove node with only left child
        
        if (parent is null) { // Node is root
           tree⇢root = currentNode⇢left
        }
        else if (parent⇢left == currentNode) {
           parent⇢left = currentNode⇢left
        }
        else {
           parent⇢right = currentNode⇢left
        }
        return true // Node found and removed
     }
     else if (currentNode⇢left is null) {
        // Remove node with only right child
        
        if (parent is null) { // Node is root
           tree⇢root = currentNode⇢right
        }
        else if (parent⇢left == currentNode) {
           parent⇢left = currentNode⇢right
        }
        else {
           parent⇢right = currentNode⇢right
        }
        return true // Node found and removed
     }
     else {
        // Remove node with two children
        
        // Find successor (leftmost child of right subtree)
        successor = currentNode⇢right
        while (successor⇢left is not null) {
           successor = successor⇢left
        }

        // Copy successor's key to current node
        currentNode⇢key = successor⇢key
        Parent = currentNode

        // Reassign currentNode and key so that loop continues with new key
        currentNode = currentNode⇢right
        key = successor⇢key
     }
     return // Node found and removed
  }
  else if (currentNode⇢key < key) {
     // Search right
     parent = currentNode
     currentNode = currentNode⇢right
  }
  else {
     // Search left
     parent = currentNode
     currentNode = currentNode⇢left
  }    }    return false // Node not found }
A

BST remove algorithm.

45
Q

The blank traverses the tree from the root to find the node to remove. When the node being removed has two children, the node’s successor is found and a recursive call is made. One node is visited per level, and in the worst case scenario the tree is traversed twice from the root to a leaf.

A

BST remove algorithm

46
Q

A BST with N nodes has at least log2N
levels and at most N levels. So removal’s worst case time complexity is blank for a BST with log2N
levels and worst case blank
for a tree with N levels.

A

O(logN)
O(N)

47
Q

In BST removal, two pointers are used to traverse the tree during removal. When the node being removed has two children, a third pointer and a copy of one node’s data are also used, and one recursive call is made. So removal’s space complexity is always
blank.

A

O(1)

48
Q

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

A

tree traversal

49
Q

An blank visits all nodes in a BST from smallest to largest, which is useful for example to print the tree’s nodes in sorted order. Starting from the root, the algorithm recursively prints the left subtree, the current node, and the right subtree.

A

inorder traversal

50
Q

The minimum N-node binary tree height is blank
, achieved when each level is full except possibly the last. The maximum N-node binary tree height is N - 1 (the - 1 is because the root is at height 0).

A

h = floor(log2 N)

51
Q

Given a node representing a BST subtree, the height can be computed as follows:

If the node is null, return blank.
Otherwise recursively compute the left and right child subtree heights, and return blank.

A

-1
1 plus the greater of the 2 child subtrees’ heights

52
Q

A BST implementation often includes a blank inside each node. A balanced BST, such as an AVL tree or red-black tree, may utilize the parent pointer to traverse up the tree from a particular node to find a node’s parent, grandparent, or siblings. The BST insertion and removal algorithms below insert or remove nodes in a BST with nodes containing parent pointers.

A

parent pointer

53
Q

BST search can be implemented using blank. A single node and search key are passed as arguments to the recursive search function. Two base cases exist. The first base case is when the node is null, in which case null is returned. If the node is non-null, then the search key is compared to the node’s key. The second base case is when the search key equals the node’s key, in which case the node is returned. If the search key is less than the node’s key, a recursive call is made on the node’s left child. If the search key is greater than the node’s key, a recursive call is made on the node’s right child.

A

recursion

54
Q

A recursive BST get-parent algorithm searches for a parent in a way similar to the normal BST search algorithm. But instead of comparing the search key with a candidate node’s key, the search key is compared with the keys of the candidate blank.

A

node’s children

55
Q

BST insertion and removal can also be implemented using blank. The insertion algorithm uses recursion to traverse down the tree until the insertion location is found. The removal algorithm uses the recursive search functions to find the node and the node’s parent, then removes the node from the tree. If the node to remove is an internal node with 2 children, the node’s successor is recursively remove

A

recursion

56
Q

A blank is a tree representing a set of strings.

A

trie (or prefix tree)

57
Q

In a trie, each non-root node represents a single blank. Each node has at most one child per distinct alphabet character.

A

character

58
Q

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

A

terminal node

59
Q

Tries provide efficient storage and quick search for strings, and are often used to implement blank and blank

A

auto-complete and predictive text input.

60
Q

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

A

insert

61
Q

current node pointer initially points to the root. A loop then iterates through the string’s characters. For each character C:

After all characters are processed, a terminal node is added and returned.

A

A new child node is added only if the current node does not have a child for C.
The current node pointer is assigned with the current node’s child for C.

62
Q

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

A

trie search

63
Q

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

A

remove operation

64
Q

Implementations commonly use a lookup table for a trie node’s children, allowing retrieval of a child node from a character in blank time. Therefore, to insert, remove, or search for a string of length M in a trie takes blank time. The trie’s current size does not affect each operation’s time complexity.

A

O(1)
O(M)

65
Q

A binary search tree can be implemented in Python using blank and blank.

A

a Node class and a BinarySearchTree class

66
Q

The blank class contains a key value and data members for the left and right children nodes.

A

Node

67
Q

The blank contains a data member for the tree’s root (a Node object).

A

BinarySearchTree class

68
Q

The binary search tree search algorithm first assigns current_node with the root. A loop is then entered, and one of three cases occurs:

A

Case 1: The current node’s key matches the desired key, so current_node is returned from the method.
Case 2: The desired key is less than the current node’s key, so current_node is assigned with current_node.left.
Case 3: The desired key is greater than the current node’s key, so current_node is assigned with current_node.right.

69
Q

The blank is used to insert a new node into the tree. If the tree is empty, then the root data member is assigned with the new node. If the tree is not empty, current_node is assigned with the root node, and a loop is entered. Inside the loop, if the new node’s key is less than the current node’s key, then the new node is inserted as the current node’s left child (if the current node has no left child), or current_node is assigned with current_node.left. If the new node’s key is greater than or equal to the current node’s key, then the new node is inserted as the current node’s right child (if the current node has no right child), or current_node is assigned with current_node.right.

A

insert() method

70
Q

The remove() method is more complicated than search() and insert() because several cases exist. Different actions are taken depending on what the kind of node is being removed.

A

Case 1: The node being removed is a leaf. The parent’s left or right data member is assigned with None, depending on which side the node is.
Case 2: The node being removed has one child. The parent’s left or right data member is assigned with the removed node’s single child.
Case 3:The node being removed has two children. The node’s key is replaced by the successor’s key, and then the successor (which will fall under either Case 1 or Case 2) is removed. The successor is the next largest node in the tree, and is always the right child’s leftmost child.