223 BST and RBTrees Flashcards
if removing a node with more than one child in a BST what do I do?
find the left most item in the right sub-tree of the node
how many nodes in a full binary tree
2^h -1
balance factor
left height - right height
General case for single rotation right
Node * k1 = k2 - > left ;
k2 - > left = k1 - > right ;
k1 - > right = k2 ;
return k1 ;
General case for single rotation left
Node * k1 = k2 - > right ;
k2 - > right = k1 - > let ;
k1 - > left = k2 ;
return k1 ;
when to do a double rotate in AVL
inserting into left childs right subtree, o right childs left subtree
double rotate with left child
{ root- > left = single_rotate_left ( root- > left ); return single_rotate_right ( root);
Double rotate with right child
{ root- > right = single_rotate_right ( root- > right ); return single_rotate_left ( root);
basic insertion algorithm for AVL
- Recursively traverse path for insertion
- Insert node
- During backtracking, update heights and determine balance factors
- Rebalance (rotate) if node’s balance factor less than −1 or greater than 1
AVL tree insert time
O(log n)
valid red black tree constraints
The root is always Black
- “NULL” nodes are Black
- If a node is Red, it’s children cannot be Red
- All paths from a node to a NULL child has the same number of Black nodes
Inserting Nodes (Overview)
Always assign inserted nodes the color Red
• If assigned Black, could make path with too many Black nodes
2. If the parent of the inserted node is Black, we are done!
• Inserting into the root is a special case … just color it Black
3. If the parent is Red, we violate the Red-Black properties
• Have to “rebalance” using tree rotations and apply color changes