Trees + AVL Flashcards
A tree designed for locating specific keys (values)
Search Trees
Operations of Search Trees
SEARCH
MINIMUM
MAXIMUM
SUCCESSOR
PREDECESSOR
INSERT
DELETE
Is a binary tree that satisfies the following properties:
For any node x:
- the keys in the left subtree of x are <= x.key
- the keys in the right subtree of x are >= x.key
Binary Search Tree (BST)
Search begins at the _________ and traces a __________ path in the tree using BST Property
root, downward
Search Pseudocode: TreeSearch(x,k)
Recursive Approach
TreeSearch(x,k){
if x == NULL or k == x.key
return x
if k < x.key return TreeSearch(x.left, k) else return TreeSearch(x.right, k) }
Search Pseudocode: TreeSearch(x,k)
Iterative Approach
TreeSearch(x,k){
while x != NULL and k != x.key
if k < x.key
x = x.left
else
x = x.right
return x
}
To get the minimum, just follow the ________________ pointers from the root
left-child
Tree-Minimum Pseudocode
TreeMinimum(x){
while x.left != NULL
x = x.left
return x }
To get the maximum, just follow the ________________ pointers from the root
right-child
Tree-Maximum Pseudocode
Tree-Maximum(x){
while x.right != NULL
x = x.right
return x
}
The ______________ of a node is the next node visited in an inorder traversal
Successor
TRUE or FALSE
If all keys are distinct, the successor of a node x is the node with the smallest key greater than x.key
TRUE
If the right subtree of node x is non-empty, then the successor of x is the ___________________________.
leftmost node in x’s right subtree (minimum of right-subtree)
If the right subtree of node x is empty, then the successor of x is the ______________ of x whose ______________ is also an ancestor of x.
lowest ancestor, left-child
Tree-Successor Pseudocode
Tree-Succcessor(x){
if x.right != NULL
return Tree-Minimum(x.right)
else y = x.parent while y != NULL and x == y.right x = y y = y.parent return y }
The ______________ of a node is the previous visited node in an inorder traversal
Predecessor
If the left subtree of node x is non-empty, then the predecessor of x is the ___________________________.
rightmost node in x’s left subtree (maximum of left subtree)
If the left subtree of node x is empty, then the predecessor of x is the ______________ of x whose ______________ is also an ancestor of x.
lowest ancestor, right-child
Tree-Predecessor Pseudocode
Tree-Predecessor(x){
if x.left != NULL
return Tree-Maximum(x.left)
else y = x.parent while y != NULL and x == y.left x = y y = y.parent return y }
___________ and ___________ operations change BST in a way that the BST property is preserved
Insertion, Deletion
_________ searches for a NULL to replace with the new node Z
Insertion
Tree-Insert Pseudocode
Tree-Insert(T,x){
x = T.root
y = NULL
while x != NULL y = x if z.key <= x.key x = x.left else x = x.right z.p = y if y == NULL T.root = z else if z.key <= y.key y.left = z else y.right = z }
3 Cases for Node Deletion in BST
Case 1: No Children
Case 2: Has 1 Child
Case 3: Has 2 Children
Case 1 (No Children) deletion fix-up
Remove it by replacing its parent’s child pointer to NULL
Case 2 (1 Child) deletion fix-up
Elevate the child to take z’s position in the tree. Modify z’s parent to replace z with z’s child
(z here is the node to be deleted)