Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 INTRODUCTION TO AVL TREES P. 839 – 854

2 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 48

3 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

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

5 AVL TREES (cont.) - Example of a Balanced AVL Tree: -Balance factor shown as node 0 00 +1 0 0 0 +10 00

6 AVL TREES (cont.) Example of an unbalanced AVL-Tree: +2 0 +1 -2 0 0

7 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 };

8 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); };

9 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

10 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)

11 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)

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

13 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)

14 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 C 5)left link of A = right link of C 6)right link of C = A

15 AVL TREES – INSERT (cont.) - Example: consider: - We add 6, tree is unbalanced 10 (+1 ) 3 (0) 10 (+2 ) 3 (- 1) 6 (0)

16 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)

17 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)

18 AVL TREES – INSERT (cont.) - we add 5: 10 (+2 ) 3 (- 1) 12 (0) 1 (0) 7 (+1 ) 5 (0)

19 AVL TREES – INSERT (cont.) - Tree now unbalanced, so do left-rotation: 10 (+2 ) 7 (+2 ) 12 (0) 3 (0) 5 (0) 1 (0)

20 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)

21 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

22 QUESTIONS? Next Section: Multiway Trees (B-Trees) P. 855 - 873 (pay attention to 2-3-4 trees)


Download ppt "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."

Similar presentations


Ads by Google