Balanced Trees: Understanding 2-3-4 Trees and Red-Black Trees, Lecture notes of Data Structures and Algorithms

The concept of balanced trees, specifically focusing on 2-3-4 trees and their evolution into red-black trees. Learn about the properties, insertion process, and time complexity of these tree structures.

Typology: Lecture notes

2021/2022

Uploaded on 09/27/2022

aeinstein
aeinstein 🇺🇸

4.6

(22)

259 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 16: Balanced Trees
erm 204
2-3-4 Trees and Red-
Black Trees
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Balanced Trees: Understanding 2-3-4 Trees and Red-Black Trees and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

2-3-4 Trees and Red-

Black Trees

2-3-4 Trees Revealed

  • Nodes store 1, 2, or 3 keys and have 2, 3, or 4 children, respectively
  • All leaves have the same depth

b e h n r

a (^) c d f g (^) i l m p s x

--- (^) log ( N + 1 ) ≤ height ≤ log( N + 1 )

k

b e h n r

  • That means if d = N 1/2, we get a height of 2
  • However, searching out the correct child

on each level requires O(log N 1/2^ ) by binary search

  • 2 log N1/2^ = O(log N) which is not as good

as we had hoped for!

  • 2-3-4-trees will guarantee O(log N) height

using only 2, 3, or 4 children per node

Why 2-3-4?

  • Why not minimize height by maximizing children in a “d-tree”?
  • Let each node have d children so that we

get O(log (^) d N) search time! Right? log dN = log N/log d

Insertion into 2-3-4 Trees

  • Insert the new key at the lowest internal node reached in the search
  • What about a 4-node?
  • We can’t insert another key!

g (^) d d g

  • 3-node becomes 4-node
  • 2-node becomes 3-node

f^ d g^ d f g

Splitting the Tree

As we travel down the tree, if we encounter any 4-node we will break it up into 2-nodes. This guarantees that we will never have the problem of inserting the middle element of a former 4- node into its parent 4-node.

a (^) f i l p r x

a (^) f i l p r

g

g

n

c (^) t

x

c n t

Whoa, cowboy

a (^) f i l p r x

a (^) f i l p r x

c (^) t

n

n

c t

g

g

Whoa, cowboy

Time Complexity of Insertion

in 2-3-4 Trees

Time complexity:

  • A search visits O(log N) nodes
  • An insertion requires O(log N) node splits
  • Each node split takes constant time
  • Hence, operations Search and Insert each take time O(log N)

Notes:

  • Instead of doing splits top-down, we can perform them bottom-up starting at the in- sertion node, and only when needed. This is called bottom-up insertion.
  • A deletion can be performed by fusing nodes (inverse of splitting), and takes O(log N) time

Beyond 2-3-4 Trees

What do we know about 2-3-4 Trees?

  • Balanced
  • O(log N) search time
  • Different node structures

Can we get 2-3-4 tree advantages in a binary

tree format???

Welcome to the world of Red-Black Trees!!!

Siskel

Ebert

Roberto

2-3-4 Tree Evolution

Note how 2-3-4 trees relate to red-black trees

2-3-4 (^) Red-Black

Now we see red-black trees are just a way of representing 2-3-4 trees!

or

More Red-Black Tree

Properties

N # of internal nodes L # leaves (= N + 1) H height B black height

Property 1: 2 B^ ≤ N + 1 ≤ 4 B

Property 2:

This implies that searches take time O(logN)!

Property 3:

--- (^) log ( N + 1 ) ≤ B ≤ log( N + 1 )

log ( N + 1 ) ≤ H ≤ 2 log( N + 1 )

Let: n be the new node p be its parent g be its grandparent

Insertion - Plain and Simple

Case 1: Incoming edge of p is black

g

p

n

STOP!

Pretty easy, huh?

Well... it gets messier...

No violation

Restructuring

We call this a “ right rotation

  • No further work on tree is necessary
  • Inorder remains unchanged
  • Tree becomes more balanced
  • No two consecutive red edges!

Case 2: Incoming edge of p is red, and

its sibling is black

g

p

n

g

p

n

Uh oh!

Much Better!

Double Rotations

What if the new node is between its parent and grandparent in the inorder sequence?

We must perform a “double rotation”

(which is no more difficult than a “single” one)

g

p

n

p g

n

This would be called a “left-right double rotation”

Last of the Rotations

And this would be called a “right-left double rotation”

g

p

n

g p

n