Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim...

Preview:

Citation preview

Chapter 19: Binary Search Trees

or How I Learned to Love AVL Trees

andBalance The Tree

Group 6: Tim Munn

Basics

What Is It? ‘A simple data structure that extends

the binary search algorithm to allow for insertions and deletions’

Average time for operations is O(log N) but a worst case scenario of O(N)

Basics continued…

How Do We Avoid Worst Case? AVL Trees Red-Black Trees AA-Trees

Focus Will Be: AVL Trees

Some Definitions

Order Property For every node x, values in left

subtree are less than x and values in right subtree are greater than x

Duplicate nodes or key values can be handled by a counter

Basic Operations or Methods find()

Start at root and go left or right based on comparison

findMin() Start at root and keep going left

findMax() Start at root and keep going right

insert() remove()

A Search Tree

Not A Search Tree

Problems

Insertions and Deletions Can Lead To Unbalanced Tree

If Input Is Sorted…

If Input Contains Long Sequence of Nonrandomness

Insertion Sequence: 7,2,9,5,3,1

Insertion Sequence: 3 2 1 9 7 5

Deleting A Node

Most complicated operation If node is a leaf = delete If node has one child, connect child

to node to be deleted’s parent, then delete

If node has two children = most complicated!

Deleting a Node with 2 Children

Idea: Replace node to be deleted with the smallest node in the right subtree then remove the node Finding smallest node is easy -> keep

going left in right subtree

Deletion Example: Single Child

Remove 5

Deletion Example: Single Child with a Subtree

Remove 5

Deletion Example: 2 Children

Remove 2

Remove 2

Balanced Tree

An Unbalanced Tree

AVL Trees

Balanced Search Tree Adelson-Velskii and Landis for those

historically inclined

Satisfies O(log N)

Basic Idea

Require left and right tree are same height

Using recursion, this implies that all nodes must adhere to this as each node is a root of a subtree

Insertion become difficult due to balancing

AVL Definition

A Binary Search Tree with balance property that for any node, the left and right subtree heights differ by no more than 1.

AVL Tree…Not

AVL Tree…Not – The Problem

Balancing Approaches ‘Random’ insertions and deletions will

eventually lead to an unbalanced tree To simplify task, information regarding

the height of a node's subtrees is stored in the node structure.

If we know the relative height, we can arrange to simply store a value that indicates The subtree heights are the same Left subtree is higher Right subtree is higher

It All Boils Down To...

Only nodes on along the insertion to the root may have their balances alterered...because...only their subtrees are being changed

A height imbalance occurs if for a node X abs(height(left subtree of X) –

height(right subtree of X)) = 2

The 4 Cases for Rotations

1. Insertion in left subtree of left child of X

2. Insertion in right subtree of left child of X

3. Insertion in left subtree of right child of X

4. Insertion in right subtree of right child of X

Become 2 Cases for Rotations

1 & 4 are symmetrically the same -> left–left & right–right Handled by Single Rotation

2 & 3 are symmetrically the same -> right–left & left-right Handled by more complicated Double

Rotation

The Single Rotation

Switches Role of Parent and Childwhile maintaining search order

Before Inserting 1

During

After

Double Rotation

Start at page 627

A ‘Simple’ Project????public class AVLNode extends BinaryNode {

public static final int LEFT_HEAVY  =  -1;public static final int RIGHT_HEAVY =  +1;public static final int EQUAL =   0;

private int balance;

AVLNode(Comparable theElement){

super(theElement);this.balance = EQUAL;

}public int getBalance({ return balance;}public void setBalance(int balance){ this.balance = balance;}

}

Recommended