Docsity

Balanced Trees: 2-3-4 Trees and Red-Black Trees, Slides of Data Representation and Algorithm Design

Toggle sidebar
bg1
4.4 Balanced Trees
2
Symbol Table Review
Symbol table: key-value pair abstraction.
!Insert a value with specified key.
!Search for value given key.
!Delete value with given key.
Randomized BST.
!O(log N) time per op. [unless you get ridiculously unlucky]
!Store subtree count in each node.
!Generate random numbers for each insert/delete op.
This lecture. 2-3-4 trees, red-black trees, B-trees.
2-3-4 Trees
4
2-3-4 Tree
2-3-4 tree. Generalize node to allow multiple keys; keep tree balanced.
Perfect balance. Every path from root to leaf has same length.
Allow 1, 2, or 3 keys per node.
!2-node: one key, two children.
!3-node: two keys, three children.
!4-node: three keys, four children.
M O
K R
WC E
< K K — R> R
D F G JA S V Y ZQNL
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Balanced Trees: 2-3-4 Trees and Red-Black Trees and more Slides Data Representation and Algorithm Design in PDF only on Docsity!

4.4 Balanced Trees

2

Symbol Table Review

Symbol table: key-value pair abstraction.

! Insert a value with specified key.

! Search for value given key.

! Delete value with given key.

Randomized BST.

! O(log N) time per op. [unless you get ridiculously unlucky]

! Store subtree count in each node.

! Generate random numbers for each insert/delete op.

This lecture. 2 - 3-4 trees, red-black trees, B-trees.

2-3-4 Trees

4

2 - 3 - 4 Tree

2-3-4 tree. Generalize node to allow multiple keys; keep tree balanced.

Perfect balance. Every path from root to leaf has same length.

Allow 1, 2, or 3 keys per node.

! 2-node: one key, two children.

! 3-node: two keys, three children.

! 4-node: three keys, four children.

M O

K R

C E W

< K K — R > R A D F G J L N Q S V Y Z

5 2 - 3 - 4 Tree: Search

Search.

! Compare search key against keys in node.

! Find interval containing search key.

! Follow associated link (recursively).

M O

K R

C E W

< K K — R > R A D F G J L N Q S V Y Z 6 2 - 3 - 4 Tree: Insert

Insert.

! Search to bottom for key.

! 2-node at bottom: convert to 3-node.

! 3-node at bottom: convert to 4-node.

! 4-node at bottom: ??

M O

K R

C E W

< K K — R > R A D F G J L N Q S V Y Z 7 2 - 3 - 4 Tree: Splitting Four Nodes

Transform tree on the way down.

! Ensures last node is not a 4-node.

! Local transformation to split 4-nodes:

Invariant. Current node is not a 4-node.

Consequence. Insertion at bottom is easy since it's not a 4-node.

8 2 - 3 - 4 Tree: Splitting a Four Node

Ex. To split a four node, move middle key up.

A-C

K Q W

D

E-J L-P R-V X-Z A-C

K

D Q

E-J L-P R-V X-Z

W

Red-Black Trees

14

Red-Black Tree

Represent 2-3-4 tree as a BST.

! Use "internal" edges for 3- and 4- nodes.

! Correspondence between 2-3-4 trees and red-black trees.

red glue not 1-1 because 3-nodes swing either way. 15

Red-Black Tree: Splitting Nodes

Two easy cases. Switch colors.

16

Red-Black Tree: Splitting Nodes

Two easy cases. Switch colors.

Two hard cases. Use rotations.

do single rotation

do double rotation

17 Red-Black Tree: Splitting Nodes

right rotate R!

left rotate E!

change colors

inserting G

18 Red-Black Tree: Insertion E A P E

X

M

L

19 Red-Black Tree: Balance

Property A. Every path from root to leaf has same number of black links.

Property B. At most one red link in-a-row.

Property C. Height of tree is less than 2 lg N + 2.

20 Symbol Table: Implementations Cost Summary

Note. Comparison within nodes are accounted for.

  • † aNss iusm tehse hnausmhb mera po (^) fi sn roadnedso emv (^) efro ri (^) naslel (^) rkteeyds ‡ § parmoobratbizileisdt igcu gauraarnatenteee Sorted array Implementation Unsorted list BST Randomized BST Splay log N Search N N log N ‡ log N §

N

Insert 1 N log N ‡ log N § log N Search N log N † log N log N §

N

Insert 1 log N † log N log N §

N

Delete 1 log N † log N log N §

Worst Case Average Case

Red-Black log N log N log N log N log N

N

Delete 1 N log N ‡ log N § log N Hashing N 1 N 1 * 1 * 1 *

25 B-Tree Example (cont) 26 Symbol Table: Implementations Cost Summary

B-Tree. Number of page accesses is logMN per op.

Sorted array Implementation Unsorted list BST Randomized BST Splay log N Search N N log N ‡ log N §

N

Insert N N log N ‡ log N § log N Search N log N † log N § log N §

N / 2

Insert N log N † log N § log N §

N / 2

Delete N log N † log N § log N § Worst Case Average Case Red-Black log N §^ log N §^ log N §^ log N §^ log N §

N

Delete N N log N ‡ log N § log N § B-Tree 1 1 1 1 1 1 page accesses Hashing N 1 N 1 * 1 * 1 * effectively a constant 27 B-Trees in the Wild

Variants.

! B trees: Bayer-McCreight. [1972, Boeing]

! B+ trees: all data in external nodes.

! B* trees: keeps pages at least 2/3 full.

! R-trees for spatial searching: GIS, VLSI.

File systems.

! Windows: HPFS.

! Mac: HFS, HFS+.

! Linux: ReiserFS, XFS, Ext3FS, JFS.

Databases.

! Most common index type in modern databases.

! ORACLE, DB2, INGRES, SQL, PostgreSQL, …

28 Summary

Goal. ST implementation with log N guarantee for all ops.

! Probabilistic: randomized BST.

! Amortized: splay tree.

! Worst-case: red-black tree.

! Algorithms are variations on a theme: rotations when inserting.

Abstraction extends to give search algorithms for huge files.

! B-tree.

Splay Trees

30

Splay Trees

Splay trees = self-adjusting BST.

! Tree automatically reorganizes itself after each op.

! After inserting x or searching for x, rotate x up to root using

double rotations.

! Tree remains "balanced" without explicitly storing any balance

information.

Amortized guarantee: any sequence of N ops, starting from empty

splay tree, takes O(N log N) time.

! Height of tree can be N.

! Individual op can take linear time.

31

A Self-Adjusting Tree

32

Splay Trees

Splay.

! Check two links above current node.

! ZIG-ZAG: if orientations differ, same as root insertion.

! ZIG-ZIG: if orientations match, do top rotation first.

ZIG-ZAG

B C

y

D

z

x

C D

z

y

A

A B

x

37 Splay Example

Search for 1. 10

ZIG-ZIG

38 Splay Example

Search for 1. 10

ZIG-ZIG

39 Splay Example

Search for 1.

ZIG

40 Splay Example

Search for 1. 1

41 Splay Example

Search for 2.

Splay(2)

42 Splay Trees

Intuition.

! Splay rotations halve search path.

! Reduces length of path for many other nodes in tree.

insert 1 , 2 , …, 40

search 1

search 2

search 3

search 4

insert 1 , 2 , …, 40

search for

random key

43 Symbol Table: Implementations Cost Summary

Splay: Sequence of N ops takes linearithmic time.

Ahead: Can we do all ops in log N time guaranteed?

  • assumes we know location of node to be deleted † if delete allowed, insert/search become sqrt(N) ‡ probabilistic guarantee § amortized guarantee Sorted array Implementation Unsorted list BST Randomized BST Splay log N Search N N log N ‡ log N §

N

Insert 1 N log N ‡ log N § log N Search N log N log N log N §

N

Insert 1 log N log N log N §

N

Delete 1 sqrt(N) † log N log N § Worst Case Average Case N Delete 1 N log N ‡ log N § Hashing N 1 N 1 * 1 * 1 *