Binary trees:
Binary search
search for key k in sorted array A[l…r]
algorithm search (A, l, r, k):
recurrence equation:
$T(n) = \begin{cases} 1 & \text{if $n = 1$}\\ T(\frac{n}{2})+1 & \text{if $n>1$}\end{cases}$
in O(logn)
Lookup table:
Binary search tree:
AVL tree (balanced)
height of tree == height of root == max path to leaf
BST where for every node, absolute value of difference between left and right height is max 1
as in BST, but balanced:
insert/delete are more complicated, have to rebalance:
T1, T2, T3 and T4 are subtrees.
z y
/ \ / \
y T4 Right Rotate (z) x z
/ \ - - - - - - - - -> / \ / \
x T3 T1 T2 T3 T4
/ \
T1 T2
z z x
/ \ / \ / \
y T4 Left Rotate (y) x T4 Right Rotate(z) y z
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
T1 x y T3 T1 T2 T3 T4
/ \ / \
T2 T3 T1 T2
z y
/ \ / \
T1 y Left Rotate(z) z x
/ \ - - - - - - - -> / \ / \
T2 x T1 T2 T3 T4
/ \
T3 T4
z z x
/ \ / \ / \
T1 y Right Rotate (y) T1 x Left Rotate(z) z y
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
x T4 T2 y T1 T2 T3 T4
/ \ / \
T2 T3 T3 T4