Docsity

Height-Balanced Trees - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Height-Balanced Trees - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Outline

  • Introduction
  • Definitions
  • Height-Balanced Trees
  • Picking Out AVL Trees
  • Why AVL Trees?
  • Added Complexity
  • Violations
  • The Balance Factor
  • What is a rotation?
  • Single Rotations
  • Double Rotations

Introduction

• AVL tree is the first balanced binary search

tree (name after its discovers, Adelson-Velskii

and Landis).

• The AVL tree is a balanced search tree that has

an additional balance condition.

• The balance condition must be easy to

maintain and ensures that the depth of the

tree is O(logN)

Height-Balanced Trees

  • Height of a tree is the length of the longest path from a root to a leaf.
  • A binary search tree T is a height-balanced k-tree or HB[k]- tree, if each node in the tree has the HB[k] property.
  • A node has the HB[k] property if the height of the left and right sub-trees of the node differ in height by at most k.
  • A HB[1] tree is called an AVL tree.

Picking Out AVL Trees

  • Notation: NODE = Left-Ht, Right-Ht

Yes! 5 = 2, 1 4 = 1, 0 3 = 0, 0 7 = 0, 0

No! 10 = 3, 1

Why AVL Trees?

  • The worst case for Binary Search Trees:

Insert: O(n).

Delete: O(n).

Search :O(n).

The worst case for AVL trees:

Insert: O(log n).

Delete: O(log n).

Search:O(log n).

Recall when comparing the time complexity

O(log n) < O(n)

Why AVL Trees? (Cont’d)

• AVL trees allow for logarithmic Inserts and

Deletes, and they allow for easy traversals,

such as the “In-order” traversal if we want to

sort our elements.

Violations

No!

No! 5 = 3, 4 = 2,

Notation: NODE = Left-Ht, Right-Ht

The Balance Factor

• In order to detect when a “violation” of a AVL

tree occurs, we need to have each node keep

track of the difference in height between its

right and left sub-trees.

• Notation for the balance factor:

bf (node) = Left-Ht – Right- Ht

To be a valid AVL Tree, -1 <= bf(x) <= 1 for all

nodes.

The Balance Factor (Cont’d)

Notation for the balance factor:

bf (node) = Left-Ht – Right- Ht

bf(8) = 2 – 0 = 2

bf(4) = 1 – 0 = 1

Bf(3) = 0 – 0 = 0

Invalid Tree!!

What is a rotation?

• A rotation is an operation to rearrange nodes

to maintain balance of an AVL tree after

adding and removing a node

• There are two case to perform the rotation

1) Single Rotation

2) Double Rotation

When to perform rotations?

we need to perform a rotation anytime a node’s

balance factor is 2 or -

Single Rotations

After inserting (a) 60; (b) 50; (c) 20 into an

initially empty binary search tree, the tree is

not balanced; d) a corresponding AVL tree

rotates its nodes to restore balance.

Single Rotations (Cont’d)

  • Algorithm for left rotation

Algorithm rotateLeft(nodeN) nodeC = right child of nodeN Set nodeN ’s right child to nodeC ’s left child Set nodeC ’s left child to nodeN

Double Rotations

Before and after an addition to an AVL

sub-tree that requires both a right

rotation and a left rotation to maintain

its balance.

Double Rotations (Cont’d)

Before and after an addition to an AVL

sub-tree that requires both a left and

right rotation to maintain its balance.