Presentation is loading. Please wait.

Presentation is loading. Please wait.

3.1 Height-Balanced Trees 3.2 Weight-Balanced Trees

Similar presentations


Presentation on theme: "3.1 Height-Balanced Trees 3.2 Weight-Balanced Trees"— Presentation transcript:

1 3.1 Height-Balanced Trees 3.2 Weight-Balanced Trees
Chen Song

2 Height-Balanced Trees

3 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.

4 Height-Balanced Trees
Example:

5 Height-Balanced Trees
Theorem A height-balanced tree of height h has at least ( ) ( ) ℎ − ( 3− ) ( 1− ) ℎ leaves.

6 Height-Balanced Trees
Tree T 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 => x2-x-1=0

7 Height-Balanced Trees
Insert and Delete After insert or delete: Case 1: Tree is still balanced Case 2: Tree is not balanced at node n |n->left-height − n->right-height|=2

8 Height-Balanced Trees
1. n->left->height = n->right->height+2 and n->left->left->height=n->right->height+1 Right rotation on node n 2. n->left->height = n->right->height+2 and n->left->left->height=n->right-height Left rotation on node n->left, and follow right rotation on node n

9 Height-Balanced Trees
3. n-> right->height = n->left->height+2 and n->right->right->height=n->left->height+1 Left rotation on node n->left 4. n->right->height = n->left->height+2 and n->right->right->height=n->left-height Right rotation on node n->right, and follow left rotation on node n

10 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(logn) O(1) O(logn) Delete => search + delete + rebalance => O(logn) O(logn) O(1) O(logn)

11 Weight-Balanced Trees

12 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.

13 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. αWT≤WT1≤(1-α)WT αWT≤WT2≤(1-α)WT Tree T: T1 T2

14 Weight-Balanced Trees
Theorem An α-weight-balanced tree of height h≥2 has at least ( 1 1−α ) ℎ leaves.

15 Weight-Balanced Trees
Rebalance n is current node α∈[ 2 7 , 1− ] Case 1: n->left->weight ≥ α*n->weight and n->right->weight ≥ α*n->weight No rebalancing

16 Weight-Balanced Trees
Case 2: n->right->weight ≥ α*n->weight If n->left->left->weight > (α+ε)n->weight, do right rotation on node n Else left rotation on n->left, followed right rotation on node n. ε ≤ α2-2α+ 1 2

17 Weight-Balanced Trees
Case 3: n->left->weight ≥ α*n->weight If n->right->right->weight > (α+ε)n->weight, do left rotation on node n Else right rotation on n->right, followed left rotation on node n.

18 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(logn) O(1) O(logn) Delete => search + delete + rebalance => O(logn) O(logn) O(1) O(logn)

19 END


Download ppt "3.1 Height-Balanced Trees 3.2 Weight-Balanced Trees"

Similar presentations


Ads by Google