Balanced Trees - Chapter 6 Flashcards
An blank is a BST with a height balance property and specific operations to rebalance the tree when a node is inserted or removed.
AVL tree
A BST is blank if for any node, the heights of the node’s left and right subtrees differ by only 0 or 1.
height balanced
A node’s blank is the left subtree height minus the right subtree height, which is 1, 0, or -1 in an AVL tree.
balance factor
With the height stored as a member of each node, the balance factor for any node can be computed in blank time
O(1)
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.
Adelson-Velsky and Landis,
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.
rotation
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.
rebalanced
blank an item into an AVL tree may cause the tree to become unbalanced. A rotation can rebalance the tree.
Inserting
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.
double rotation
An AVL blank involves searching for the insert location, inserting the new node, updating balance factors, and rebalancing.
tree insertion
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].
Insertion
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
}
}
AVLTreeInsert algorithm
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.
O(1)
O(log N)
Because a fixed number of temporary pointers are needed for the AVL insertion algorithm, including any rotations, the space complexity is blank.
O(1)
Given a key, an AVL tree blank removes the first-found matching node, restructuring the tree to preserve all AVL tree requirements.
remove operation
Removal begins by removing the node using the blank.
standard BST removal algorithm
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.
rebalanced
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.
BSTSearch
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.
O(1)
O(log N)
Because a fixed number of temporary pointers are needed for the AVL removal algorithm, including any rotations, the space complexity is blank.
O(1)
Because AVL Trees are a form of a blank, the AVLTree class is similar to the BinarySearchTree class.
binary search tree
In an AVL tree, A Node class is used that contains data members key, left, and right, as well as two new data members:
parent
height
blank - A pointer to the parent node, or None for the root.
parent
blank - The height of the subtree at the node. A single node has a height of 0.
height
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.
height
parent
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.
get_balance()
blank - Calculates the node’s current height and assigns the height data member with the new value.
update_height()
blank - Assigns either the left or right child data members with a new node.
set_child()
blank - Replaces a current child node with a new node.
replace_child()
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
rotate_left() and rotate_right()
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.
rebalance() method
AVL insertions are performed in two steps:
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.
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.
O(log n)