CS221

46
CS221 Week 8 - Friday

description

Week 8 - Friday. CS221. Last time. What did we talk about last time? BST deletion AVL trees. Questions?. Project 3. Assignment 4. Balancing a Tree by Construction. How to make a balanced tree. What if we knew ahead of time which numbers we were going to put into a tree? - PowerPoint PPT Presentation

Transcript of CS221

Page 1: CS221

CS221Week 8 - Friday

Page 2: CS221

Last time

What did we talk about last time? Practiced with 2-3 trees Red-black trees

Page 3: CS221

Questions?

Page 4: CS221

Project 3

Page 5: CS221

Assignment 4

Page 6: CS221

Red-black Trees

Page 7: CS221

Building the tree

We can do an insertion with a red-black tree using a series of rotations and recolors

We do a regular BST insert Then, we work back up the tree as the recursion unwinds

If the right child is red and the left is black, we rotate the current node left

If the left child is red and the left child of the left child is red, we rotate the current node right

If both children are red, we recolor them black and the current node red

You have to do all these checks, in order! Multiple rotations can happen

It doesn't make sense to have a red root, so we always color the root black after the insert

Page 8: CS221

Left rotation

We perform a left rotation when the right

child is red

Y

X

B

A

C

Current

Y

X

BA

C

Current

Page 9: CS221

Right rotation

We perform a right rotation when the left child is red and

its left child is red

Z

Y

BA

D

Current

X C

Z

Y

BA D

Current

X

C

Page 10: CS221

Recolor

We recolor both children and the current node when both children

are red

Y

X

BA D

Current

Z

C

Y

X

BA D

Current

Z

C

Page 11: CS221

Red-black tree practice

Add the following keys to a red-black tree: 62 11 32 7 45 24 88 25 28 90

Page 12: CS221

Analysis of red-black trees

The height of a red-black tree is no more than 2 log n

Find is Θ(height), so find is Θ(log n) Since we only have to go down that

path and back up to insert, insert is Θ(log n)

Delete in red-black trees is messy, but it is also actually Θ(log n)

Page 13: CS221

AVL Trees

Page 14: CS221

AVL trees

Another approach to balancing is the AVL tree Named for its inventors G.M. Adelson-Velskii and

E.M. Landis Invented in 1962

An AVL tree is better balanced than a red-black tree, but it takes more work to keep such a good balance It's faster for finds (because of the better balance) It's slower for inserts and deletes Like a red-black tree, all operations are Θ(log n)

Page 15: CS221

AVL definition

An AVL tree is a binary search tree where

The left and right subtrees of the root have heights that differ by at most one

The left and right subtrees are also AVL trees

Page 16: CS221

AVL tree?

10

6 14

1 9 17

72 15

Page 17: CS221

Balance factor

The balance factor of a tree (or subtree) is the height of its right subtree minus the height of its left

In an AVL tree, the balance factor of every subtree is -1, 0, or +1

What’s the balance factor of this tree?

6

1 9

2

Page 18: CS221

How do we build an AVL tree?

Carefully. Every time we do an add, we have to

make sure that the tree is still balanced

There are 4 cases1. Left Left2. Right Right3. Left Right4. Right Left

Page 19: CS221

Left Left

3

2

1

BA

C

D

3

2

1

BA C D

Right Rotation

Page 20: CS221

Right Right

2

1

B

A

C D

3

3

2

1

BA C D

Left Rotation

Page 21: CS221

Left Right

3

2

1

B

A

C D

3

2

1

B AC D

Left Rotation + Right Rotation

Page 22: CS221

Right Left

2

1

B

A

CD

3 3

2

1

BA CD

Right Rotation + Left

Rotation

Page 23: CS221

Let’s make an AVL tree!

Let’s just add these numbers in order:1, 2, 3, 4, 5, 6, 7, 8, 9, 10

What about these?7, 24, 92, 32, 2, 57, 67, 84, 66, 75

Page 24: CS221

Balancing a Tree by Construction

Page 25: CS221

How to make a balanced tree

What if we knew ahead of time which numbers we were going to put into a tree?

How could we make sure the tree is balanced?

Answer: Sort the numbers Recursively add the numbers such that

the tree stays balanced

Page 26: CS221

Balanced insertion

Write a recursive method that adds a sorted array of data such that the tree stays balanced

Assume that an add(int key) method exists Use the usual convention that start is the

beginning of a range and end is the location after the last legal element in the range

public void balance(int[] data, int start, int end )

Page 27: CS221

DSW algorithm

Takes a potentially unbalanced tree and turns it into a balanced tree

Step 1 Turn the tree into a degenerate tree (backbone or

vine) by right rotating any nodes with left children Step 2

Turn the degenerate tree into a balanced tree by doing sequences of left rotations starting at the root

The analysis is not obvious, but it takes O(n) total rotations

Page 28: CS221

Which method is best?

How much time does it take to insert n items with: Red-black or AVL tree Balanced insertion method Unbalanced insertion + DSW rebalance

How much space does it take to insert n items with: Red-black or AVL tree Balanced insertion method Unbalanced insertion + DSW rebalance

Page 29: CS221

Self-Adjusting Trees

Page 30: CS221

Self-Adjusting Trees

Balanced binary trees ensure that accessing any element takes O(log n) time

But, maybe only a few elements are accessed repeatedly

In this case, it may make more sense to restructure the tree so that these elements are closer to the root

Page 31: CS221

Restructuring strategies

1. Single rotation Rotate a child about its parent if an

element in a child is accessed, unless it's the root

2. Moving to the root Repeat the child-parent rotation until the

element being accessed is the root

Page 32: CS221

Splaying

A concept called splaying takes the rotate to the root idea further, doing special rotations depending on the relationship of the child R with its parent Q and grandparent P

Cases:1. Node R's parent is the root

Do a single rotation to make R the root2. Node R is the left child of Q and Q is the left child of P

(respectively right and right) Rotate Q around P Rotate R around Q

3. Node R is the left child of Q and Q is the right child of P (respectively right and left)

Rotate R around Q Rotate R around P

Page 33: CS221

Danger, Will Robinson!

We are not going deeply into self-organizing trees

It's an interesting concept, and rigorous mathematical analysis shows that splay trees should perform well, in terms of Big O bounds

In practice, however, they usually do not An AVL tree or a red-black tree is generally the

better choice If you come upon some special problem where

you repeatedly access a particular element most of the time, only then should you consider splay trees

Page 34: CS221

Hash TablesStudent Lecture

Page 35: CS221

Hash Tables

Page 36: CS221

Recall: Symbol table ADT We can define a symbol table ADT with a few

essential operations: put(Key key, Value value)▪ Put the key-value pair into the table

get(Key key):▪ Retrieve the value associated with key

delete(Key key)▪ Remove the value associated with key

contains(Key key)▪ See if the table contains a key

isEmpty() size()

It's also useful to be able to iterate over all keys

Page 37: CS221

Unordered symbol table

We have been talking a lot about trees and other ways to keep ordered symbol tables

Ordered symbol tables are great, but we may not always need that ordering

Keeping an unordered symbol table might allow us to improve our running time

Page 38: CS221

Hash tables: motivation

Balanced binary search trees give us: O(log n) time to find a key O(log n) time to do insertions and

deletions

Can we do better? What about:

O(1) time to find a key O(1) to do an insertion or a deletion

Page 39: CS221

Hash tables: theory

We make a huge array, so big that we’ll have more spaces in the array than we expect data values

We use a hashing function that maps keys to indexes in the array

Using the hashing function, we know where to put each key but also where to look for a particular key

Page 40: CS221

Hash table: example

Let’s make a hash table to store integer keys

Our hash table will be 13 elements long

Our hashing function will be simply modding each value by 13

Page 41: CS221

Hash table: example

Insert these keys: 3, 19, 7, 104, 89

0 1 2 3 4 5 6 7 8 9 10 11 12

104 3 19 7 89

0 1 2 3 4 5 6 7 8 9 10 11 12

Page 42: CS221

Hash table: example

Find these keys:

19 YES!

88 NO!

16 NO!

104 3 19 7 89

0 1 2 3 4 5 6 7 8 9 10 11 12

Page 43: CS221

Hash table: issues

We are using a hash table for a space/time tradeoff

Lots of space means we can get down to O(1)

How much space do we need? How do we pick a good hashing

function? What happens if two values collide

(map to the same location)

Page 44: CS221

Upcoming

Page 45: CS221

Next time…

Hashing functions

Page 46: CS221

Reminders

Keep working on Project 3 Sign up for your teams by today!

Finish Assignment 4 Due tonight by midnight