













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














1
2
Balance Rotation
Search Insert
3
Tree Balance
Worst case Search in O(n) time
Average case Search in O( log(n) ) time
Degenerate binary tree
Balanced binary tree
4
Tree Balance
Can we keep tree (mostly) balanced?
AVL trees Red-black trees
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
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
Properties ensures no leaf is twice as far from root as another leaf
8
Red-black Trees
9
Red-black Trees
Discovered in 1972 by Rudolf Bayer
Insert / delete may require complicated bookkeeping & rotations
TreeMap, TreeSet use red-black trees
10
Tree Rotations
Move nodes Change edges
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
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
16
Types of Multi-way Search Trees
Internal nodes have 2 or 3 children
Internal nodes have up to 26 children (for strings)
T = minimum degree Non-root internal nodes have T-1 to 2T-1 children All leaves have same depth
19
Multi-way Search Trees
Insert( 4 )
20
Multi-way Search Trees
Insert( 1 )
Split node Split parent
21
Height of tree is O( log (^) T (n) ) Reduces number of nodes accessed Wasted space for non-full nodes
1 node = 1 disk block Reduces number of disk blocks read
22
Key C can be decomposed into a sequence of subkeys C 1 , C 2 , … Cn Redundancy exists between subkeys
Store subkey at each node Path through trie yields full key
Huffman tree
25
Standard Tries
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
{ a, an, and, any, at }
27
Standard Trie Example
{ 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
Value between 1…m Reference to m children Array or linked list
Class Node { Letter value; // Letter V = { V 1 , V 2 , … Vm } Node child[ m ]; }
31
Compressed Trie
Internal node v of T is redundant if v has one child and is not the root
A chain of redundant nodes can be compressed Replace chain with single node Include concatenation of labels from chain
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
33
Compact Tries
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”
Uses O(s) space, where s = # of strings in the array Serves as an auxiliary index structure
34
Compact Representation
0 1 2 3 4
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
Collection of all searchable words Stored in compressed trie
Associated with a word List of pages (URLs) containing that word Called occurrence list
Ranked by relevance
39
Computational Biology
Sequence of 4 different nucleotides (ATCG) Portions of DNA sequence produce proteins (genes)
Master DNA sequence for organism For Human 46 chromosomes 3 billion nucleotides