Top Banner
AVL Trees
21

AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

Jan 04, 2016

Download

Documents

Lizbeth Riley
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

AVL Trees

Page 2: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
Page 3: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

AVL Node Structure The AVL node structure follows the

same structure as the binary search tree, with the addition of a term to store the balance factor.

Node { int data; Node *left; Node *right; int balanceFactor;}

Page 4: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

AVL Insert Now that we have seen how to

balance a tree, we are ready to look at the algorithms.

The search and retrieval algorithms are the same as for any binary tree.

However, because the AVL tree is a special case of a binary search tree, you will want to use an inorder traversal method.

Page 5: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

AVL Insert As with the BST, all inserts take place at

a leaf (or leaf-like) node. To find the appropriate leaf node, we

follow the path from the root, going left when the new data node’s key is less than the root node’s key and right when it’s greater.

Once we have found the leaf, we connect the new node to the leaf and begin to back out of the tree.

Page 6: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

AVL Insert It is at this point that the AVL

insert differs from the BST insert. As we back out of the tree, we

constantly check the balance of each node.

When we find that a node is out of balance, we balance it and then continue up the tree.

Note that not all inserts will produce an out-of-balance tree.

Page 7: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

AVL Delete The delete logic is similar to the

BST delete logic. Again, however, we must make

sure that we include the logic to keep the tree balanced.

Page 8: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
Page 9: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
Page 10: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

AVL - height 1

Page 11: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

AVL - height 2

Page 12: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

AVL - height 3

Page 13: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

AVL - height 4

Page 14: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

AVL - height 5

Page 15: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

AVL - height 6

Page 16: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

Counting nodes

Height 0 1 2 3 4 5 6 7 8 9 10Num Nodes 1 2 4 7 12 20 33 54 88 143 232

Page 17: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

Relationship to Fibonacci Let N be the fewest number of

nodes in an AVL tree of height H It is straightforward to show thatN = F(H+3) - 1,where F(k) is the kth Fibonacci number

For large values of k, Fk 1

5

1 52

k

Page 18: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

number of nodes The fewest number of nodes in an

AVL tree with height H is given by

N 15

1 52

H3 1

Page 19: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.

Solving for H if we solve this near equality for H,

we getH 1.44 log2 N

This means that the height of an AVL tree with N nodes is no more than 44% larger than the optimal height of a binary search tree with N nodes

Page 20: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
Page 21: AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.