Top Banner
Quiz3! Midterm! Assignment 2! (most) Quiz4! Today’s special: 4 for 1
21

Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Dec 17, 2015

Download

Documents

Ashley Richard
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Quiz3!

Midterm!

Assignment2!

(most) Quiz4!

Today’s special: 4 for 1

Page 2: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Balanced trees:

Red-Black Trees

Page 3: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Definitions Insertion In a nutshell

Page 4: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Definition: an extended Node

Balanced trees: Red-black trees

Before, all our nodes were born equals.

In a red-black tree, a node is either red or black.

For some algorithms, it is easier to have a pointer to the parent.

right

left parent

parent

public class RBNode extends Node{ private Node parent; private boolean color; // true for black, false for red public RBNode(int key, Object data) { super(key, data); parent = null; color = true; } }

Page 5: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Definition: Red-Black Tree

Balanced trees: Red-black trees

• Each node must have exactly two children. For each child that is lacking, you create a fake black one.

10

5 13

713

96

← needs two fake children

← needs two fake children

← needs one fake childneeds two fake children →

Page 6: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Definition: Red-Black Tree

Balanced trees: Red-black trees

• Each node must have exactly two children. For each child that is lacking, you create a fake black one.

10

5 13

713

96

Think of those as gostly nodes: they

are not really there…

In practice, don’t bother

drawing them.

Page 7: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Definition: Red-Black Tree

Balanced trees: Red-black trees

• Each node must have exactly two children. For each child that is lacking, you create a fake black one.

• The root is black.

• Every path from a node to a leaf contains the same number of black nodes.

• If a node is red then both its children must be black.

Page 8: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Example

Balanced trees: Red-black trees

The root is black.

The children of red nodes are both black.

Page 9: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Example

Balanced trees: Red-black trees

The root is black.

The children of red nodes are both black.

Page 10: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

The children of a red node must be black.

Algorithm: Insertion

Balanced trees: Red-black trees

A red-black tree is a particular binary search tree, so create a new node as red and insert it.

What property may be violated?

(similarly to an AVL: you insert the node, that may screw up a few properties, so you try to fix them after)

579

Violation!

7

Page 11: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Balanced trees: Red-black trees

Algorithm: Insertion

There are different situations, and fixing them follows a very scientific and rigorous process.

Page 12: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Algorithm: Insertion

Balanced trees: Red-black trees

We have detected a need for balance when z is red and his parent too.

• If z has a red uncle: colour the parent and uncle black, and grandparent red.

z

Page 13: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Algorithm: Insertion

Balanced trees: Red-black trees

We have detected a need for balance when z is red and his parent too.

• If z has a red uncle: colour the parent and uncle black, and grandparent red.

• If z is a left child and has a black uncle: colour the parent black and the grandparent red, then rotateRight(z.parent.parent)

Page 14: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Algorithm: Insertion

Balanced trees: Red-black trees

We have detected a need for balance when z is red and his parent too.

• If z has a red uncle: colour the parent and uncle black, and grandparent red.

• If z is a left child and has a black uncle: colour the parent black and the grandparent red, then rotateRight(z.parent.parent)• If z is a right child and has a black uncle, then rotateLeft(z.parent) and

Page 15: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Algorithm: Insertion

Balanced trees: Red-black trees

4

7

12Double red violation!

It also shows it’s unbalanced…

Let’s insert 4, 7, 12, 15, 3 and 5.

Page 16: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Algorithm: Insertion

Balanced trees: Red-black trees

7

4 12

Let’s insert 4, 7, 12, 15, 3 and 5.

15

Double red violation.

We can’t have a better balance, and

there is a red uncle…

3

What should we do?

Nothing, no double red. 5

Page 17: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Algorithm: Insertion

Balanced trees: Red-black trees

To practice more: http://gauss.ececs.uc.edu/RedBlack/redblack.html

7

4

9

3

5

6

2

1

Page 18: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

In a nutshell:

What to optimize?

Page 19: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Which operation matters?

Balanced trees: Red-black trees

If we want to optimize the access, which primitive would you choose?

We have seen how to use three primitive structures: arrays, simple pointers (as in a LinkedList) and trees.

An array, because access is in O(1).

If we want to optimize the insertion, which primitive would you choose?

Pointers with a shortcut to the tail.

Inserting at the end will be O(1).

What about we want to optimize insertion and access?

Page 20: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Balanced trees: Red-black trees

Which operation matters?

What about we want to optimize insertion and access?

You can’t have O(1) for both.

But with a balanced tree you get O(log n).

If you want to optimize one operation, go for arrays or simple

pointers. Beyond, use a tree.

Page 21: Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.

Balanced trees: Red-black trees

A bit of practice1a) Write a method findAllElements that returns the content of a binary search tree as a LinkedList L.

1b) Let say that we delete the tree and we add all the elements from L to the tree again, from first to last. How can you ensure that we will get the same tree back?

2b) Write showPathReverse(int key1, int key2) that will show the keys in the other order (from key2 to key1).

2a) Write a method showPath(int key1, int key2) that will show the keys on the path from key1 to key2. Assume both keys exist.

2c) Write showPathReverse(int key1, int key2) without recursive calls.