Test 2, Red Black (AVL) Trees
Describe the recurrence relation of Merge Sort (what each letter a, b, c(n), d(n) means)
T(n) = 2T(n/2) + O(n) + O(n); a = 2 means that merge sort splits the problem into two subproblems each recursive call b = 2 means that each subproblem receives half the input c(n) = O(n) means that it takes linear time to get to the bas case of the recursion (height of the tree is n) d(n) = n means that each time 2 subproblems rejoin, it takes n times (the conquer part)
How do you remove a node form a binary tree?
if there are two children, replace the node with the minimum element in the right subtree and then remove the right subtree's min; otherwise set node to child
Insertion Case 2 (x's parent's sibling is black and x is a right child of x's parent) (insert node x) into Red-Black Tree
left rotation is used to to transform into case 3 (x transforms to its parent and x's parent transforms into x's left child); O(1)
What is the balance factor of a tree node?
the difference between the heights of its left and right subtree (hl - hr)
black height of a node x bh(x)
the number of bodes on any path from x (not including x)
How do you solve a recurrence relation using the tree method?
1. Draw a tree where each node is the number of operations for each recursive call 2. keep track of the recursive call, and sum of all the nodes 3. find the kth recursive call and sum of nodes 4. use the bas case to find the height of the tree 5. find the sum from 0 to the height of the kth sum of all nodes
How do you solve a recurrence relation using the iterating method?
1. Rewrite the recurrence relation in terms of the next recursive call 2. write the relation on the kth recursion 3. Set the remaining recursive calls in the kth equation to 1 (using the base case) and solve for k (in terms of n) 4. substitute all k's in the equation as terms of n 5. solve for Big-O
In place algorithm
Algorithm does not need extra memory space to manipulate input (may require small amounts of extra space for variables)
What is the recurrence relations and big-O of best case, worst case, and average case for quick sort? what scenarios are best case, worst case, and average case occurring?
Best = 2T(n/2) + O(n) = O(nlog(n)) Worst case = T(n-1) + O(n) = O(n^2) Average case = T(cn) + T((1-c)n) + O(n) (0 < c < (1/2)) = O(nlog(n)) Best Case occurs when the pivot point is the middle of an already sorted list; Worst case occurs when the pivot point is the beginning of an already sorted list
What is a sorting algorithm based off a binary tree?
Build the binary tree - O(nlogn) best case; O(n^2) worst case - Run in order traversal O(n)
Red-Black Tree
Every node in the tree is either red or black the root an every leaf node (null) is black if a node is red, then both it children are black (no two consecutive red nodes) every simple path from node to descendant leaf contains the same number of black nodes (black height)
How do you make a binary expression tree out of a postfix expression?
If token (t) is an operand - push t to NodeStack if t is a binary operator - pop two values, make them children of t, push t to NodeStack if t is a uniary operator - pop 1 value, make it a child o t, and push t
How do you evaluate a postfix expression using a stack?
If token (t) is an operand - push t to stack if t is a binary operator - pop two values, evaluate the binary expression, push result to valStack if t is a uniary operator - pop 1 value, evaluate, and push result
Typical growth rates in increasing order:
Linear: 1 (constant) log(n) (logarithmic) n (linear) n^2 (quadradic) Exponential: 2^n e^n n! n^n
What is the big-O of best case, worst case, and average case for merge sort? what scenarios are best case, worst case, and average case occurring?
Merge sort performs the same number of comparisons in all cases: Best = Worst = Average = O(nlog(n))
What is the big-O of search, insert, delete of a binary tree?
O(h) - h = height
What is the big-O of insertions and deletions of nodes in an AVL tree?
O(logn)
What is the time complexity of inserting a node in a red-black tree?
O(logn)
what is the height of a red black tree with n nodes?
O(logn)
Which tree traversal should be used when evaluating a binary operator tree?
Post-order (then you get postfix notation)
How do you write a recurrence relation/equation for a recursive function?
T(n) = aT(n/b) + c(n) + d(n) a = number of subproblems each recursive call splits into b = number the input size is divided by c = length it takes to form all subproblems d = length to combine the solutions of the subproblems; The base case is the n where the algorithm stops splitting
Masters Theorem
T(n) = aT(n/b) + cn^d; if a < b^d; f(n) = O(n^d) if a = b^d; f(n) = O(n^(d) * log(n)) if a > b^d; f(n) = O(n^(log_b(a)))
Insertion Case 3 (x's parent's sibling is black and x is the left child of it's parent) (insert node x) into Red-Black Tree
X's parent and x's grandparent change color and x's parent becomes the new grandparent (right rotation); O(1)
Is Merge Sort or Quick Sort in place algorithms?
Yes, by recursively calling Merge Sort on a sub-set of the input array Quick sort is in place
2-4 Tree
an AVL tree where each internal node has anywhere form 1-3 values, each internal node has anywhere from 2-4 children; the keys inside the nodes are labeled as k1...k3 and the children of each node as c1 ... c4
Insertion Case 1 (x's parent and x's parent's sibling is red) (insert node x) into Red-Black Tree
change the parent and parent's sibling to black, change the grandparent to red; O(logn)
Proper/full binary tree
every node other than the leaves have two children
How do you convert an expression from infix to postfix form using que and stack?
output = que, uses opStack to keep operations; if token (t) is an operand - place t in output; if t is an operator and OpStack is empty - push t if t is an operator and t has > precedence - push t if t is an operator and t has <= precedence - pop the stack until t has greater precedence or stack is empty, push t; For parenthisis: have highest precedence (doesn't pop any operators until corresponding parenthesis found)
Insert x into 2-4 using split method
this occurs when inserting a node (x) causes an overflow; Split into two nodes A & B where: A gets the 1st and 2nd keys (and the 1st 2nd and 3rd children) the 3rd key gets moved to the parent (or becomes new root if there is no parent) B gets the remaining key and children; may occur an overflow in the parent, if this occurs, repeat the split with at the parent; O(logn)
Post-order traversal of binary search tree
visit left node, then right node, then root
In-order traversal of binary search tree
visit left node, then root, then right node
Pre-order traversal of binary search tree
visit root, then left node, then right node
When does a rotation need to occur on a AVL tree?
when the abs val of the balance factor of any node is greater than 1