Docsity

AVL Trees: Balanced Binary Search Trees with Logarithmic Search Time, Slides of Data Structures and Algorithms

Partial preview of the text

Download AVL Trees: Balanced Binary Search Trees with Logarithmic Search Time and more Slides Data Structures and Algorithms in PDF only on Docsity!

AVL TREE

Introduction

• An AVL tree is another balanced binary search

tree.

• Named after their inventors, A delson- V elskii

and L andis,

• 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( log n)

search time.

Balance requirement for an AVL tree: the

left and right sub-trees differ by at most 1

in height

r i n h e i g h t b y a t m o s t o n

Docsity.com

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

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.

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

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

Unbalanced Tree After 1 Shift Right

Jill

Eric

John

Hank

Carl

Mary

Ann Fred

Gary

Kim Derek

  • 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

Ann Hank

Fred

Kim Derek

  • 1 Shift Right On Root Left SubTree

Jill

Eric

John

Gary

Carl

Mary

Ann Hank

Fred

Kim

Derek

 Now we have an AVL Tree