Balanced Trees - Chapter 6 Flashcards

1
Q

An blank is a BST with a height balance property and specific operations to rebalance the tree when a node is inserted or removed.

A

AVL tree

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

A BST is blank if for any node, the heights of the node’s left and right subtrees differ by only 0 or 1.

A

height balanced

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

A node’s blank is the left subtree height minus the right subtree height, which is 1, 0, or -1 in an AVL tree.

A

balance factor

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

With the height stored as a member of each node, the balance factor for any node can be computed in blank time

A

O(1)

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

AVL is named for inventors blank and blank who described the data structure in a 1962 paper: “An Algorithm for the Organization of Information”, often cited as the first balanced tree structure.

A

Adelson-Velsky and Landis,

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

Inserting an item into an AVL tree may require rearranging the tree to maintain height balance. A blank is a local rearrangement of a BST that maintains the BST ordering property while rebalancing the tree.

A

rotation

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

When an AVL tree node has a balance factor of 2 or -2, which only occurs after an insertion or removal, the node must be blank via rotations.

A

rebalanced

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

blank an item into an AVL tree may cause the tree to become unbalanced. A rotation can rebalance the tree.

A

Inserting

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

Sometimes, the imbalance is due to an insertion on the inside of a subtree, rather than on the outside as above. One rotation won’t rebalance. A blank is needed.

A

double rotation

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

An AVL blank involves searching for the insert location, inserting the new node, updating balance factors, and rebalancing.

A

tree insertion

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

Blank starts with the standard BST insertion algorithm. After inserting a node, all ancestors of the inserted node, from the parent up to the root, are rebalanced. A node is rebalanced by first computing the node’s balance factor, then performing rotations if the balance factor is outside of the range [-1,1].

A

Insertion

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

AVLTreeInsert(tree, node) {
if (tree⇢root == null) {
tree⇢root = node
node⇢parent = null
return
}

cur = tree⇢root
while (cur != null) {
if (node⇢key < cur⇢key) {
if (cur⇢left == null) {
cur⇢left = node
node⇢parent = cur
cur = null
}
else {
cur = cur⇢left
}
}
else {
if (cur⇢right == null) {
cur⇢right = node
node⇢parent = cur
cur = null
}
else
cur = cur⇢right
}
}

node = node⇢parent
while (node != null) {
AVLTreeRebalance(tree, node)
node = node⇢parent
}
}

A

AVLTreeInsert algorithm

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

The AVL insertion algorithm traverses the tree from the root to a leaf node to find the insertion point, then traverses back up to the root to rebalance. One node is visited per level, and at most 2 rotations are needed for a single node. Each rotation is an blank operation. Therefore, the runtime complexity of insertion is blank.

A

O(1)
O(log N)

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

Because a fixed number of temporary pointers are needed for the AVL insertion algorithm, including any rotations, the space complexity is blank.

A

O(1)

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

Given a key, an AVL tree blank removes the first-found matching node, restructuring the tree to preserve all AVL tree requirements.

A

remove operation

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

Removal begins by removing the node using the blank.

A

standard BST removal algorithm

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

After removing a node, all ancestors of the removed node, from the nodes’ parent up to the root, are blank. A node is rebalanced by first computing the node’s balance factor, then performing rotations if the balance factor is 2 or -2.

A

rebalanced

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

To remove a key, the AVL tree removal algorithm first locates the node containing the key using blank. If the node is found, AVLTreeRemoveNode is called to remove the node. Standard BST removal logic is used to remove the node from the tree. Then AVLTreeRebalance is called for all ancestors of the removed node, from the parent up to the root.

A

BSTSearch

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

In the worst case scenario, the AVL removal algorithm traverses the tree from the root to the lowest level to find the node to remove, then traverses back up to the root to rebalance. One node is visited per level, and at most 2 rotations are needed for a single node. Each rotation is an blank operation. Therefore, the runtime complexity of an AVL tree removal is blank.

A

O(1)
O(log N)

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

Because a fixed number of temporary pointers are needed for the AVL removal algorithm, including any rotations, the space complexity is blank.

A

O(1)

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

Because AVL Trees are a form of a blank, the AVLTree class is similar to the BinarySearchTree class.

A

binary search tree

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

In an AVL tree, A Node class is used that contains data members key, left, and right, as well as two new data members:

A

parent
height

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

blank - A pointer to the parent node, or None for the root.

A

parent

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

blank - The height of the subtree at the node. A single node has a height of 0.

A

height

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

The blank data member is used to detect imbalances in the tree after insertions or removals, while the blank data member is used during rotations to correct imbalances.

A

height
parent

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

blank - Returns the node’s balance factor. The balance factor is the left child’s height minus the right child’s height. A height of -1 is used in the calculation if the child is None.

A

get_balance()

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

blank - Calculates the node’s current height and assigns the height data member with the new value.

A

update_height()

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

blank - Assigns either the left or right child data members with a new node.

A

set_child()

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

blank - Replaces a current child node with a new node.

A

replace_child()

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

Tree rotations are required to correct any problems where the left and right subtrees of a node have heights that differ by more than 1. After a new node is inserted into an AVL tree, either one or two rotations will fix any imbalance that happens. The blank and blank methods perform these operations

A

rotate_left() and rotate_right()

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

The blank examines the structure of the subtree of a node, and determines which rotations to do if a height imbalance exists at the node.

A

rebalance() method

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

AVL insertions are performed in two steps:

A

Insert into the tree using the normal binary search tree insertion algorithm.
Call rebalance() on all nodes along a path from the new node’s parent up to the root.

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

Insert into the tree using the normal binary search tree insertion algorithm requires blank
steps, since an AVL tree has blank
levels, and the loop makes the current node go down one level with each iteration.

A

O(log n)

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

Call rebalance() on all nodes along a path from the new node’s parent up to the root. requires blank
steps, again since the path back up to the root has blank
levels to visit, and rotations are blank
operations.

A

O(log n)
O(log n)
O(1)

35
Q

The insert() method has a worst-case runtime of blank

A

O(log n)

36
Q

def insert(self, node):

# Special case: if the tree is empty, just set the root to
# the new node.
if self.root is None:
    self.root = node
    node.parent = None

else:
    # Step 1 - do a regular binary search tree insert.
    current_node = self.root
    while current_node is not None:
        # Choose to go left or right
        if node.key < current_node.key:
            # Go left. If left child is None, insert the new
            # node here.
            if current_node.left is None:
                current_node.left = node
                node.parent = current_node
                current_node = None
            else:
                # Go left and do the loop again.
                current_node = current_node.left
        else:
            # Go right. If the right child is None, insert the
            # new node here.
            if current_node.right is None:
                current_node.right = node
                node.parent = current_node
                current_node = None
            else:
                # Go right and do the loop again.
                current_node = current_node.right
        
    # Step 2 - Rebalance along a path from the new node's parent up
    # to the root.
    node = node.parent
    while node is not None:
        self.rebalance(node)
        node = node.parent
A

The AVLTree insert() method

37
Q

Removal from an AVL tree is a two-step process, similar to insertion:

A

Remove the node in the same way nodes are removed from a binary search tree. One of four cases is identified to determine how to remove the node. In the most general case, the node’s key is replaced by the successor node’s key in the tree, and the successor is then more easily removed.
Call rebalance() on all the nodes on the path from the removed node’s parent up to the root. If the node’s successor was ultimately removed, the rebalancing begins from the successor’s parent, not the original target node.

38
Q

As with insertion, in AVT removal each step requires blank
operations to be performed, first on a path from the root down to a leaf, and then on a path near a leaf back up to the root.

A

O(1)

39
Q

Because the height of an AVL tree is guaranteed to be blank
, the entire remove algorithm runs in worst-case blank time.

A

O(log n)

40
Q

def remove_node(self, node):
# Base case:
if node is None:
return False

# Parent needed for rebalancing.
parent = node.parent
    
# Case 1: Internal node with 2 children
if node.left is not None and node.right is not None:
    # Find successor
    successor_node = node.right
    while successor_node.left != None:
        successor_node = successor_node.left
        
    # Copy the value from the node
    node.key = successor_node.key
        
    # Recursively remove successor
    self.remove_node(successor_node)
        
    # Nothing left to do since the recursive call will have rebalanced
    return True

# Case 2: Root node (with 1 or 0 children)
elif node is self.root:
    if node.left is not None:
         self.root = node.left
    else:
         self.root = node.right

    if self.root is not None:
         self.root.parent = None

    return True

# Case 3: Internal with left child only
elif node.left is not None:
    parent.replace_child(node, node.left)
    
# Case 4: Internal with right child only OR leaf
else:
    parent.replace_child(node, node.right)
    
# node is gone. Anything that was below node that has persisted is already correctly
# balanced, but ancestors of node may need rebalancing.
node = parent
while node is not None:
    self.rebalance(node)            
    node = node.parent

return True
A

The AVLTree remove_node() method

41
Q

Often the user does not know where the desired node object to be removed is blank, or even if the node exists at all. The remove_key() method can be used to first search for the node using the BinarySearchTree search() method, and then call remove_node() only if search()returns a Node pointer.

A

in the tree

42
Q

binary search tree search operation. Returns the node with the

Searches for a node with a matching key. Does a regular
# matching key if it exists in the tree, or None if there is no
# matching key in the tree.
def search(self, key):
current_node = self.root
while current_node is not None:
# Compare the current node’s key with the target key.
# If it is a match, return the current key; otherwise go
# either to the left or right, depending on whether the
# current node’s key is smaller or larger than the target key.
if current_node.key == key: return current_node
elif current_node.key < key: current_node = current_node.right
else: current_node = current_node.left

Attempts to remove a node with a matching key. If no node has a matching key
# then nothing is done and False is returned; otherwise the node is removed and
# True is returned.
def remove_key(self, key):
node = self.search(key)
if node is None:
return False
else:
return self.remove_node(node)

A

The search() and remove_key() methods

43
Q

A blank is a BST with two node types, namely red and black, and supporting operations that ensure the tree is balanced when a node is inserted or removed.

A

red-black tree

44
Q

The red-black tree’s requirements ensure that a tree with N nodes will have a height of blank

A

O(log N)

45
Q

In a red black tree, Every node is colored either blank or blank.

A

red or black

46
Q

In a red black tree, the root node is blank.

A

black

47
Q

In a red black tree, All paths from a node to any null leaf descendant node must have the same number of blank.

A

black nodes

48
Q

In a red black tree, A null child is considered to be a blank.

A

black leaf node

49
Q

in a red black tree, A red node’s children cannot be blank.

A

red

50
Q

A rotation is a local rearrangement of a BST that maintains the BST ordering property while rebalancing the tree. blank are used during the insert and remove operations on a red-black tree to ensure that red-black tree requirements hold.

A

Rotations

51
Q

Rotating is said to be done “at” a node. A blank at a node causes the node’s right child to take the node’s place in the tree.

A

left rotation

52
Q

A blank at a node causes the node’s left child to take the node’s place in the tree.

A

right rotation

53
Q

The blank function sets a node’s left child, if the whichChild parameter is “left”, or right child, if the whichChild parameter is “right”, and updates the child’s parent pointer.

A

RBTreeSetChild utility

54
Q

The blank utility function replaces a node’s left or right child pointer with a new value.

A

RBTreeReplaceChild

55
Q

Given a new node, a red-black tree blank inserts the new node in the proper location such that all red-black tree requirements still hold after the insertion completes.

A

insert operation

56
Q

The red-black balance operation consists of the steps:

A

Assign parent with node’s parent, uncle with node’s uncle, which is a sibling of parent, and grandparent with node’s grandparent.
If node is the tree’s root, then color node black and return.
If parent is black, then return without any alterations.
If parent and uncle are both red, then color parent and uncle black, color grandparent red, recursively balance grandparent, then return.
If node is parent’s right child and parent is grandparent’s left child, then rotate left at parent, assign node with parent, assign parent with node’s parent, and go to step 7.
If node is parent’s left child and parent is grandparent’s right child, then rotate right at parent, assign node with parent, assign parent with node’s parent, and go to step 7.
Color parent black and grandparent red.
If node is parent’s left child, then rotate right at grandparent, otherwise rotate left at grandparent.

57
Q

Given a key, a red-black tree blank removes the first-found matching node, restructuring the tree to preserve all red-black tree requirements. First, the node to remove is found using BSTSearch. If the node is found, RBTreeRemoveNode is called to remove the node.

A

remove operation

58
Q

Given a key, a red-black tree blank removes the key from the tree, if present, restructuring as needed to preserve all red-black tree requirements. First, BSTSearch() is called to find the node containing the key. If the node is found, RBTreeRemoveNode() is called to remove the node.

A

remove-key operation

59
Q

RBTreeRemoveNode() consists of the following steps:

A

If the node has two children, copy the key from the node’s predecessor to a temporary value, recursively remove the predecessor from the tree, replace the node’s key with the temporary value, and return.
If the node is black, call RBTreePrepareForRemoval() to restructure the tree in preparation for the node’s removal.
Remove the node using the standard BST removal algorithm.

60
Q

Prepare-for-removal algorithm case descriptions -
Node is red or node’s parent is null.

A

None and stop

61
Q

Prepare-for-removal algorithm case descriptions -
Sibling node is red.

A

Color parent red and sibling black. If node is left child of parent, rotate left at parent node, otherwise rotate right at parent node.

Proceed

62
Q

Prepare-for-removal algorithm case descriptions -
Parent is black and both of sibling’s children are black.

A

Color sibling red and call removal preparation function on parent.

Stop

63
Q

Prepare-for-removal algorithm case descriptions -
Parent is red and both of sibling’s children are black.

A

Color parent black and sibling red.

Stop

64
Q

Prepare-for-removal algorithm case descriptions -
Sibling’s left child is red, sibling’s right child is black, and node is left child of parent.

A

Color sibling red and sibling’s left child black. Rotate right at sibling.

Proceed

65
Q

Prepare-for-removal algorithm case descriptions -
Sibling’s left child is black, sibling’s right child is red, and node is right child of parent.

A

Color sibling red and sibling’s right child black. Rotate left at sibling.

Proceed

66
Q

Preparation for removing a node first checks for each of the six cases, performing the operations

A

1 If the node is red or the node’s parent is null, then return.
2 If the node has a red sibling, then color the parent red and the sibling black. If the node is the parent’s left child then rotate left at the parent, otherwise rotate right at the parent. Continue to the next step.
3 If the node’s parent is black and both children of the node’s sibling are black, then color the sibling red, recursively call on the node’s parent, and return.
4 If the node’s parent is red and both children of the node’s sibling are black, then color the parent black, color the sibling red, then return.
5 If the sibling’s left child is red, the sibling’s right child is black, and the node is the left child of the parent, then color the sibling red and the left child of the sibling black. Then rotate right at the sibling and continue to the next step.
6 If the sibling’s left child is black, the sibling’s right child is red, and the node is the right child of the parent, then color the sibling red and the right child of the sibling black. Then rotate left at the sibling and continue to the next step.
7 Color the sibling the same color as the parent and color the parent black.
8 If the node is the parent’s left child, then color the sibling’s right child black and rotate left at the parent. Otherwise color the sibling’s left child black and rotate right at the parent.

67
Q

The RedBlackTree class is similar to the BinarySearchTree class because a red-black tree is a form of a binary search tree. The RBTNode class, representing red-black tree nodes, contains data members key, left, and right, as well as two new data members:

A

parent
color

68
Q

blank - A pointer to the parent node, or None for the root.

A

parent

69
Q

blank- A string representing the node’s color, set to either “red” or “black”.

A

color

70
Q

Returns true if both child nodes are black. A child set to None is considered to be black, as per the red-black tree requirements.

A

are_both_children_black()

71
Q

Returns the number of nodes in this subtree, including the node itself.

A

count()

72
Q

Returns this node’s grandparent, or None if this node has no grandparent.

A

get_grandparent()

73
Q

Returns the predecessor from this node’s left subtree.

A

get_predecessor()

74
Q

Returns this node’s sibling, which is the other child of this node’s parent. Returns None if this node has no sibling.

A

get_sibling()

75
Q

Returns this node’s uncle, which is the sibling of this node’s parent. Returns None if this node has no uncle.

A

get_uncle()

76
Q

Returns True if this node is black, False otherwise.

A

is_black()

77
Q

Returns True if this node is red, False otherwise.

A

is_red()

78
Q

Replaces a current child node with a new node. Takes two RBTNode arguments: current_child and new_child, and if current_child is one of the node’s children, replaces current_child with new_child.

A

replace_child()

79
Q

Assigns either the left or right child data members with a new node. Takes two arguments: which_child (string) and child (RBTNode), and sets child as the node’s left child if which_child is “left”, or sets child as the right child if which_child is “right”.

A

set_child()

80
Q

Blank are required to help correct red-black tree requirement violations during an insertion or removal. The rotate_left() and rotate_right() RedBlackTree class methods perform left and right rotations, respectively.

A

Tree rotations

81
Q

The RedBlackTree class contains 3 methods for insertion:

A

insert()
insert_node()
insertion_balance()

82
Q

Blank - Takes a key as an argument, creates a new RBTNode containing the key, and then adds the node to the tree using insert_node().

A

insert()

83
Q

blank - Takes a node as an argument, adds the node to the tree using standard BST insertion rules, and then calls insertion_balance() to balance the tree.

A

insert_node()

84
Q

Alters the tree as necessary to ensure red-black tree requirements are met after inserting a new node.

A

insertion_balance()