Download presentation

Presentation is loading. Please wait.

Published byElvis Berry Modified over 2 years ago

1
AVL TREE Name :TIN HO

2
Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after their inventors, Adelson-Velskii and Landis, Named after their inventors, Adelson-Velskii and Landis, They were the first dynamically balanced trees to be proposed. They were the first dynamically balanced trees to be proposed. Like red-black trees, they are not perfectly balanced, but pairs of sub-trees differ in height by at most 1, maintaining an O(logn) search time. Like red-black trees, they are not perfectly balanced, but pairs of sub-trees differ in height by at most 1, maintaining an O(logn) search time.

3
Definition of an AVL tree ► Addition and deletion operations also take O(logn) time. ► An AVL tree is a binary search tree which has the following properties: ► The sub-trees of every node differ in height by at most one. ► Every sub-tree is an AVL tree.

4
Balance requirement for an AVL tree: the left and right sub-trees differ by at most 1 in height 1.T h e s u b - t r e e s o f e v e r y n o d e d i f f e r i n h e i g h t b y a t m o s t o n e. 2.E v e r y s u b - t r e e i s a n A V L t r e e.

5
Be careful with this definition: it permits some apparently unbalanced trees! For example, here are some trees: Tree AVL tree? Yes Examination shows that each left sub-tree has a height 1 greater than each right sub-tree.

6
► AVL tree? ► No ► Sub-tree with root 8 has height 4 and sub-tree with root 18 has height 2

7
Key terms AVL trees Trees which remain balanced - and thus guarantee O(logn) search times - in a dynamic environment. Or more importantly, since any tree can be re-balanced - but at considerable cost - can be re-balanced in O(logn) time. Trees which remain balanced - and thus guarantee O(logn) search times - in a dynamic environment. Or more importantly, since any tree can be re-balanced - but at considerable cost - can be re-balanced in O(logn) time.

8
AVL TREE AVL Tree An AVL tree is a tree which is balanced We earlier defined a balanced tree as a tree whose height is [log (n+1)] Another definition is “A tree is balanced if the number of nodes in every left sub-tree differs by at most 1 from the number of nodes in the corresponding right sub-tree. This is known as count-balanced One method of count-balancing a tree is to repeatedly move the root into the sub-tree with the smaller amount of nodes Each move is called a shift

9
Gary Eric John Jill Carl Mary Fred Ann Hank Kim Derek Unbalanced Tree

10
This tree has 7 nodes to the left of the root and only 3 nodes to the right of the root To count-balance we will right shift Jill into the right sub-tree Jill’s in-order predecessor, Hank will now become the new root

11
Unbalanced Tree After 1 Shift Right Jill Eric John Hank Carl Mary Fred Ann Gary Kim Derek

12
Unbalanced Tree After 1 Shift Right The tree is still unbalanced with 6 nodes to the left of root and 4 nodes to the left The tree is still unbalanced with 6 nodes to the left of root and 4 nodes to the left We perform a shift right one more time moving Hank into the right tree and making his in-order predecessor (Gary) the new root We perform a shift right one more time moving Hank into the right tree and making his in-order predecessor (Gary) the new root

13
Unbalanced Tree After 2 Shifts Right n The tree is now balanced 5-5. However the Root->left subtree is not balanced i.e 3-1 so to correct that we will right shift Eric Jill Eric John Gary Carl Mary HankAnn Fred Kim Derek

14
1 Shift Right On Root Left SubTree Jill Eric John Gary Carl Mary HankAnn Fred Kim Derek n Now we have an AVL Tree

15
Count Balance Algorithm // return tree whose root is n, as a count balanced (AVL) tree public BinNode countbalance(BinNode n) { if (not n empty) then leftnum = number of nodes in left child rightnum = number of nodes in right child if (leftnum > rightnum) then loop for i going from1 to (leftnum-rightnum)/2 do right-shift root else loop for i going from1 to (rightnum-leftnum)/2 do left-shift root endif n left child = countbalance(n left child) n right child = countbalance(n right child) return n }

Similar presentations

OK

Balanced Search Trees CS 3110 Fall 2010. Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.

Balanced Search Trees CS 3110 Fall 2010. Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on indian textile industries in uganda Ppt on job interview skills Ppt on wireless networks Ppt on computer ethics crime and security Ppt on higher education system in india Ppt on varactor diode characteristics Download ppt on evaporation Ppt on all windows operating system File converter pdf to ppt online Ppt on media revolutionary