- 783 Views
- Updated On :
- Presentation posted in: Sports / GamesEducation / CareerFashion / BeautyGraphics / DesignNews / Politics

Search Trees Red-Black and Other Dynamically BalancedTrees. ECE573: Data Structures and Algorithms Electrical and Computer Engineering Dept. Rutgers University http://www.cs.rutgers.edu/~vchinni/dsa/. Searching - Re-visited. Binary tree O( log n) if it stays balanced

Related searches for Search Trees Red-Black and Other Dynamically BalancedTrees

Search Trees Red-Black and Other Dynamically BalancedTrees

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Search TreesRed-Black and Other Dynamically BalancedTrees

ECE573: Data Structures and Algorithms

Electrical and Computer Engineering Dept.

Rutgers University

http://www.cs.rutgers.edu/~vchinni/dsa/

- Binary tree O(log n)if it stays balanced
- Simple binary tree good for static collections
- Low (preferably zero) frequency of insertions/deletions
but my collection keeps changing!

- It’s dynamic
- Need to keep the tree balanced

- First, examine some basic tree operations
- Useful in several ways!

ECE 573: Data Structures & Algorithms

- Traversal = visiting every node of a tree
- Three basic alternatives
- Pre-order
- Root
- Left sub-tree
- Right sub-tree

¬

®

¯

°

x A + x + B C x D E F

L

R

L

L

R

ECE 573: Data Structures & Algorithms

11

- Traversal = visiting every node of a tree
- Three basic alternatives
- In-order
- Left sub-tree
- Root
- Right sub-tree

µ

¬

±

³

¯

®

²

´

A x B + C x D x E + F

°

L

L

R

ECE 573: Data Structures & Algorithms

11

- Traversal = visiting every node of a tree
- Three basic alternatives
- Post-order
- Left sub-tree
- Right sub-tree
- Root

µ

¬

³

´

¯

²

A B C + D E x x F + x

±

°

®

L

L

R

ECE 573: Data Structures & Algorithms

11

- Post-order
- Left sub-tree
- Right sub-tree
- Root

- Reverse-Polish
Normal algebraic form

which traversal?

µ

¬

³

´

¯

²

±

°

(A (((BC+)(DEx) x) F +)x )

®

(A x(((B+C)(DxE))+F))

ECE 573: Data Structures & Algorithms

- Binary search tree
- Produces a sorted list by in-order traversal

- In order: A D E G H K L M N O P T V

ECE 573: Data Structures & Algorithms

- Binary search tree
- Preserving the order
- Observe that this transformation preserves thesearch tree

ECE 573: Data Structures & Algorithms

- Binary search tree
- Preserving the order
- Observe that this transformation preserves thesearch tree

- We’ve performed a rotation of the sub-tree about the T and O nodes

ECE 573: Data Structures & Algorithms

- Binary search tree
- Rotations can be either left- or right-rotations
- For both trees: the inorder traversal is
- A x B y C

ECE 573: Data Structures & Algorithms

- Binary search tree
- Rotations can be either left- or right-rotations
- Note that in this rotation, it was necessary to moveB from the right child of x to the left child of y

ECE 573: Data Structures & Algorithms

- A Red-Black Tree
- Binary search tree
- Each node is “coloured” red or black
- An ordinary binary search tree with node colouringsto make a red-black tree

ECE 573: Data Structures & Algorithms

- A Red-Black Tree
- Every node is RED or BLACK
- Every leaf is BLACK

When you examinerb-tree code, you will

see sentinel nodes (black) added as the leaves.

They contain no data.

Sentinel nodes (black)

ECE 573: Data Structures & Algorithms

- A Red-Black Tree
- Every node is RED or BLACK
- Every leaf is BLACK
- If a node is RED, then both children are BLACK

This implies that no path

may have two adjacent

RED nodes.

(But any number of BLACK

nodes may be adjacent.)

ECE 573: Data Structures & Algorithms

- A Red-Black Tree
- Every node is RED or BLACK
- Every leaf is BLACK
- If a node is RED, then both children are BLACK
- Every path from a node to a leaf contains the same number of BLACK nodes

From the root,

there are 3 BLACK nodes

on every path

ECE 573: Data Structures & Algorithms

- A Red-Black Tree
- Every node is RED or BLACK
- Every leaf is BLACK
- If a node is RED, then both children are BLACK
- Every path from a node to a leaf contains the same number of BLACK nodes

The length of this path is the

black height of the tree

ECE 573: Data Structures & Algorithms

- Lemma
A RB-Tree with n nodes has

height £2 log(n+1)

- Proof .. See Cormen

- Essentially,height £2 black height
- Search time
O( log n )

ECE 573: Data Structures & Algorithms

- Data structure
- As we’ll see, nodes in red-black trees need to know their parents,
- so we need this data structure

struct t_red_black_node {

enum { red, black } colour;

void *item;

struct t_red_black_node *left,

*right,

*parent;

}

Same as a

binary tree

with these two

attributes

added

ECE 573: Data Structures & Algorithms

- Insertion of a new node
- Requires a re-balance of the tree

rb_insert( Tree T, node x ) {

/* Insert in the tree in the usual way */

tree_insert( T, x );

/* Now restore the red-black property */

x->colour = red;

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

Insert node

4

Mark itred

Label the current node

x

ECE 573: Data Structures & Algorithms

rb_insert( Tree T, node x ) {

/* Insert in the tree in the usual way */

tree_insert( T, x );

/* Now restore the red-black property */

x->colour = red;

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

While we haven’t reached the root

and x’s parent is red

x->parent

ECE 573: Data Structures & Algorithms

rb_insert( Tree T, node x ) {

/* Insert in the tree in the usual way */

tree_insert( T, x );

/* Now restore the red-black property */

x->colour = red;

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

If x is to the left of it’s granparent

x->parent->parent

x->parent

ECE 573: Data Structures & Algorithms

/* Now restore the red-black property */

x->colour = red;

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

y is x’s right uncle

x->parent->parent

x->parent

right “uncle”

ECE 573: Data Structures & Algorithms

while ((x != T->root)&& (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/*If x's parent is a left, y is x's right'uncle’*/

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

If the uncle is red, change

the colours of y, the grand-parent

and the parent

x->parent->parent

x->parent

right “uncle”

ECE 573: Data Structures & Algorithms

while ((x != T->root)&& (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

ECE 573: Data Structures & Algorithms

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

x’s parent is a left again,

mark x’s uncle

but the uncle is black this time

New x

ECE 573: Data Structures & Algorithms

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

else {

/* y is a black node */

if ( x == x->parent->right ) {

/* and x is to the right */

/* case 2 - move x up and rotate */

x = x->parent;

left_rotate( T, x );

.. but the uncle is black this time

and x is to the right of it’s parent

ECE 573: Data Structures & Algorithms

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

else {

/* y is a black node */

if ( x == x->parent->right ) {

/* and x is to the right */

/* case 2 - move x up and rotate */

x = x->parent;

left_rotate( T, x );

.. So move x up and

rotate about x as root ...

ECE 573: Data Structures & Algorithms

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

else {

/* y is a black node */

if ( x == x->parent->right ) {

/* and x is to the right */

/* case 2 - move x up and rotate */

x = x->parent;

left_rotate( T, x );

ECE 573: Data Structures & Algorithms

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

else {

/* y is a black node */

if ( x == x->parent->right ) {

/* and x is to the right */

/* case 2 - move x up and rotate */

x = x->parent;

left_rotate( T, x );

.. but x’s parent is still red ...

ECE 573: Data Structures & Algorithms

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

else {

/* y is a black node */

if ( x == x->parent->right ) {

/* and x is to the right */

/* case 2 - move x up and rotate */

x = x->parent;

left_rotate( T, x );

.. The uncle is black ..

uncle

.. and x is to the left of its parent

ECE 573: Data Structures & Algorithms

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

else {

/* y is a black node */

if ( x == x->parent->right ) {

/* and x is to the right */

/* case 2 - move x up and rotate */

x = x->parent;

left_rotate( T, x );

else { /* case 3 */

x->parent->colour = black;

x->parent->parent->colour = red;

right_rotate( T, x->parent->parent );

}

.. So we have the final case ..

ECE 573: Data Structures & Algorithms

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

else {

/* y is a black node */

if ( x == x->parent->right ) {

/* and x is to the right */

/* case 2 - move x up and rotate */

x = x->parent;

left_rotate( T, x );

else { /* case 3 */

x->parent->colour = black;

x->parent->parent->colour = red;

right_rotate( T, x->parent->parent );

}

.. Change colours

and rotate ..

ECE 573: Data Structures & Algorithms

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

else {

/* y is a black node */

if ( x == x->parent->right ) {

/* and x is to the right */

/* case 2 - move x up and rotate */

x = x->parent;

left_rotate( T, x );

else { /* case 3 */

x->parent->colour = black;

x->parent->parent->colour = red;

right_rotate( T, x->parent->parent );

}

ECE 573: Data Structures & Algorithms

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

else {

/* y is a black node */

if ( x == x->parent->right ) {

/* and x is to the right */

/* case 2 - move x up and rotate */

x = x->parent;

left_rotate( T, x );

else { /* case 3 */

x->parent->colour = black;

x->parent->parent->colour = red;

right_rotate( T, x->parent->parent );

}

This is now a red-black tree ..

So we’re finished!

ECE 573: Data Structures & Algorithms

while ( (x != T->root) && (x->parent->colour == red) ) {

if ( x->parent == x->parent->parent->left ) {

/* If x's parent is a left, y is x's right 'uncle' */

y = x->parent->parent->right;

if ( y->colour == red ) {

/* case 1 - change the colours */

x->parent->colour = black;

y->colour = black;

x->parent->parent->colour = red;

/* Move x up the tree */

x = x->parent->parent;

else {

/* y is a black node */

if ( x == x->parent->right ) {

/* and x is to the right */

/* case 2 - move x up and rotate */

x = x->parent;

left_rotate( T, x );

else { /* case 3 */

x->parent->colour = black;

x->parent->parent->colour = red;

right_rotate( T, x->parent->parent );

}

}

else ....

There’s an equivalent set of

cases when the parent is to

the right of the grandparent!

ECE 573: Data Structures & Algorithms

- Addition
- InsertionComparisons O(log n)
- Fix-up
- At every stage,x moves up the tree at least one levelO(log n)

- OverallO(log n)

- Deletion
- AlsoO(log n)

- More complex
- ... but gives O(log n) behaviour in dynamic cases

ECE 573: Data Structures & Algorithms

- Code?
- This is not a course for masochists!
- You can find it in a text-book

- This is not a course for masochists!
- You need to know
- The algorithm exists
- What it’s called
- When to use it
- ie what problem does it solve?

- Its complexity
- Basically how it works
- Where to find an implementation
- How to transform it to your application

ECE 573: Data Structures & Algorithms

- Red-Black Trees
- A complex algorithm
- Gives O(log n) addition, deletion and search

- Software engineers
- Know about algorithms
- Know when to use them
- Know performance
- Know where to find an implementation
- Are too clever to re-invent wheels ...
- They re-use code
- So they have more time for
- Sailing, eating, drinking, ...
Anything else I should put here

- Sailing, eating, drinking, ...

ECE 573: Data Structures & Algorithms

- AVL Trees
- First balanced tree algorithm
- Discoverers: Adelson-Velskii and Landis

- Properties
- Binary tree
- Height of left and right-subtrees differ by at most 1
- Subtrees are AVL trees

AVL Tree

AVL Tree

ECE 573: Data Structures & Algorithms

- Theorem
- An AVL tree of height h has at leastFh+3+1 nodes

- Proof
- Let Sh be the size of the smallest AVL tree of height h
- Clearly, S0= 1 and S1= 2
- Also,Sh = Sh-1+ Sh-2 + 1
- A minimum height tree must be composed of min height trees differing in height by at most 1
- By induction ..
- Sh =Fh+3+1

ECE 573: Data Structures & Algorithms

- Now, for large i, Fi+1 / Fi = f,
where f = ½(1 + Ö5)

orFi» c (½(1 + Ö5))i

- Sh =Fh+3 + 1 = O( bh )

- n³Sh,so n is W( bh )
andh £logb n or h is O(log n)

ECE 573: Data Structures & Algorithms

- Now, for large i, Fi+1 / Fi = f,
where f = ½(1 + Ö5)

orFi» c (½(1 + Ö5))i

- Sh =Fh+3 + 1 = O( bh )

- n³Sh,so n is W( bh )
andh £logb n or h is O(log n)

- In this case, we can show
- h £1.44logb (n+2) - 1.328

his no worse than 44% higher than the optimum

ECE 573: Data Structures & Algorithms

- Insertion leads to non-AVL tree
- 4 cases
- 1 and 4 are mirror images
- 2 and 3 are mirror images

1

2

3

4

ECE 573: Data Structures & Algorithms

- Case 1 solved by rotation
- Case 4 is the mirror image rotation

ECE 573: Data Structures & Algorithms

- Case 2 needs a double rotation
- Case 3 is the mirror image rotation

ECE 573: Data Structures & Algorithms

- AVL trees can be implemented with a flag to indicate the balance state
- Insertion
- Insert a new node (as any binary tree)
- Work up the tree re-balancing as necessary to restorethe AVL property

typedef enum { LeftHeavy, Balanced, RightHeavy } BalanceFactor;

struct AVL_node { BalanceFactor bf; void *item; struct AVL_node *left, *right; }

ECE 573: Data Structures & Algorithms

- Insertion
- AVL : two passes through the tree
- Down to insert the node
- Up to re-balance

- Red-Black : two passes through the tree
- Down to insert the node
- Up to re-balance
but Red-Black is more popular??

- AVL : two passes through the tree

ECE 573: Data Structures & Algorithms

- Insertion
- If you read Cormen et al,
- There’s no reason to prefer a red-black tree

- However, in Weiss’ text
M A Weiss, Algorithms, Data Structures and Problem Solving with C++, Addison-Wesley, 1996

- you find that you can balance a red-black tree in one pass!
- Making red-black more efficient than AVLif coded properly!!!

- If you read Cormen et al,

ECE 573: Data Structures & Algorithms

- Insertion
- If you read Cormen et al,
- There’s no reason to prefer a red-black tree

- However, in Weiss’ text
M A Weiss, Algorithms, Data Structures and Problem Solving with C++, Addison-Wesley, 1996

- you find that you can balance a red-black tree in one pass!
- Making red-black more efficient than AVLif coded properly!!!

- If you read Cormen et al,

Moral: You need to read the literature!

ECE 573: Data Structures & Algorithms

- Insertion in one pass
- As you proceed down the tree,if you find a node with two red children,make it red and the children black
- This doesn’t alter the number of black nodes in any path
- If the parent of this node was red,a rotation is needed ...
- May need to be a single or a double rotation

ECE 573: Data Structures & Algorithms

- Adding 4 ...

Discover two red

children here

Swap colours around

ECE 573: Data Structures & Algorithms

- Adding 4 ...

Red sequence,

violates

red-black property

Rotate

ECE 573: Data Structures & Algorithms

- Adding 4 ...

Rotate

Add the 4

ECE 573: Data Structures & Algorithms

- Basically the same ideas
- 2-3 Trees
- 2-3-4 Trees
- Special cases of m-way trees ... coming!
- Variable number of children per node
- A more complex implementation
- 2-3-4 trees
- Map to red-black trees
Possibly useful to understand red-black trees

- Map to red-black trees

ECE 573: Data Structures & Algorithms

- AVL Trees
- First dynamically balanced tree
- Height within 44% of optimum
- Rebalanced with rotations
- O(log n)
- Less efficient than properly coded red-black trees

- 2-3, 2-3-4 trees
- m-way trees - Yet more variations
- 2-3-4 trees map to red-black trees

ECE 573: Data Structures & Algorithms

- Only two children per node?
- Reduce the depth of the tree to O(logmn)with m-way trees
- m children, m-1 keys per node
- m = 10 : 106 keys in 6 levels vs20 for a binary tree
- but ........

ECE 573: Data Structures & Algorithms

- But you have to search through the m keys in each node!
- Reduces your gain from having fewer levels!
- A curiosity only?

ECE 573: Data Structures & Algorithms

- All leaves are on the same level
- All nodes except for the root and the leaveshave
- at least m/2 children
- at most m children

- B+ trees
- All the keys in the nodes are dummies
- Only the keys in the leaves point to “real” data
- Linking the leaves
- Ability to scan the collection in orderwithout passing through the higher nodes

Each node is at least

half full of keys

ECE 573: Data Structures & Algorithms

- B+ trees
- All the keys in the nodes are dummies
- Only the keys in the leaves point to “real” data
- Data records kept in a separate area

ECE 573: Data Structures & Algorithms

- B+ trees
- Linking the leaves
- Ability to scan the collection in orderwithout passing through the higher nodes

- Linking the leaves

ECE 573: Data Structures & Algorithms

- Use - Large Databases
- Reading a disc block is much slower than reading memory ( ~ms vs ~ns )
- Put each block of keys in one disc block

Physical disc

blocks

ECE 573: Data Structures & Algorithms

- Insertion
- B-tree property : block is at least half-full of keys
- Insertion into block with m keys
- block overflows
- split block
- promote one key
- split parent if necessary
- if root is split, tree becomes one level deeper

ECE 573: Data Structures & Algorithms

- Insertion
- Insert 9
- Leaf node overflows,split it
- Promote middle (8)
- Root overflows,split it
- Promote middle (6)
- New root node formed
- Height increased by 1

ECE 573: Data Structures & Algorithms

- Disc blocks
- 512 - 8k bytes
100s of keys

- Use binary search within the block

- 512 - 8k bytes
- Overall
- O( log n )
- Matched to hardware!

- Deletion similar
- But merge blocks to maintain B-tree property (at least half full)

ECE 573: Data Structures & Algorithms