AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree...

15
AVL TREE AVL TREE Name :TIN HO Name :TIN HO

Transcript of AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree...

Page 1: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

AVL TREEAVL TREE

Name :TIN HOName :TIN HO

Page 2: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

IntroductionIntroduction An An AVL treeAVL tree is another balanced binary search is another balanced binary search

tree. tree. Named after their inventors, Named after their inventors, AAdelson-delson-VVelskii elskii

and and LLandis,andis, They were the first dynamically balanced trees They were the first dynamically balanced trees

to be proposed. to be proposed. Like red-black trees, they are not perfectly Like red-black trees, they are not perfectly

balanced, but pairs of sub-trees differ in height balanced, but pairs of sub-trees differ in height by at most 1, maintaining an by at most 1, maintaining an O(O(loglogn)n) search search time.time.

Page 3: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

Definition of an AVL treeDefinition of an AVL tree

►Addition and deletion operations also Addition and deletion operations also take take O(O(loglogn)n) time. time.

►An AVL tree is a binary search tree An AVL tree is a binary search tree which has the following properties: which has the following properties:

►The sub-trees of every node differ in The sub-trees of every node differ in height by at most one. height by at most one.

►Every sub-tree is an AVL treeEvery sub-tree is an AVL tree. .

Page 4: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

Balance requirement for an AVL Balance requirement for an AVL tree: the left and right sub-trees tree: the left and right sub-trees

differ by at most 1 in heightdiffer by at most 1 in height  

1. The sub-trees of every node differ in height by at most one.

2. Every sub-tree is an AVL tree.

                            

            

Page 5: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

• 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.

Page 6: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

► AVL tree?AVL tree?

► NoNo► Sub-tree with root 8 has height 4 and sub-tree with root 18 has Sub-tree with root 8 has height 4 and sub-tree with root 18 has

height 2height 2

Page 7: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

Key termsKey terms

AVL treesAVL trees

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

Page 8: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

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

Page 9: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

Gary

Eric John

Jill

Carl Mary

Fred

Ann

Hank

KimDerek

Unbalanced Tree

Page 10: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

Unbalanced Tree

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

Page 11: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

Unbalanced Tree After 1 Shift Unbalanced Tree After 1 Shift RightRight

Jill

Eric John

Hank

Carl Mary

FredAnn

Gary

KimDerek

Page 12: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

Unbalanced Tree After 1 Shift Unbalanced Tree After 1 Shift RightRight

• The tree is still unbalanced with 6 The tree is still unbalanced with 6 nodes to the left of root and 4 nodes nodes to the left of root and 4 nodes to the leftto the left

• We perform a shift right one more We perform a shift right one more time moving Hank into the right tree time moving Hank into the right tree and making his in-order predecessor and making his in-order predecessor (Gary) the new root(Gary) the new root

Page 13: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

• Unbalanced Tree After 2 Shifts Right

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

KimDerek

Page 14: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

• 1 Shift Right On Root Left SubTree

Jill

Eric

John

Gary

Carl Mary

HankAnn

Fred

Kim

Derek

Now we have an AVL Tree

Page 15: AVL TREE Name :TIN HO. Introduction An AVL tree is another balanced binary search tree. An AVL tree is another balanced binary search tree. Named after.

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