Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.

Similar presentations


Presentation on theme: "1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower."— Presentation transcript:

1 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower bounds on the heights of the subtrees of each node. –Force the subtrees of each node to have similar sizes (=number of nodes)

2 2 AVL Trees AVL tree = –A binary search tree –with the property: for every node, the heights of the left and right subtrees differ at most by one. Implementation issues: –Each node contains a value (-1, 1, 0) indicating which subtree is "heavier" –Insert and Delete are modified. They restructure the tree to make it balanced (if necessary).

3 3 AVL trees 1 1 0 01 0 nodes are marked with balance value

4 4 AVL trees: Fixing imbalances An imbalance is detected when the height difference between two subtrees of a node becomes greater than 1 or smaller than -1. There are two types of imbalances: 2 0 -2 1 0 2 1 0 0 or TYPE 1TYPE 2

5 5 AVL trees: Fixing imbalances Fixing an imbalance is done by rotating the tree. There are two types of rotation: –single rotation for TYPE 1 imbalances –double rotation for TYPE 2 imbalances consists of two single rotations. The rotations must always preserve the BST property.

6 6 AVL Trees: single rotation 6 4 2 6 4 2 node with imbalance A B C D ABCD This is a single right rotation. A single left rotation is symmetric. right rotate at node 6

7 7 AVL Trees: Double rotation 6 2 4 6 4 2 node with imbalance A B C D ABCD STEP 1: left rotate at node 2 6 4 2 A B C D STEP2: right rotate at node 6 node with imbalance

8 8 AVL Trees: Double rotation 6 2 4 6 4 2 node with imbalance A B C D ABCD If you want to do it in one step, imagine taking 4 and moving it up in between 2 and 6, so that 2 and 6 become its new children: B and C will then be adopted by 2 and 6 respectively, in order to maintain the BST property.

9 9 AVL Trees: Insert 1. Insert the node as in a BST 2. Starting at the newly inserted node, travel up the tree (towards the root), updating the balances along the way. a.If the tree becomes unbalanced, decide which rotation needs to be performed, rotate the tree and update the balances.

10 10 AVL Trees: Delete 1. Delete the node as in a BST 2. Starting at the newly inserted node, travel up the tree (towards the root), updating the balances along the way. a.If the tree becomes unbalanced, decide which rotation needs to be performed, rotate the tree and update the balances. b.Keep traveling towards the root, checking the balances. You may need to rotate again.

11 11 AVL Trees: Insert/Delete Insert –maximum possible number of rotations = 1 Delete –maximum possible number of rotations = lgn Worst case times –search: O(lgn) –insert: O(lgn) –delete: O(lgn) –one rotation: O(1)

12 12 AVL Trees: Efficiency It can be shown that the worst case height of an AVL tree is at most 44% larger than the minimum possible for a BST (i.e. approximately 1.44lgn)

13 13 Other balanced trees Red-black trees –Each node has an extra bit to hold a color –The tree stays balanced by placing restrictions on the way the nodes are colored Every path from the root to a leaf has the same number of black nodes and there cannot be consecutive red nodes. –A red-black tree is balanced when the longest path from the root to a leaf is at most twice the length of the shortest path from the root to a leaf. –A red-black tree with n nodes has height at most 2lg(n+1)

14 14 Other balanced trees α-balanced trees –α is a constant between 1/2 and 1 –A tree is α-balanced if, for every node x: size[left(x)]  α·size[x] size[right(x)]  α·size[x]


Download ppt "1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower."

Similar presentations


Ads by Google