223 BST and RBTrees Flashcards

1
Q

if removing a node with more than one child in a BST what do I do?

A

find the left most item in the right sub-tree of the node

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

how many nodes in a full binary tree

A

2^h -1

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

balance factor

A

left height - right height

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

General case for single rotation right

A

Node * k1 = k2 - > left ;
k2 - > left = k1 - > right ;
k1 - > right = k2 ;
return k1 ;

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

General case for single rotation left

A

Node * k1 = k2 - > right ;
k2 - > right = k1 - > let ;
k1 - > left = k2 ;
return k1 ;

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

when to do a double rotate in AVL

A

inserting into left childs right subtree, o right childs left subtree

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

double rotate with left child

A
{
root- > left = single_rotate_left ( root- > left );
return single_rotate_right ( root);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Double rotate with right child

A
{
root- > right = single_rotate_right ( root- > right );
return single_rotate_left ( root);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

basic insertion algorithm for AVL

A
  1. Recursively traverse path for insertion
  2. Insert node
  3. During backtracking, update heights and determine balance factors
  4. Rebalance (rotate) if node’s balance factor less than −1 or greater than 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

AVL tree insert time

A

O(log n)

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

valid red black tree constraints

A

The root is always Black

  1. “NULL” nodes are Black
  2. If a node is Red, it’s children cannot be Red
  3. All paths from a node to a NULL child has the same number of Black nodes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Inserting Nodes (Overview)

A

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

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