introduction to avl trees p. 839 – 854. intro review of binary trees: –binary trees are useful...

22
INTRODUCTION TO AVL TREES P. 839 – 854

Upload: lynn-wheeler

Post on 27-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

INTRODUCTION TO AVL TREES

P. 839 – 854

INTRO

Review of Binary Trees:– Binary Trees are useful for quick retrieval of items

stored in the tree– order of items in the tree is important factor of how

quickly this retrieval is done• if well organized, items can be handled quickly• if not well structured, retrieval can be cumbersome• ex of good tree:

3

51

0 4 8

INTRO (cont.)

since items well-distributed, search time is O(log n), with n being number of nodes in tree

ex of not-so-good tree:

since items are all right-children of previous node, search time is O(n) in worst case

there exists a method of developing binary-trees that maintain the O(log n) retrieval time by making sure that the nodes of the tree are balanced (height is maintained)

– these are called AVL Trees (or height-balanced)

0

2

8

4

AVL TREES (dictionary)

- AVL Tree:- binary tree where each node has a balance factor of 0, 1 or -1

- Balance Factor of node x: - (height of lft subtree of x) - (height of rt subtree of x)

- Constructor of AVL Tree: - same as before (set root to 0)

- DeleteComplete Tree: - same as before (remove all nodes)

- Search AVL Tree: - same as before

- Insert Function: - now must be change to ensure the tree remains height-balanced after item is

inserted- Delete Function:

- must be designed to ensure the tree is balanced after item is removed

AVL TREES (cont.)

- Example of a Balanced AVL Tree:- Balance factor shown as node

0

00

+1

0

+1 0

-1

0

+1 0

+1 -1

0 0

AVL TREES (cont.)

• Example of an unbalanced AVL-Tree:

+2

0

+1

-2

-20

-1

0

IMPLEMENTATION OF AVL SEARCH TREE

• will use same structure as binary tree:

typedef int Item;

struct TreeNode {

Item Value; //data

TreeNode *left; //pointer to left child

TreeNode *right; //pointer to right child

};

AVL TREE IMPLEMENTATION (CONT.)

class AVLTree { public:

AVLTree(); ~AVLTree(); void Display(); void Insert(const Item &anItem); bool Search(const Item &anItem); bool Delete(const Item &anItem);

private: TreeNode *root; int balance_factor;void InsertTree(TreeNode &root, const Item &anItem); void DisplayTree(TreeNode root); bool SearchTree(TreeNode root, const Item &anItem); void DeleteCompleteTree(); bool DeleteTreeValue(TreeNode &root, const Item &anItem);

};

AVL MEMBER FUNCTIONS

- Constructor- Same as before, but also sets balance factor; sets root

to null, indicating the tree is empty AVLTree::AVLTree() {

root = 0; balance_factor = 0;

}

- Search, Display and Delete:- All same as before

AVL Member Functions (cont.)

- Insertion- must now perform rotations on the tree to ensure that the

balance factor remains valid- types of rotations:

- right rotation:- used to insert a new item into left subtree of left child of parent with

balance factor +2 after the add- steps:

1) reset link from parent of A to B 2) set left link of A = right link of B 3) set right link of B = A

- Example:- Suppose the tree contains:

10 (+1)

4 (0)

AVL TREES – INSERT (cont.)

- If we insert a node containing the value 1:

- Tree now unbalanced- Applying right-rotation:

4 (+1)

10 (+2)

1 (0)

4 (0)

1 (0)

10 (0)

AVL TREES-INSERT (cont.)• Left-Rotation:

• used when inserting new item into right subtree of right child B making parent of B have balance factor –2

• Steps: 1) reset link of parent of A to B 2) set the right link of A = left link of B 3) set left link of B = A

• Example: • Suppose the tree contains:

• And we add: 2 3

4 (0)

1 (0)

10 (0)

AVL TREES – INSERT (cont.)

• Tree now unbalanced, so do left-rotation:

• Balanced tree

4 (+2)

1(-2)

10 (0)

2 (-1)

3 (0)

4 (+1)

2(0)

10 (0)

3 (0)

1 (0)

AVL TREES – INSERT (cont.)

- Left-Right Rotation:- left rotation followed by a right rotation- done when adding item into right subtree of left child B of

parent A produces +2- Steps:

1) left link of A = root C of right subtree of B 2) right link of B = left link of C 3) left link of C = B Then:4) set link from parent of A to point to C5) left link of A = right link of C6) right link of C = A

AVL TREES – INSERT (cont.)

- Example: consider:

- We add 6, tree is unbalanced

10 (+1)

3(0)

10 (+2)

3 (-1)

6(0)

AVL TREES – INSERT (cont.)

- so must first do left-rotation:

- Then, right rotation:

- Tree now balanced

10 (+2)

6(+1)

3(0)

6 (0)

3(0)

10 (0)

AVL TREES – INSERT (cont.)

- left-right rotation may also occur when: a) B has no right child before new node inserted as right

child of B (seen above) b) B has a right child C, and new node inserted in left

subtree of C c) B has a right child C, and new node inserted into right subtree of C

- example of b) - Initial tree:

10 (+1)

3 (0)

12 (0)

1 (0)

7 (0)

AVL TREES – INSERT (cont.)

- we add 5:

10 (+2)

3 (-1)

12 (0)

1 (0)

7 (+1)

5 (0)

AVL TREES – INSERT (cont.)

- Tree now unbalanced, so do left-rotation:

10 (+2)

7 (+2)

12 (0)

3 (0)

5 (0)

1 (0)

AVL TREES – INSERT (cont.)

- Then a right-rotation (of entire tree):

- Tree is now balanced- Right-left rotation is basically the same as above, except right

rotation occurs before left

7 (0)

3 (0)

10 (-1)

1 (0)

5 (0)

12 (0)

AVL-TREES (SUMMARY)

- using AVL tree keeps the search time at O(log n) even as tree grows

- may be costly if # of insertions is much larger than # of searches, due to overhead in rebalancing trees

- studies have shown that: 1) on average, rebalancing required for 45% of insertions 2) half of those are double rotations

QUESTIONS?

• Next Section: Multiway Trees (B-Trees)

P. 855 - 873 (pay attention to 2-3-4 trees)