2-3-4 Trees and Red- Black Trees 204 erm CS 16: Balanced Trees - - PDF document

2 3 4 trees and red black trees
SMART_READER_LITE
LIVE PREVIEW

2-3-4 Trees and Red- Black Trees 204 erm CS 16: Balanced Trees - - PDF document

CS 16: Balanced Trees 2-3-4 Trees and Red- Black Trees 204 erm CS 16: Balanced 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 k n r b e h n r b e


slide-1
SLIDE 1

CS 16: Balanced Trees erm

204

2-3-4 Trees and Red- Black Trees

slide-2
SLIDE 2

CS 16: Balanced Trees erm

205

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 1 2

  • N

1 + ( ) log height N 1 + ( ) log ≤ ≤ k b e h n r

slide-3
SLIDE 3

CS 16: Balanced Trees erm

206

  • Introduction of nodes with more than 1 key,

and more than 2 children

4-node:

  • 3 keys, 4 links

2-3-4 Tree Nodes 2-node:

  • same as a binary node

<a >a

3-node:

  • 2 keys, 3 links

>b <a <a >c >a <b >b <c a b c >a <b a a b

slide-4
SLIDE 4

CS 16: Balanced Trees erm

207

  • That means if d = N1/2, we get a height of 2
  • However, searching out the correct child
  • n each level requires O(log N1/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(logd N) search time! Right? logdN = log N/log d

slide-5
SLIDE 5

CS 16: Balanced Trees erm

208

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!

d g d g

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

d g d f g f

slide-6
SLIDE 6

CS 16: Balanced Trees erm

209

  • Now we can perform the

insertion using one of the previous two cases

  • Since we follow this

method from the root down to the leaf, it is called top down insertion

Top Down Insertion

  • In our way down the tree, whenever we

reach a 4-node, we break it up into two 2- nodes, and move the middle element up into the parent node n g e f n d g d f g f n e d e

slide-7
SLIDE 7

CS 16: Balanced Trees erm

210

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

  • f inserting the middle element of a former 4-

node into its parent 4-node. a x f i l p r a f i l p r g g n c t x c n t

Whoa, cowboy

slide-8
SLIDE 8

CS 16: Balanced Trees erm

211

a x f i l p r a x f i l p r c t n n c t g g

Whoa, cowboy

slide-9
SLIDE 9

CS 16: Balanced Trees erm

212

a x p r t n f l a x p r c i t n l a x p r c t n g f i l c i g f g

Whoa, cowboy

slide-10
SLIDE 10

CS 16: Balanced Trees erm

213

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

slide-11
SLIDE 11

CS 16: Balanced Trees erm

214

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

slide-12
SLIDE 12

CS 16: Balanced Trees erm

215

Red-Black Tree

A red-black tree is a binary search tree with the following properties:

  • edges are colored red or black
  • no two consecutive red edges
  • n any root-leaf path
  • same number of black edges
  • n any root-leaf path

(= black height of the tree)

  • edges connecting leaves are black

Black Edge Red Edge

slide-13
SLIDE 13

CS 16: Balanced Trees erm

216

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!

  • r
slide-14
SLIDE 14

CS 16: Balanced Trees erm

217

More Red-Black Tree Properties

N # of internal nodes L # leaves (= N + 1) H height B black height Property 1: 2B ≤ N + 1 ≤ 4B Property 2: This implies that searches take time O(logN)! Property 3: 1 2

  • N

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

slide-15
SLIDE 15

CS 16: Balanced Trees erm

218

Insertion into Red-Black Trees

1.Perform a standard search to find the leaf where the key should be added 2.Replace the leaf with an internal node with the new key 3.Color the incoming edge of the new node red 4.Add two new leaves, and color their incoming edges black 5.If the parent had an incoming red edge, we now have two consecutive red edges! We must reorganize tree to remove that

  • violation. What must be done depends on

the sibling of the parent. R G R G

slide-16
SLIDE 16

CS 16: Balanced Trees erm

219

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

slide-17
SLIDE 17

CS 16: Balanced Trees erm

220

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!

slide-18
SLIDE 18

CS 16: Balanced Trees erm

221

More Rotations

Similar to a right rotation, we can do a “left rotation”... Simple, huh? g p n g p n

slide-19
SLIDE 19

CS 16: Balanced Trees erm

222

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 g p n This would be called a “left-right double rotation”

slide-20
SLIDE 20

CS 16: Balanced Trees erm

223

Last of the Rotations

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

slide-21
SLIDE 21

CS 16: Balanced Trees erm

224

  • We call this a “promotion”
  • Note how the black depth remains un-

changed for all of the descendants of g

  • This process will continue upward beyond

g if necessary: rename g as n and repeat.

Bottom-Up Rebalancing

Case 3: Incoming edge of p is red and its sibling is also red g p n g p n

slide-22
SLIDE 22

CS 16: Balanced Trees erm

225

Summary of Insertion

  • If two red edges are present, we do either
  • a restructuring (with a simple or double

rotation) and stop, or

  • a promotion and continue
  • A restructuring takes constant time and is

performed at most once. It reorganizes an

  • ff-balanced section of the tree.
  • Promotions may continue up the tree and

are executed O(log N) times.

  • The time complexity of an insertion is

O(logN).

slide-23
SLIDE 23

CS 16: Balanced Trees erm

226

An Example

Start by inserting “REDSOX” into an empty tree E R D S O X Now, let’s insert “C U B S”...

slide-24
SLIDE 24

CS 16: Balanced Trees erm

227

E R D S O X

A Cool Example C

E R D S O X C

slide-25
SLIDE 25

CS 16: Balanced Trees erm

228

An Unbelievable Example U

E R D S O X C E R D S O X C What should we do?

Oh No!

U

slide-26
SLIDE 26

CS 16: Balanced Trees erm

229

E R D S O X C U E R D S O X C

Double Rotation

U

slide-27
SLIDE 27

CS 16: Balanced Trees erm

230

A Beautiful Example B

E R D S O X C U E R S O X D U B C What now?

slide-28
SLIDE 28

CS 16: Balanced Trees erm

231

E R S O X D U B C

Rotation

E R S O X C U D B

slide-29
SLIDE 29

CS 16: Balanced Trees erm

232

S A Super Example

E R S O X C U D B E R S O X C U D B S Holy Consecutive Red Edges, Batman!

We could’ve placed it on either side

slide-30
SLIDE 30

CS 16: Balanced Trees erm

233

E R S O X C U D B S Use the E R S O X C U D B S BIFF! Bat-Promoter!!

slide-31
SLIDE 31

CS 16: Balanced Trees erm

234

E R S O X C U D B S E R S O X C U D B S

Rotation

The SUN lab and Red-Bat trees are safe... ...for now!!!