3 1 HeightBalanced Trees 3 2 WeightBalanced Trees
3. 1 Height-Balanced Trees 3. 2 Weight-Balanced Trees Chen Song
Height-Balanced Trees
Height-Balanced Trees Height – The maximum length of any path from the root to a leaf. Height-Balanced Tree – In each interior node, the height of the right subtree and left subtree differ by at most one.
Height-Balanced Trees Example:
Height-Balanced Trees
Height-Balanced Trees Tree T h-1 Tree T has at least leaves: leaves(h)=leaves(h-1)+leaves(h-2) leaves(0)=1 leaves(1)=2 Characteristic equation: xh=xh-1+xh-2 => x 2 -x-1=0 h-2
Height-Balanced Trees
Height-Balanced Trees 1. n->left->height = n->right->height+2 and n->left->height=n->right->height+1 Right rotation on node n 2. n->left->height = n->right->height+2 and n->left->height=n->right-height Left rotation on node n->left, and follow right rotation on node n
Height-Balanced Trees 3. n-> right->height = n->left->height+2 and n->right->height=n->left->height+1 Left rotation on node n->left 4. n->right->height = n->left->height+2 and n->right->height=n->left-height Right rotation on node n->right, and follow left rotation on node n
Height-Balanced Trees Height-Balanced Tree structure supports search, insert, and delete in O(logn) time Search => O(logn) Insert => search + insert + rebalance => O(logn) O(1) O(logn) Delete => search + delete + rebalance => O(logn) O(1) O(logn)
Weight-Balanced Trees
Weight-Balanced Trees Weight – The number of leaves of a tree. Weight-Balanced Tree – The weight of the right and left subtree in each node differ by at most one.
Weight-Balanced Trees α-weight-balanced trees For each subtree, the left and right sub-subtrees has each at least a fraction of α of total weight of the subtree. Tree T: T 1 T 2 αWT≤WT 1≤(1 -α)WT αWT≤WT 2≤(1 -α)WT
Weight-Balanced Trees
Weight-Balanced Trees
Weight-Balanced Trees
Weight-Balanced Trees Case 3: n->left->weight ≥ α*n->weight If n->right->weight > (α+ε)n->weight, do left rotation on node n Else right rotation on n->right, followed left rotation on node n.
Weight-Balanced Trees Theorem The weight-balanced tree structure supports search, insert, and delete in O(logn) time. Search => O(logn) Insert => search + insert + rebalance => O(logn) O(1) O(logn) Delete => search + delete + rebalance => O(logn) O(1) O(logn)
END
- Slides: 19