Review of RB-Tree, AVL, B-Trees, and Skip Lists: Balanced Search Trees, Study notes of Computer Science

A lecture outline for cs 361 from the university of new mexico, covering various balanced search trees, including red-black trees, avl trees, b-trees, and skip lists. Topics include rb-properties, rb-insert, rb-insert-fixup, avl tree height, and b-tree properties.

Typology: Study notes

Pre 2010

Uploaded on 07/23/2009

koofers-user-efx
koofers-user-efx 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 361, Lecture 24
Jared Saia
University of New Mexico
Outline
RB-Tree Review
AVL Trees
B-Trees
Skip Lists
1
Red-Black Properties
A BST is a red-black tree if it satisfies the RB-Properties
1. Every node is either red or black
2. The root is black
3. Every leaf (NIL) is black
4. If a node is red, then b oth its children are black
5. For each node, all paths from the node to descendant leaves
contain the same number of black nodes
2
RB-Insert(T,z)
1. Set left(z) and right(z) to be NIL
2. Let y be the last no de processed during a search for zin T
3. Insert zas the appropriate child of y(left child if key(z)y,
right child otherwise)
4. Color z red
5. Call the procedure RB-Insert-Fixup
3
RB-Insert-Fixup(T,z)
RB-Insert-Fixup(T,z){
while (color(p(z)) is red){
case 1: z’s uncle is red{
do case 1
}
case 2: z’s uncle is black and z is a right child{
do case 2
}
case 3: z’s uncle is black and z is a left child{
do case 3
}
}
color(root(T)) = black;
}
4
Case 1




























































D
B
A
CC
AD
B
C
B
A
D
C
B
A
D
T1
T2 T3
T4 T5
T1 T2
T3 T4 T5
T1
T2 T3
T4 T5
T1 T2
T3 T4 T5
z
y
new z
z
y
new z
5
pf3
pf4

Partial preview of the text

Download Review of RB-Tree, AVL, B-Trees, and Skip Lists: Balanced Search Trees and more Study notes Computer Science in PDF only on Docsity!

CS 361, Lecture 24

Jared Saia University of New Mexico

Outline

  • RB-Tree Review
  • AVL Trees
  • B-Trees
  • Skip Lists

Red-Black Properties

A BST is a red-black tree if it satisfies the RB-Properties

  1. Every node is either red or black
  2. The root is black
  3. Every leaf (NIL) is black
  4. If a node is red, then both its children are black
  5. For each node, all paths from the node to descendant leaves contain the same number of black nodes

RB-Insert(T,z)

  1. Set left(z) and right(z) to be NIL
  2. Let y be the last node processed during a search for z in T
  3. Insert z as the appropriate child of y (left child if key(z)≤ y, right child otherwise)
  4. Color z red
  5. Call the procedure RB-Insert-Fixup

RB-Insert-Fixup(T,z)

RB-Insert-Fixup(T,z){ while (color(p(z)) is red){ case 1: z’s uncle is red{ do case 1 } case 2: z’s uncle is black and z is a right child{ do case 2 } case 3: z’s uncle is black and z is a left child{ do case 3 } } color(root(T)) = black; }

Case 1



 





 

 

 





 



   

      

      

      

  

D B

A

C C A (^) D B

C

B

A

D

C B

A

D

T T2 T

T4 T

T1 T

T3 T4^ T

T T2 T

T4 T

T1 T

T3 (^) T4 T

z

y

new z

z

y

new z

Case 2 and 3



 

 



 



B

A

C

T T2 T

z

T4 y

C B

A T1 T

T

T

Case 2 Case 3

B

A C

z

z

y

T1 T2^ T3^ T

Loop Invariant

At the start of each iteration of the loop:

  • Node z is red
  • If parent(z) is the root, then parent(z) is black
  • If there is a violation of the red-black properties, there is at most one violation, and it is either property 2 or 4. If there is a violation of property 2, it occurs because z is the root and is red. If there is a violation of property 4, it occurs because both z and parent(z) are red.

Pseudocode

  • Detailed Pseudocode for RB-Insert and RB-Insert-Fixup is in the book, Chapter 13.
  • There’s also a detailed proof of correctness for RB-Insert- Fixup in the the same section

Other Balanced BSTs

  • We’ll now briefly discuss some other balanced BSTs
  • They all implement Insert, Delete, Lookup, Successor, Pre- decessor, Maximum and Minimum efficiently

AVL Trees

  • An AVL tree is height-balanced: For each node x, the heights of the left and right subtrees of x differ by at most 1
  • Each node has an additional height field h(x)
  • Claim: An AVL tree with n nodes has height O(log n)

AVL Trees

  • Claim: An AVL tree with n nodes has height O(log n)
  • Q: For an AVL tree of height h, how many nodes must it have in it?
  • A: We can write a recurrence relation. Let T (h) be the minimum number of nodes in a tree of height h
  • Then T (h) = T (h − 1) + T (h − 2) + 1, T (2) = T (1) ≥ 1
  • This is similar to the recurrence relation for Fibonnaci num- bers! Solution:

T (h) = √^1 5

( 1 +

)h − 2

Note

  • The above properties imply that the height of a B-tree is no more than logt n+1 2 , for t ≥ 2, where n is the number of keys.
  • If we make t, larger, we can save a larger (constant) fraction over RB-trees in the number of nodes examined
  • A (2-3-4)-tree is just a B-tree with t = 2

In-Class Exercise

We will now show that for any B-Tree with height h and n keys, h ≤ logt n+1 2 , where t ≥ 2.

Consider a B-Tree of height h > 1

  • Q1: What is the minimum number of nodes at depth 1, 2, and 3
  • Q2: What is the minimum number of nodes at depth i?
  • Q3: Now give a lowerbound for the total number of keys (e.g. n ≥???)
  • Q4: Show how to solve for h in this inequality to get an upperbound on h

Splay Trees

  • A Splay Tree is a kind of BST where the standard operations run in O(log n) amortized time
  • This means that over l operations (e.g. Insert, Lookup, Delete, etc), where l is sufficiently large, the total cost is O(l ∗ log n)
  • In other words, the average cost per operation is O(log n)
  • However a single operation could still take O(n) time
  • In practice, they are very fast

Skip Lists

  • Technically, not a BST, but they implement all of the same operations
  • Very elegant randomized data structure, simple to code but analysis is subtle
  • They guarantee that, with high probability, all the major op- erations take O(log n) time
  • We’ll discuss them more next class

High Level Analysis

Comparison of various BSTs

  • RB-Trees: + guarantee O(log n) time for each operation, easy to augment, − high constants
  • AVL-Trees: + guarantee O(log n) time for each operation, − high constants
  • B-Trees: + works well for trees that won’t fit in memory, − inserts and deletes are more complicated
  • Splay Tress: + small constants, − amortized guarantees only
  • Skip Lists: + easy to implement, − runtime guarantees are probabilistic only

Which Data Structure to use?

  • Splay trees work very well in practice, the “hidden constants” are small
  • Unfortunately, they can not guarantee that every operation takes O(log n)
  • When this guarantee is required, B-Trees are best when the entire tree will not be stored in memory
  • If the entire tree will be stored in memory, RB-Trees, AVL- Trees, and Skip Lists are good