Docsity

Advanced Data Structures: Balanced Trees and Multi-way Search Trees - Prof. Nelson Padua-P, Study notes of Computer Science

Partial preview of the text

Download Advanced Data Structures: Balanced Trees and Multi-way Search Trees - Prof. Nelson Padua-P and more Study notes Computer Science in PDF only on Docsity!

1

CMSC 132:

Object-Oriented Programming II

Advanced Tree Structures

Department of Computer Science

University of Maryland, College Park

2

Overview

Binary trees

Balance Rotation

Multi-way trees

Search Insert

Indexed tries

3

Tree Balance

Degenerate

Worst case Search in O(n) time

Balanced

Average case Search in O( log(n) ) time

Degenerate binary tree

Balanced binary tree

4

Tree Balance

Question

Can we keep tree (mostly) balanced?

Self-balancing binary search trees

AVL trees Red-black trees

Approach

Select invariant (that keeps tree balanced) Fix tree after each insertion / deletion Maintain invariant using rotations Provides operations with O( log(n) ) worst case

7

Red-black Trees

Properties

Binary search tree Every node is red or black The root is black Every leaf is black All children of red nodes are black For each leaf, same # of black nodes on path to root

Characteristics

Properties ensures no leaf is twice as far from root as another leaf

8

Red-black Trees

Example

9

Red-black Trees

History

Discovered in 1972 by Rudolf Bayer

Algorithm

Insert / delete may require complicated bookkeeping & rotations

Java collections

TreeMap, TreeSet use red-black trees

10

Tree Rotations

Changes shape of tree

Move nodes Change edges

Types

Single rotation Left Right Double rotation Left-right Right-left

13

Example – Single Rotations

T 0 T 1 T 3 T 0 T 1 T 2

T 3

single left rotation

T 2

T 3 T 2 T 1

T 0 T 1 T 2 T 3

T 0

single right rotation

14

Example – Double Rotations

right-left double rotation

T 0 T 2 T 1

T 3 T 0

T 2 T 1 T 3

T 3 T 1 T 2

T 0 T 3

T 1 T 0 T 2

left-right double rotation

15

Multi-way Search Trees

Properties

Generalization of binary search tree Node contains 1…k keys (in sorted order) Node contains 2…k+1 children Keys in j th^ child < j th^ key < keys in (j+1) th^ child

Examples

1 3 7 9 19 21^44

16

Types of Multi-way Search Trees

2-3 tree

Internal nodes have 2 or 3 children

Index search trie

Internal nodes have up to 26 children (for strings)

B-tree

T = minimum degree Non-root internal nodes have T-1 to 2T-1 children All leaves have same depth

T-1 … 2T-

1 2 … 2T

c

a o s

19

Multi-way Search Trees

Insert Example (for 2-3 tree)

Insert( 4 )

20

Multi-way Search Trees

Insert Example (for 2-3 tree)

Insert( 1 )

Split node Split parent

21

B-Trees

Characteristics

Height of tree is O( log (^) T (n) ) Reduces number of nodes accessed Wasted space for non-full nodes

Popular for large databases

1 node = 1 disk block Reduces number of disk blocks read

22

Indexed Search Tree ( Trie)

Special case of tree

Applicable when

Key C can be decomposed into a sequence of subkeys C 1 , C 2 , … Cn Redundancy exists between subkeys

Approach

Store subkey at each node Path through trie yields full key

Example

Huffman tree

C 3

C 1

C 2

C 3 C 4

25

Standard Tries

Approach

Each node (except root) is labeled with a character Children of node are ordered (alphabetically) Paths from root to leaves yield all input strings

Trie for Morse Code

26

Standard Trie Example

For strings

{ a, an, and, any, at }

27

Standard Trie Example

For strings

{ bear, bell, bid, bull, buy, sell, stock, stop }

a

e

b

r

l

l

s

u

l

l

y

e t

l

l

o

c

k

p

i

d

28

Standard Tries

Node structure

Value between 1…m Reference to m children Array or linked list

Example

Class Node { Letter value; // Letter V = { V 1 , V 2 , … Vm } Node child[ m ]; }

31

Compressed Trie

Observation

Internal node v of T is redundant if v has one child and is not the root

Approach

A chain of redundant nodes can be compressed Replace chain with single node Include concatenation of labels from chain

Result

Internal nodes have at least 2 children Some nodes have multiple characters

32

Compressed Trie

e

b

ar ll

s

u

ll y

ell to

ck p

id

a

e

b

r

l

l

s

u

l

l

y

e t

l

l

o

c

k

p

i

d

Example

33

Compact Tries

Compact representation of a compressed trie

Approach

For an array of strings S = S[0], … S[s-1] Store ranges of indices at each node Instead of substring Represent as a triplet of integers (i, j, k) Such that X = s[i][j..k] Example: S[0] = “abcd”, (0,1,2) = “bc”

Properties

Uses O(s) space, where s = # of strings in the array Serves as an auxiliary index structure

34

Compact Representation

Example

s e e

b e a r

s e l l

s t o c k

b u l l

b u y

b i d

h e

b e l l

s t o p

0 1 2 3 4

S [0] = a r

S [1] = S [2] = S [3] =

S [4] = S [5] = S [6] =

S [7] = S [8] = S [9] =

0 1 2 3 0 1 2 3

1, 1, 1

1, 0, 0 0, 0, 0

4, 1, 1

0, 2, 2

3, 1, 2

1, 2, 3 8, 2, 3

6, 1, 2

4, 2, 3 5, 2, 2 2, 2, 3 3, 3, 4 9, 3, 3

7, 0, 3

0, 1, 1

37

Suffix Trie Example

e nimize

nimize ze

i mi ze

mize nimize ze

m i n i m i z e 0 1 2 3 4 5 6 7

38

Tries and Web Search Engines

Search engine index

Collection of all searchable words Stored in compressed trie

Each leaf of trie

Associated with a word List of pages (URLs) containing that word Called occurrence list

Trie is kept in memory (fast)

Occurrence lists kept in external memory

Ranked by relevance

39

Computational Biology

DNA

Sequence of 4 different nucleotides (ATCG) Portions of DNA sequence produce proteins (genes)

Genome

Master DNA sequence for organism For Human 46 chromosomes 3 billion nucleotides