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

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

Embed Size (px)

Citation preview

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

Chapter 19: Binary Search Trees

or How I Learned to Love AVL Trees

andBalance The Tree

Group 6: Tim Munn

Page 2: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance 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)

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

Basics continued…

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

Focus Will Be: AVL Trees

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

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

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

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()

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

A Search Tree

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

Not A Search Tree

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

Problems

Insertions and Deletions Can Lead To Unbalanced Tree

If Input Is Sorted…

If Input Contains Long Sequence of Nonrandomness

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

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

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

Insertion Sequence: 3 2 1 9 7 5

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

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!

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

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

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

Deletion Example: Single Child

Remove 5

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

Deletion Example: Single Child with a Subtree

Remove 5

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

Deletion Example: 2 Children

Remove 2

Remove 2

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

Balanced Tree

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

An Unbalanced Tree

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

AVL Trees

Balanced Search Tree Adelson-Velskii and Landis for those

historically inclined

Satisfies O(log N)

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

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

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

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.

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

AVL Tree…Not

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

AVL Tree…Not – The Problem

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

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

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

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

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

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

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

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

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

The Single Rotation

Switches Role of Parent and Childwhile maintaining search order

Before Inserting 1

During

After

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

Double Rotation

Start at page 627

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

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

}