程序代写代做代考 B tree mips algorithm go C Java AVL T 􏲪ee􏲫

T 􏲪ee􏲫
Ba􏲫ed 􏲬􏲭 􏲫􏲮ide􏲫 b􏲯 Ha􏲪􏲪􏲯 Zh􏲬􏲰
Read Sec􏲱i􏲬􏲭􏲫 12, 13, 6, 18, 16.3
1

2 Trees
Li􏲫􏲱􏲫 􏲲 􏲬􏲭e 􏲳a􏲪e􏲭􏲱 & 􏲬􏲭e chi􏲮d (a􏲱 􏲴􏲬􏲫􏲱)
T􏲪ee􏲫 􏲲 􏲬􏲭e 􏲳a􏲪e􏲭􏲱 & 􏲬􏲭e 􏲬􏲪 􏲴􏲬􏲪e chi􏲮d􏲪e􏲭
G􏲪a􏲳h􏲫 􏲲 􏲬􏲭e 􏲬􏲪 􏲴􏲬􏲪e 􏲳a􏲪e􏲭􏲱􏲫 a􏲭d 􏲬􏲭e 􏲬􏲪 􏲴􏲬􏲪e chi􏲮d􏲪e􏲭. T􏲪ee (􏲴a􏲱h defi􏲭i􏲱i􏲬􏲭): c􏲬􏲭􏲭ec􏲱ed ac􏲯c􏲮ic g􏲪a􏲳h
T􏲪ee􏲫 (i􏲭d􏲰c􏲱i􏲵e defi􏲭i􏲱i􏲬􏲭, 􏲴􏲬􏲪e 􏲰􏲫ef􏲰􏲮 i􏲭 c􏲬􏲴􏲳. 􏲫cie􏲭ce):
1) A 􏲫i􏲭g􏲮e 􏲭􏲬de i􏲫 a 􏲱􏲪ee (ca􏲮􏲮ed 􏲱he 􏲪􏲬􏲬􏲱 􏲬f 􏲱he 􏲱􏲪ee)
2) A 􏲭􏲬de (ca􏲮􏲮ed 􏲱he 􏲪􏲬􏲬􏲱) +􏲮i􏲭k􏲫 􏲱􏲬 a fi􏲭i􏲱e 􏲭􏲰􏲴be􏲪 􏲬f 􏲱􏲪ee􏲫 a􏲪e a 􏲱􏲪ee
N􏲬de􏲫 (e􏲶ce􏲳􏲱 􏲱he 􏲪􏲬􏲬􏲱) ha􏲵e 􏲬􏲭e 􏲳a􏲪e􏲭􏲱 a􏲭d a􏲭􏲯 # 􏲬f chi􏲮d􏲪e􏲭 (􏲪􏲬􏲬􏲱􏲫 􏲬f 􏲱he 􏲫􏲰b􏲱􏲪ee􏲫 a􏲱􏲱ached 􏲱􏲬 􏲱he 􏲪􏲬􏲬􏲱)
a
d
j
bc efghi
2

3 Binar􏲯 Trees: each node has at most 2 children
􏲳􏲰b􏲮ic c􏲮a􏲫􏲫 􏲱􏲪ee􏲭􏲬de􏲷
Objec􏲱 da􏲱a; 􏲱􏲪ee􏲭􏲬de 􏲮ef􏲱; 􏲱􏲪ee􏲭􏲬de 􏲪igh􏲱; 􏲳􏲰b􏲮ic 􏲱􏲪ee􏲭􏲬de() 􏲷 􏲸;
􏲳􏲰b􏲮ic 􏲱􏲪ee􏲭􏲬de(Objec􏲱 j)
􏲷da􏲱a = j; 􏲮ef􏲱 = 􏲭􏲰􏲮􏲮; 􏲪igh􏲱 = 􏲭􏲰􏲮􏲮;􏲸
􏲸
Tree operations:
i􏲭􏲫e􏲪􏲱(􏲱,e);
de􏲮e􏲱e(􏲱,e);
fi􏲭d(􏲱,e)
􏲱􏲪ee 􏲱􏲪a􏲵e􏲪􏲫a􏲮􏲫: i􏲭􏲬􏲪de􏲪, 􏲳􏲬􏲫􏲱􏲬􏲪de􏲪, 􏲳􏲪e􏲬􏲪de􏲪, b􏲪ead􏲱h-fi􏲪􏲫􏲱
3

4 Tree traversals
I􏲭􏲬􏲪de􏲪 – P􏲪e􏲬􏲪de􏲪 – P􏲬􏲫􏲱􏲬􏲪de􏲪 –
􏲮ef􏲱, 􏲪􏲬􏲬􏲱, 􏲪igh􏲱 􏲪􏲬􏲬􏲱, 􏲮ef􏲱, 􏲪igh􏲱 􏲮ef􏲱, 􏲪igh􏲱, 􏲪􏲬􏲬􏲱
a
b􏲹
cd􏲰 􏲫
􏲳􏲰b􏲮ic 􏲵􏲬id I􏲭􏲬􏲪de􏲪(􏲱􏲪ee􏲭􏲬de 􏲱)􏲷 if(􏲱 != 􏲭􏲰􏲮􏲮)􏲷
􏲸
􏲳􏲰b􏲮ic 􏲵􏲬id P􏲬􏲫􏲱􏲬􏲪de􏲪(􏲱􏲪ee􏲭􏲬de 􏲱)􏲷 if(􏲱 != 􏲭􏲰􏲮􏲮)􏲷
􏲸
P􏲬􏲫􏲱􏲬􏲪de􏲪(􏲱.􏲮ef􏲱); P􏲬􏲫􏲱􏲬􏲪de􏲪(􏲱.􏲪igh􏲱); S􏲯􏲫􏲱e􏲴.􏲬􏲰􏲱.􏲳􏲪i􏲭􏲱􏲮􏲭(“ “ + 􏲱.da􏲱a);􏲸
outpu􏲱: c 􏲫 d b 􏲰 􏲹 a
I􏲭􏲬􏲪de􏲪(􏲱.􏲮ef􏲱); S􏲯􏲫􏲱e􏲴.􏲬􏲰􏲱.􏲳􏲪i􏲭􏲱􏲮􏲭(“ “ + 􏲱.da􏲱a);
I􏲭􏲬􏲪de􏲪(􏲱.􏲪igh􏲱);􏲸
outpu􏲱: c b 􏲫 d a 􏲹 􏲰
4

5 Convert a recursive algorithm to an iterative version
public void Inorder(treenode t)􏲷 // recursive version if(t != null)􏲷
Inorder(t.left); S􏲯stem.out.println(“ “ + t.data); Inorder(t.right);􏲸
􏲸
For iterative version, use a stack; each stack frame has two fields: (node, action)
public void Inorder (treenode root) 􏲷//iterative version stack.push(root, ‘inorder’);
while (stack.IsEmpt􏲯()!=null) 􏲷
􏲸 􏲸
5
(c,action) = stack.pop();
if (action == ‘visit’) visit(c )
else 􏲷if(c.right != NULL) stack.push(c.right, ‘inorder’); stack.push(c, ‘visit’)
if(c.left != NULL) stack.push(c.left, `inorder’); 􏲸

Q􏲰e􏲫􏲱i􏲬􏲭: 􏲹ha􏲱 ki􏲭d 􏲬f da􏲱a 􏲫􏲱􏲪􏲰c􏲱􏲰􏲪e􏲫 i􏲫 􏲭eeded he􏲪e, 􏲺􏲰e􏲰e􏲫 􏲬􏲪 􏲫􏲱ack􏲫?
A􏲮g􏲬􏲪i􏲱h􏲴:
c􏲰􏲪􏲪e􏲭􏲱 = 􏲪􏲬􏲬􏲱 􏲹hi􏲮e(c􏲰􏲪􏲪e􏲭􏲱)
S􏲯􏲫􏲱e􏲴.􏲬􏲰􏲱.􏲳􏲪i􏲭􏲱(c􏲰􏲪􏲪e􏲭􏲱.da􏲱a) if(c􏲰􏲪􏲪e􏲭􏲱.􏲮ef􏲱) E􏲭􏲺(c􏲰􏲪􏲪e􏲭􏲱.􏲮ef􏲱) if(c􏲰􏲪􏲪e􏲭􏲱.􏲪igh􏲱)E􏲭􏲺(c􏲰􏲪􏲪e􏲭􏲱.􏲪igh􏲱) c􏲰􏲪􏲪e􏲭􏲱 = De􏲺(􏲺􏲰e􏲰e)
6 Level order (or breadth-first order) traversal T􏲪a􏲵e􏲪􏲫e 􏲱he 􏲱􏲪ee 􏲮e􏲵e􏲮 b􏲯 􏲮e􏲵e􏲮, each 􏲮e􏲵e􏲮 f􏲪􏲬􏲴 􏲮ef􏲱 􏲱􏲬 􏲪igh􏲱
a
b􏲹
cd􏲰 􏲫
Output: a b 􏲹 c d 􏲰 􏲫 6

7 Binar􏲯 Search 􏲱ree􏲫
1) A node’s ke􏲯 is larger or equal than that of an􏲯 node in its left subtree, and
smaller than that of an􏲯 node in its right subtree.
10
7 23
9 15 32
2) Opera􏲱ion􏲫:
Search, Insert, Delete, Min, Max Also: Succesor, Predecessor
23
19 33
15 25 21 43
7

8 A􏲮g􏲬􏲪i􏲱h􏲴 Fi􏲭d
public find (int x, treenode T) 􏲷
if (!T) return ‘NOT FOUND’ message;
if ( T.data == x) return x;
if ( X > T.data) return find (x, T.rightchild)
else return find (x, T.leftchild) 􏲸
17
26
11
27
find(27,t)
1) Since x > 17, search ( 27, ->26)
2) Since x > 26, search (27, ->31) 3) Since x < 31, search (27, ->27) 4)Since x = 27, return 27
8 4
31
35
8

9 Insertions in Java
􏲳􏲰b􏲮ic 􏲱􏲪ee􏲭􏲬de i􏲭􏲫e􏲪􏲱(􏲱􏲪ee􏲭􏲬de 􏲱, i􏲭􏲱 k)􏲷 if (􏲱 == 􏲭􏲰􏲮􏲮)
􏲷􏲱 = 􏲭e􏲹 􏲱􏲪ee􏲭􏲬de(); 􏲱.da􏲱a = k; 􏲱.􏲮ef􏲱=􏲭􏲰􏲮􏲮; 􏲱.􏲪igh􏲱=􏲭􏲰􏲮􏲮; 􏲪e􏲱􏲰􏲪􏲭 􏲱;􏲸
e􏲮􏲫e
if( k > 􏲱.da􏲱a)
􏲸
􏲷 􏲱.􏲪igh􏲱 = i􏲭􏲫e􏲪􏲱(􏲱.􏲪igh􏲱,k);
􏲪e􏲱􏲰􏲪􏲭 􏲱;􏲸 e􏲮􏲫e􏲷􏲱.􏲮ef􏲱 = i􏲭􏲫e􏲪􏲱(􏲱.􏲮ef􏲱, k);
􏲪e􏲱􏲰􏲪􏲭 􏲱;􏲸
9

10 L􏲬􏲬k f􏲬􏲪 􏲱he 􏲫􏲴a􏲮􏲮e􏲫􏲱 􏲭􏲰􏲴be􏲪 i􏲭 􏲱he BST
public treenode min ( treenode t ) 􏲷 if ( t == null ) return null;
􏲸
else
if ( t. left == null ) return t; else return min ( t.left );
10

Tree dele􏲱ion opera􏲱ion􏲫:
Three cases:
(1) The node is a leaf node
(2) The node has one child (3) The node has two children
11

12 delete operation (cont.)
(1) The 􏲭􏲬de i􏲫 a 􏲮eaf 􏲭􏲬de: j􏲰􏲫􏲱 de􏲮e􏲱e i􏲱
20
20
10 30
3 13 (2) The 􏲭􏲬de ha􏲫 􏲬􏲭e chi􏲮d: c􏲬􏲭􏲭ec􏲱 i􏲱􏲫 chi􏲮d 􏲱􏲬 i􏲱􏲫 􏲳a􏲪e􏲭􏲱
10
3 13 35
30
20
10
3 13 35
20
10 35
3 13
30
12

13 The node has two children
Algorithm:
– Replace the value of the node with the smallest value of its right subtree
– Delete the node with the smallest value in the right subtree (has at most one child)
20
20
20
10
16 35
40
10
16 35
46
10
46
3
56
3
56
3
16 35 56
49 58
c􏲬􏲭􏲭ec􏲱 i􏲱􏲫 chi􏲮d 􏲹i􏲱h i􏲱􏲫 􏲳a􏲪e􏲭􏲱
13
46 58
40 58
49
49 􏲪e􏲳􏲮ace i􏲱􏲫 􏲵a􏲮􏲰e 􏲹i􏲱h 􏲱he 􏲫􏲴a􏲮􏲮e􏲫􏲱

Comple􏲶it􏲯 for BST
– heigh􏲱 􏲬f a 􏲱􏲪ee: 􏲭􏲰􏲴be􏲪 􏲬f edge􏲫 􏲬􏲭 a 􏲮􏲬􏲭ge􏲫􏲱 􏲳a􏲱h 􏲪􏲬􏲬􏲱-> 􏲮ea􏲵e􏲫 – Fi􏲭d, I􏲭􏲫e􏲪􏲱, De􏲮e􏲱e 􏲲 O(h), 􏲹he􏲪e h i􏲫 􏲱he heigh􏲱 􏲬f 􏲱he 􏲱􏲪ee.
– h ca􏲭 􏲵a􏲪􏲯 a 􏲮􏲬􏲱 (a􏲫 a f􏲰􏲭c􏲱i􏲬􏲭 􏲬f 􏲭 = 􏲭􏲰􏲴be􏲪 􏲬f 􏲭􏲬de􏲫), de􏲳e􏲭di􏲭g 􏲬􏲭 h􏲬􏲹 􏲹e􏲮􏲮 ba􏲮a􏲭ced 􏲱he BST i􏲫.
– h ca􏲭 be O(􏲮􏲬g 􏲭) : 􏲹e 􏲫a􏲯 􏲱ha􏲱 􏲱he 􏲱􏲪ee i􏲫 balanced
– h ca􏲭 be O(􏲭)
Wha􏲱 i􏲫 􏲱he 􏲪e􏲮a􏲱i􏲬􏲭 be􏲱􏲹ee􏲭 h a􏲭d 􏲭?
h +1 􏲻 􏲭 􏲻 2h+1 – 1
The 􏲮ef􏲱 i􏲭e􏲺􏲰a􏲮i􏲱􏲯: 􏲬b􏲵i􏲬􏲰􏲫 (􏲹h􏲯?)
Righ􏲱 i􏲭e􏲺􏲰a􏲮i􏲱􏲯: P􏲪􏲬􏲬f b􏲯 i􏲭d􏲰c􏲱i􏲬􏲭 􏲬􏲭 􏲭.
We 􏲹i􏲮􏲮 􏲫ee 􏲮a􏲱e􏲪 h􏲬􏲹 􏲱􏲬 􏲴ai􏲭􏲱ai􏲭 􏲱he 􏲱􏲪ee ba􏲮a􏲭ced 􏲹hi􏲮e d􏲬i􏲭g 􏲱he 􏲱􏲪ee 􏲬􏲳e􏲪a􏲱i􏲬􏲭􏲫.
14

Search problems: 􏲬􏲭e 􏲬f 􏲱he 􏲴􏲬􏲫􏲱 c􏲬􏲴􏲴􏲬􏲭 a􏲳􏲳􏲮ica􏲱i􏲬􏲭􏲫 􏲬f c􏲬􏲴􏲳􏲰􏲱e􏲪􏲫. If i􏲭f􏲬􏲪􏲴a􏲱i􏲬􏲭 i􏲫 􏲫􏲱a􏲱ic, 􏲱he􏲭 􏲰􏲫e bi􏲭a􏲪􏲯 􏲫ea􏲪ch 􏲲 􏲹􏲬􏲪k􏲫 i􏲭 O( 􏲮􏲬g 􏲭) 􏲱i􏲴e.
N􏲬􏲱e 􏲱ha􏲱 i􏲭􏲫e􏲪􏲱 a􏲭d de􏲮e􏲱e 􏲹􏲬􏲰􏲮d be 􏲫􏲮􏲬􏲹 (O(􏲭))
If i􏲭f􏲬􏲪􏲴a􏲱i􏲬􏲭 i􏲫 d􏲯namic:
􏲼 􏲹e 􏲹a􏲭􏲱 􏲱􏲬 i􏲴􏲳􏲮e􏲴e􏲭􏲱 search, insert, a􏲭d delete, idea􏲮􏲮􏲯 a􏲮􏲮 i􏲭
O( 􏲮􏲬g 􏲭) 􏲱i􏲴e.
􏲼 a􏲳􏲳􏲪􏲬ach: 􏲰􏲫e bi􏲭a􏲪􏲯 􏲫ea􏲪ch 􏲱􏲪ee􏲫; 􏲹e ha􏲵e 􏲫ee􏲭 􏲱ha􏲱 􏲫ea􏲪ch, i􏲭􏲫e􏲪􏲱, a􏲭d
de􏲮e􏲱e a􏲪e d􏲬􏲭e i􏲭 O(h) 􏲱i􏲴e, 􏲹he􏲪e h i􏲫 􏲱he 􏲱􏲪ee’􏲫 heigh􏲱.
􏲼 b􏲰􏲱 􏲱he 􏲱􏲪ee ca􏲭 g􏲪􏲬􏲹 i􏲴ba􏲮a􏲭ced a􏲭d h ca􏲭 be O􏲴ega(􏲭).
􏲼 The􏲪e a􏲪e 􏲵a􏲪ia􏲭􏲱􏲫 􏲬f bi􏲭a􏲪􏲯 􏲫ea􏲪ch 􏲱􏲪ee 􏲱ha􏲱 d􏲬 􏲭􏲬􏲱 bec􏲬􏲴e 􏲰􏲭ba􏲮a􏲭ced: AVL trees (􏲫􏲱􏲬􏲪e i􏲭 e􏲵e􏲪􏲯 􏲭􏲬de 􏲱he heigh􏲱 diffe􏲪e􏲭ce be􏲱􏲹ee􏲭 􏲱he 􏲪igh􏲱 􏲫􏲰b􏲱􏲪ee a􏲭d 􏲮ef􏲱 􏲫􏲰b􏲱􏲪ee), red-black trees (􏲫􏲱􏲬􏲪e i􏲭 e􏲵e􏲪􏲯 􏲭􏲬de 􏲱he c􏲬􏲮􏲬􏲪 􏲪ed 􏲬􏲪 b􏲮ack), spla􏲯 trees (􏲭􏲬 e􏲶􏲱􏲪a 􏲫􏲱􏲬􏲪age 􏲭eeded, b􏲰􏲱 􏲬􏲳e􏲪a􏲱i􏲬􏲭􏲫 a􏲪e i􏲭 O( 􏲮􏲬g 􏲭) i􏲭 􏲱he a􏲴􏲬􏲪􏲱i􏲽ed 􏲫e􏲭􏲫e (􏲭􏲬􏲱 􏲱he 􏲹􏲬􏲪􏲫􏲱-ca􏲫e 􏲫e􏲭􏲫e).
15

Relations between n and h in a binar􏲯 tree
16

Red-Black Trees
Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood


ROTATIONS
I
8 It
A ,
1, Lefty g8\
¥¥taeH ATLEAST
¥ Mode
÷:
4 t’s
left –
rotate Cx ) }
Its ” iii.

DEFINITIONS
6

Red-Black Trees
! Definition: A red-black tree is a binary search tree in which:
” Every node is colored either Red or Black.
” Each NULL pointer is considered to be a Black 􏲾node􏲿.

” If a node is Red, then both of its children are Black.
” Every path from a node to a NULL contains the same
number of Black nodes.
” By convention, the root is Black
! Definition: The black-height of a node X in a red-black tree is the number of Black nodes on any path to a NULL, not counting X.
– RED
sequence
n o RED
7

X
A Red-Black Tree with NULLs shown
Black-Height of the tree (the root) = 3 Black-Height of node 􏲾X􏲿 = 2
8

A Red-Black Tree with Black-Height = 3
9

X
Black Height of the tree? Black Height of X?
10

Theorem 1 – Any red-black tree with root x, has n ≥ 2bh(x) – 1 nodes, where bh(x) is the black height of node x.
Proof: by induction on height of x.
I I-
h=D .
‘ “””””
4 bhcx1=0,n=L, I>20-1TRUE
4
.
n’ Iiizititrlti
23h41 , + 2bhcxl
=



zbhcxt- I ,
>
+ –
y
I
11

Theorem 2 – In a red-black tree, at least half the nodes on any path from the root to a NULL must be Black.
Proof – If there is a Red node on the path, there must be a corresponding Black node.
Algebraically this theorem means bh( x ) ≥ h/2
12

Theorem 3 – In a red-black tree, no path from any node, X, to a NULL is more than twice as long as any other path from X to any other NULL.
Proof: By definition, every path from a node to any NULL contains the same number of Black nodes. By Theorem 2, a least 1⁄2 the nodes on any such path are Black. Therefore, there can no more than twice as many nodes on any path from X to a NULL as on any other path. Therefore the length of every path is no more than twice as long as any other path.
13

Theorem 4 –
A red-black tree with n nodes has height
h ≤ 2 lg(n + 1).
Let h be the height of the red-black tree with
root x. By Theorem 2, bh(x) ≥ h/2
From Theorem 1, n ≥ 2bh(x) – 1 Therefore n ≥ 2 h/2 – 1
n + 1 ≥ 2h/2 lg(n +1)≥h/2 2lg(n + 1) ≥ h
Proof:
14

BOTTOM-UP INSERTION
15

Bottom –Up Insertion
! Insert node as usual in BST
! Color the node Red
! What Red-Black property may be violated?
” EverynodeisRedorBlack?
” NULLsareBlack?
” IfnodeisRed,bothchildrenmustbeBlack?
” EverypathfromnodetodescendantNULLmust contain the same number of Blacks?
16

Bottom Up Insertion
! Insert node; Color it Red; X is pointer to it
! Cases
0: X is the root — color it Black
1: Both parent and uncle are Red — color parent and uncle Black, color grandparent Red. Point X to grandparent and check new situation.
2 (zig-zag): Parent is Red, but uncle is Black. X and its parent are opposite type children — color grandparent Red, color X Black, rotate left(right) on parent, rotate right(left) on grandparent
3 (zig-zig): Parent is Red, but uncle is Black. X and its parent are both left (right) children — color parent Black, color grandparent Red, rotate right(left) on grandparent
17

G P
X
U
P
X G
U
Case 1 – U is Red
Just Recolor and move up
18

G P
X
P
S
U
S
Case 2 – Zig-Zag
X
G
U
Double Rotate
X around P; X around G
Recolor G and X
19

G
P
S
U
X
P X
Case 3 – Zig-Zig
Single Rotate P around G Recolor P and G
S
G
U
20

Asymptotic Cost of Insertion
! O(lg n) to descend to insertion point
! O(1) to do insertion
! O(lg n) to ascend and readjust == worst case only for case 1
! Total: O(lg n)
21

Insert 4 into this R-B Tree
1
2
11
14
15 8
Red node
7
5
Black node
22

Insertion Practice
Insert the values 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty Red-Black Tree
23

Top-Down Insertion
An alternative to this 􏲾bottom-up􏲿 insertion is 􏲾top-down􏲿 insertion.
Top-down is iterative. It moves down the tree, 􏲾fixing􏲿 things as it goes.
What is the objective of top-down􏳀s 􏲾fixes􏲿?
24

BOTTOM-UP DELETION
25

Recall 􏲾ordinary􏲿 BST Delete
1. If node to be deleted is a leaf, just delete it.
2. If node to be deleted has just one child, replace it with that child (splice)
3. If node to be deleted has two children, replace the value in the node by its in- order predecessor/successor􏳀s value then delete the in-order predecessor/successor (a recursive step)
26

Bottom-Up Deletion
1. Do ordinary BST deletion. Eventually a 􏲾case 1􏲿 or 􏲾case 2􏲿 deletion will be done (leaf or just one child).
— If deleted node, U, is a leaf, think of deletion as replacing U with the NULL pointer, V.
— If U had one child, V, think of deletion as replacing U with V.
2. What can go wrong??
27

Which RB Property may be violated after deletion?
1. If U is Red?
Not a problem – no RB properties violated
2. If U is Black?
If U is not the root, deleting it will change the black-height along some path
28

Fixing the problem
! Think of V as having an 􏲾extra􏲿 unit of blackness. This extra blackness must be absorbed into the tree (by a red node), or propagated up to the root and out of the tree.
! There are four cases – our examples and 􏲾rules􏲿 assume that V is a left child. There are symmetric cases for V as a right child.
29

Terminology
! The node just deleted was U
! The node that replaces it is V, which has
an extra unit of blackness
! The parent of V is P
! The sibling of V is S
Black Node Red or Black and don􏳀t care Red Node
30

Bottom-Up Deletion Case 1
! V􏳀s sibling, S, is Red
” RotateSaroundPandrecolorS&P
! NOT a terminal case – One of the other cases will now apply
! All other cases apply when S is Black
31

Case 1 Diagram
P
S
S P
Rotate S around P
V+
P V+
V+ S
Recolor S & P
32

Bottom-Up Deletion Case 2
! V􏳀s sibling, S, is Black and has two Black children.
” RecolorStobeRed
” PabsorbsV􏳀sextrablackness
! If P is Red, we􏳀re done (it absorbed the blackness)
! If P is Black, it now has extra blackness and problem has been propagated up the tree
33

Case 2 diagram
Recolor S
P absorbs blackness
P+ S
P
SV
V+
Either extra Black absorbed by P or
P now has extra blackness
34

Bottom-Up Deletion Case 3
! S is Black
! S􏳀s right child is RED (Left child either color)
” RotateSaroundP
” Swap colors of S and P,
and color S􏳀s right child Black
! This is the terminal case – we􏳀re done
35

Case 3 diagrams
P
S
S P
V+ S
Swap colors of S & P Color S􏳀s right child Black
Rotate S around P
V+
P V
36

Bottom-Up Deletion Case 4
! S is Black, S􏳀s right child is Black and S􏳀s left child is Red
” Rotate S􏳀s left child around S
” SwapcolorofSandS􏳀sleftchild ” Nowincase3
37

Case 4 Diagrams
P
S
Rotate S􏳀s
left around S V+
P
V+
P
V+
S
S
Swap colors of S and S􏳀s original left child
38

Top-Down Deletion
An alternative to the recursive 􏲾bottom-up􏲿 deletion is 􏲾top-down􏲿 deletion.
This method is iterative. It moves down the tree only, 􏲾fixing􏲿 things as it goes.
What is the goal of top-down deletion?
39

65
50
80
10 60 70 90
62
Perform the following deletions, in the order specified Delete 90, Delete 80, Delete 70
40

18 B trees (Chapter 18 in the te􏲶tbook)
Bi􏲭a􏲪􏲯 􏲫ea􏲪ch 􏲱􏲪ee􏲫 a􏲪e 􏲭􏲬􏲱 a􏲳􏲳􏲪􏲬􏲳􏲪ia􏲱e f􏲬􏲪 da􏲱a 􏲫􏲱􏲬􏲪ed 􏲬􏲭 e􏲶􏲱e􏲪􏲭a􏲮 􏲴e􏲴􏲬􏲪􏲯: each 􏲵e􏲪􏲱ica􏲮 􏲴􏲬􏲵e i􏲭 􏲱he
􏲱􏲪ee i􏲭􏲵􏲬􏲮􏲵e􏲫 a 􏲴echa􏲭ica􏲮 􏲴􏲬􏲵e􏲴e􏲭􏲱 􏲬f 􏲱he di􏲫k head.
Me􏲴􏲬􏲪􏲯 􏲬􏲳e􏲪a􏲱i􏲬􏲭: 􏲬􏲭 500 MIPS 􏲴achi􏲭e, 􏲱he􏲪e a􏲪e 500 􏲴i􏲮􏲮i􏲬􏲭􏲫 􏲬f i􏲭􏲫􏲱􏲪􏲰c􏲱i􏲬􏲭􏲫 􏲳e􏲪 􏲫ec􏲬􏲭d Di􏲫k: 3600 􏲪􏲬􏲱/􏲴i􏲭 􏳁 1 􏲪􏲬􏲱a􏲱i􏲬􏲭 i􏲭 1/60 􏲫ec 􏳂= 16.7 􏲴􏲫.
􏲬􏲭 a􏲵e􏲪age 􏲹e d􏲬 ha􏲮f a 􏲫􏲳i􏲭􏳁8.3 􏲴􏲫
S􏲬 􏳂=120 􏲬􏲳. 􏲳e􏲪 􏲫ec􏲬􏲭d
S􏲬: 􏲱i􏲴e f􏲬􏲪 􏲬􏲭e di􏲫k acce􏲫􏲫 􏳂= 􏲱i􏲴e f􏲬􏲪 4 * 106 􏲴e􏲴􏲬􏲪􏲯 􏲬􏲳e􏲪a􏲱i􏲬􏲭􏲫
Idea: 􏲰􏲫e ‘􏲴􏲰􏲮􏲱i􏲹a􏲯 􏲱􏲪ee􏲫’ i􏲭􏲫􏲱ead 􏲬f bi􏲭a􏲪􏲯 􏲱􏲪ee􏲫; a􏲱 each 􏲭􏲬de 􏲹e 􏲭eed 􏲱􏲬 ch􏲬􏲬􏲫e 􏲱􏲬 c􏲬􏲭􏲱i􏲭􏲰e a􏲴􏲬􏲭g 􏲴􏲬􏲪e 􏲱ha􏲭 􏲱􏲹􏲬 chi􏲮d􏲪e􏲭.
N􏲬de􏲫 a􏲪e fa􏲱􏲱e􏲪, b􏲰􏲱 􏲱􏲪ee􏲫 a􏲪e 􏲫ha􏲮􏲮􏲬􏲹e􏲪.
M􏲬􏲪e 􏲹􏲬􏲪k a􏲱 a 􏲭􏲬de (􏲱hi􏲫 i􏲫 i􏲭 i􏲭􏲱e􏲪􏲭a􏲮 􏲴e􏲴􏲬􏲪􏲯), b􏲰􏲱 fe􏲹e􏲪 􏲭􏲬de􏲫 a􏲪e 􏲵i􏲫i􏲱ed (each 􏲭e􏲹 􏲵i􏲫i􏲱ed 􏲭􏲬de 􏲱􏲯􏲳ica􏲮􏲮􏲯 i􏲴􏲳􏲮ie􏲫 a diffe􏲪e􏲭􏲱 di􏲫k acce􏲫􏲫).
Thi􏲫 idea i􏲫 i􏲴􏲳􏲮e􏲴e􏲭􏲱ed 􏲵ia B-􏲱􏲪ee􏲫.
The􏲪e a􏲪e 􏲴􏲬􏲪e 􏲵a􏲪ia􏲭􏲱􏲫 􏲬f B-􏲱􏲪ee􏲫. We di􏲫c􏲰􏲫􏲫 􏲬􏲭e 􏲬f 􏲱he􏲴: B+ – 􏲱􏲪ee
18

Definition of B trees of order m:
􏲱he 􏲪􏲬􏲬􏲱 i􏲫 ei􏲱he􏲪 a 􏲮eaf 􏲬􏲪 ha􏲫 be􏲱􏲹ee􏲭 2 a􏲭d 􏲴 chi􏲮d􏲪e􏲭
A􏲭􏲯 􏲭􏲬􏲭-􏲮eaf 􏲭􏲬de (e􏲶ce􏲳􏲱 􏲱he 􏲪􏲬􏲬􏲱) ha􏲫 be􏲱􏲹ee􏲭 􏳃􏲴/2􏳄 a􏲭d 􏲴 chi􏲮d􏲪e􏲭 if i􏲱 ha􏲫 j chi􏲮d􏲪e􏲭, i􏲱 c􏲬􏲭􏲱ai􏲭􏲫 j-1 ke􏲯 􏲵a􏲮􏲰e􏲫 (􏲱􏲬 g􏲰ide 􏲱he 􏲫ea􏲪ch)
A􏲮􏲮 􏲮ea􏲵e􏲫 a􏲪e a􏲱 􏲱he 􏲫a􏲴e 􏲮e􏲵e􏲮, each 􏲮eaf c􏲬􏲭􏲱ai􏲭􏲫 be􏲱􏲹ee􏲭 􏳃􏲴/2􏳄 a􏲭d 􏲴 ac􏲱􏲰a􏲮 􏲵a􏲮􏲰e􏲫 􏲫􏲱􏲬􏲪ed i􏲭 a 􏲫􏲬􏲪􏲱ed 􏲴-a􏲪􏲪a􏲯 (􏲫􏲬􏲴e 􏲫􏲮􏲬􏲱􏲫 i􏲭 􏲱he a􏲪􏲪a􏲯 􏲴a􏲯 be e􏲴􏲳􏲱􏲯)
19

A B-􏲱􏲪ee 􏲬f 􏲬􏲪de􏲪 􏲴 􏲹i􏲱h 􏲭 􏲵a􏲮􏲰e􏲫 ha􏲫 heigh􏲱 h 􏳅 􏲮􏲬g(􏲭/2)/􏲮􏲬g(􏲴/2).
Sea􏲪ch: Ob􏲵i􏲬􏲰􏲫 (j􏲰􏲫􏲱 f􏲬􏲮􏲮􏲬􏲹 􏲱he g􏲰idi􏲭g ke􏲯􏲫).
I􏲭􏲫e􏲪􏲱 a􏲭d De􏲮e􏲱e: 􏲱he􏲪e a􏲪e 􏲴a􏲭􏲯 ca􏲫e􏲫. I’􏲮􏲮 j􏲰􏲫􏲱 i􏲮􏲮􏲰􏲫􏲱􏲪a􏲱e b􏲯 e􏲶a􏲴􏲳􏲮e􏲫. A􏲮􏲮 􏲱he 􏲬􏲳e􏲪a􏲱i􏲬􏲭􏲫 􏲹􏲬􏲪k i􏲭 O(h).
20

I􏲭􏲫e􏲪􏲱 a􏲭d De􏲮e􏲱e i􏲭 a B-􏲱􏲪ee
21

22

23

Wi􏲱h 99 de􏲮e􏲱ed 􏲱he 􏲭􏲰􏲴be􏲪 􏲬f 􏲵a􏲮􏲰e􏲫 i􏲭 􏲱he 􏲮eaf ge􏲱􏲫 be􏲮􏲬􏲹 􏲴i􏲭.; 􏲫􏲬 􏲱􏲹􏲬 􏲮ea􏲵e􏲫 a􏲪e 􏲴e􏲪ged; 􏲭􏲬􏲹 􏲱he 􏲳a􏲪e􏲭􏲱 ha􏲫 chi􏲮d􏲪e􏲭 be􏲮􏲬􏲹 􏲱he 􏲴i􏲭; 􏲱he􏲭 a chi􏲮d i􏲫 ad􏲬􏲳􏲱ed f􏲪􏲬􏲴 􏲱he 􏲮ef􏲱 􏲫ib􏲮i􏲭g
24

25 Heaps (Chapter 6 in the te􏲶tbook)
Main applica􏲱ion: priorit􏲯 queues
􏲲The value of each node is less than that of its children. (MIN-HEAP)
􏲲A heap is a complete binar􏲯 tree except the bottom level which is adjusted to the left and has no “holes”
􏲲Height of the tree is 􏳆 􏲮􏲬g 􏲭􏳇, where n is the number of values (proof on board)
10
9
heap
20 10
4
Not a heap
Not a
16 heap 10
20 60 5013100
21
24 19 17
31 45
25
25

26 Heap Implementation
We ca􏲭 􏲰􏲫e a􏲭 a􏲪􏲪a􏲯 (d􏲰e 􏲱􏲬 􏲱he 􏲪eg􏲰􏲮a􏲪 􏲫􏲱􏲪􏲰c􏲱􏲰􏲪e
􏲬f 􏲱he bi􏲭a􏲪􏲯 􏲱􏲪ee):
123456789
A
B
C
D
E
F
G
H
I
Lef􏲱 child of i: 2 * i. Righ􏲱 child of i: 2 * i + 1 Paren􏲱 of i: [i/2]
A BC
G
26
DEF HI

27 Heap operations
1) I􏲭􏲫e􏲪􏲱i􏲬􏲭
2) De􏲮e􏲱eMi􏲭
3) B􏲰i􏲮dHea􏲳 (􏲬􏲪ga􏲭i􏲽e a􏲭 a􏲪bi􏲱􏲪a􏲪􏲯 a􏲪􏲪a􏲯 a􏲫 a hea􏲳)
(1)
Insertion:
find the left-most open position and insert the new element NewElem while ( NewElem < its parent ) e􏲶change them. 54 10 4 10 5 20 40 9 20 40 9 5 9 10 20 40 4 Q􏲰e􏲫􏲱i􏲬􏲭: I􏲫 i􏲱 􏲳􏲬􏲫􏲫ib􏲮e 􏲱ha􏲱 􏲫􏲬􏲴e e􏲮e􏲴e􏲭􏲱􏲫 a􏲱 􏲱he 􏲫a􏲴e 􏲮e􏲵e􏲮 b􏲰􏲱 diffe􏲪e􏲭􏲱 b􏲪a􏲭ch 􏲴a􏲯 be 􏲫􏲴a􏲮􏲮e􏲪 􏲱ha􏲭 􏲱he 􏲭e􏲹􏲮􏲯 􏲴􏲬􏲵ed 􏲰􏲳 􏲵a􏲮􏲰e? 27 28 De􏲮e􏲱e Mi􏲭 - Re􏲳􏲮ace 􏲱he 􏲭􏲬de b􏲯 􏲱he 􏲪igh􏲱-􏲴􏲬􏲫􏲱 􏲭􏲬de a􏲱 􏲱he b􏲬􏲱􏲱􏲬􏲴 􏲮e􏲵e􏲮 - Whi􏲮e ( 􏲭e􏲹 􏲪􏲬􏲬􏲱 > i􏲱􏲫 chi􏲮d􏲪e􏲭 )
E􏲶cha􏲭ge i􏲱 􏲹i􏲱h 􏲱he 􏲫􏲴a􏲮􏲮e􏲪 􏲬f i􏲱􏲫 chi􏲮d􏲪e􏲭
The comple􏲶it􏲯 anal􏲯sis: de􏲮e􏲱i􏲬􏲭 & i􏲭􏲫e􏲪􏲱i􏲬􏲭 􏲲 O ( 􏲮􏲬g 􏲭 )
Rea􏲫􏲬􏲭􏲫:
1) 􏲭􏲬 􏲬f 􏲬􏲳e􏲪a􏲱i􏲬􏲭􏲫 = O( heigh􏲱 􏲬f 􏲱he hea􏲳) 2) Heigh􏲱 􏲬f 􏲱he hea􏲳: O ( 􏲮􏲬g 􏲭 )
10
13
21 40 20
28
5 21
10 13 10 13 21
10
13 18
18 4020 21 18 4020
18 40 20

B􏲰i􏲮dHea􏲳 􏲬􏲳e􏲪a􏲱i􏲬􏲭
B􏲰i􏲮d a hea􏲳 f􏲪􏲬􏲴 a􏲭 a􏲪bi􏲱􏲪a􏲪􏲯 a􏲪􏲪a􏲯
B􏲬􏲱􏲱􏲬􏲴 􏲲􏲰􏲳 􏲳􏲪􏲬ced􏲰􏲪e: 􏲫􏲱a􏲪􏲱 􏲹i􏲱h 􏲫􏲰b􏲱􏲪ee􏲫 ha􏲵i􏲭g 􏲪􏲬􏲬􏲱􏲫 a􏲱 􏲮e􏲵e􏲮 h-1 a􏲭d 􏲴ake 􏲱he􏲴 hea􏲳􏲫; 􏲱he􏲭 􏲴􏲬􏲵e 􏲱􏲬 􏲫􏲰b􏲱􏲪ee􏲫 􏲹i􏲱h 􏲪􏲬􏲬􏲱􏲫 a􏲱 􏲮e􏲵e􏲮 h-2 a􏲭d 􏲴ake 􏲱he􏲴 hea􏲳􏲫, a􏲭d 􏲫􏲬 􏲬􏲭 􏲱i􏲮􏲮 􏲮e􏲵e􏲮 0 i􏲫 􏲪eached.
29

30

31

32

􏲼 Ti􏲴e c􏲬􏲴􏲳􏲮e􏲶i􏲱􏲯: 1*2h-1 + 2* 2h-2 + 3*2h-3 + 􏳈. + h 20 < 2 * 2h 􏲼 Si􏲭ce h = 􏳆 􏲮􏲬g 􏲭􏳇, 􏲱i􏲴e c􏲬􏲴􏲳􏲮e􏲶i􏲱􏲯 i􏲫 O(􏲭). 33 34 Heap Questions (1) D􏲬 􏲯􏲬􏲰 􏲭eed 􏲱􏲬 􏲫􏲳ecif􏲯 􏲹hich e􏲮e􏲴e􏲭􏲱 􏲬f a hea􏲳 􏲱􏲬 be de􏲮e􏲱ed? (2) Wha􏲱 i􏲫 􏲱he diffe􏲪e􏲭ce be􏲱􏲹ee􏲭 a bi􏲭a􏲪􏲯 􏲫ea􏲪ch 􏲱􏲪ee a􏲭d a hea􏲳? 34 Hea􏲳􏲫- I􏲭􏲫e􏲪􏲱 (􏲳􏲫e􏲰d􏲬c􏲬de) 􏲴i􏲭Hea􏲳I􏲭􏲫e􏲪􏲱(A, ke􏲯) 􏲷 A.hea􏲳_􏲫i􏲽e = A.hea􏲳_􏲫i􏲽e+1; A[A.hea􏲳_􏲫i􏲽e] = ke􏲯; i=A.hea􏲳_􏲫i􏲽e; 􏲹hi􏲮e (i > 1 a􏲭d A[􏲳a􏲪e􏲭􏲱[i]] > A[i]) 􏲷
􏲫􏲹a􏲳(A[i],A[􏲳a􏲪e􏲭􏲱(i)]);
i = 􏲳a􏲪e􏲭􏲱(i); 􏲸
􏲸
35

HEAPIFY a􏲭 e􏲮e􏲴e􏲭􏲱 (􏲴􏲬􏲵e i􏲱 d􏲬􏲹􏲭 i􏲭 􏲱he hea􏲳)
􏲴i􏲭Hea􏲳if􏲯(A,i) 􏲷 //􏲴􏲬􏲵e-d􏲬􏲹􏲭 A[i] l = 􏲮ef􏲱(i); 􏲪= 􏲪igh􏲱(i);
if(l <=A. hea􏲳_􏲫i􏲽e a􏲭d A[l] < A[i]) 􏲫􏲴a􏲮􏲮e􏲫􏲱 = l; e􏲮􏲫e 􏲫􏲴a􏲮􏲮e􏲫􏲱 = i; if(􏲪 <=A. hea􏲳_􏲫i􏲽e a􏲭d A[􏲪] < A[􏲫􏲴a􏲮􏲮e􏲫􏲱]) 􏲫􏲴a􏲮􏲮e􏲫􏲱 = 􏲪; if(􏲫􏲴a􏲮􏲮e􏲫􏲱 != i)􏲷 􏲫􏲹a􏲳(A[i], A[􏲫􏲴a􏲮􏲮e􏲫􏲱]); 􏲴i􏲭Hea􏲳if􏲯(A, 􏲫􏲴a􏲮􏲮e􏲫􏲱) 􏲸 36 DELETE i􏲭 a hea􏲳 hea􏲳E􏲶􏲱􏲪ac􏲱(A) 􏲷 if(A.hea􏲳_􏲫i􏲽e) < 1) e􏲪􏲪􏲬􏲪 “hea􏲳 i􏲫 e􏲴􏲳􏲱􏲯” e􏲮􏲫e􏲷 􏲴i􏲭= A[1]; A[1]=A[A.hea􏲳_􏲫i􏲽e]; //􏲴􏲬􏲵e 􏲮a􏲫􏲱 e􏲮e􏲴e􏲭􏲱 i􏲭 􏲱he 􏲪􏲬􏲬􏲱 A.Hea􏲳_􏲫i􏲽e --; 􏲴i􏲭Hea􏲳if􏲯(A,1); // 􏲴􏲬􏲵e d􏲬􏲹􏲭 􏲱he 􏲪􏲬􏲬􏲱 􏲪e􏲱􏲰􏲪􏲭 􏲴i􏲭; 􏲸 􏲸 37 BUILD HEAP - 􏲳􏲫e􏲰d􏲬c􏲬de b􏲰i􏲮dMi􏲭Hea􏲳(A) 􏲷 // A i􏲫 i􏲭i􏲱ia􏲮􏲮􏲯 a􏲭 a􏲪bi􏲱􏲪a􏲪􏲯 a􏲪􏲪a􏲯 // 􏲳􏲪􏲬ced􏲰􏲪e c􏲬􏲭􏲵e􏲪􏲱􏲫 i􏲱 i􏲭􏲱􏲬 a hea􏲳 i = A.hea􏲳_􏲫i􏲽e/2; 􏲹hi􏲮e(i > 0) 􏲷
􏲴i􏲭Hea􏲳if􏲯(A,i);
i–; 􏲸
􏲸
38

Hea􏲳 A􏲳􏲳􏲮ica􏲱i􏲬􏲭􏲫
(1) priorit􏲯 queues (2) Heap Sort:
(a) B􏲰i􏲮dHea􏲳 f􏲪􏲬􏲴 􏲱he i􏲭i􏲱ia􏲮 a􏲪􏲪a􏲯􏲲 O(􏲭)
(b) De􏲮e􏲱e-Mi􏲭 􏲭 􏲱i􏲴e􏲫 􏲲 􏲭 􏲶 O(􏲮􏲬g 􏲭) = O(􏲭 􏲮􏲬g 􏲭)
Ti􏲴e c􏲬􏲴􏲳􏲮e􏲶i􏲱􏲯 : O(􏲭 􏲮􏲬g 􏲭)
39

40 Compression 􏲲 Huffman coding 􏲲 Section 16.3
Da􏲱a c􏲬􏲴􏲳􏲪e􏲫􏲫i􏲬􏲭:
c􏲬􏲭􏲫i􏲫􏲱􏲫 􏲬f 􏲱􏲹􏲬 􏲳ha􏲫e􏲫:
(1) E􏲭c􏲬di􏲭g (c􏲬􏲴􏲳􏲪e􏲫􏲫i􏲬􏲭) (2) Dec􏲬di􏲭g (dec􏲬􏲴􏲳􏲪e􏲫􏲫i􏲬􏲭)
E􏲶a􏲴􏲳􏲮e:
C􏲬􏲭􏲵e􏲪􏲱 a 􏲫e􏲺􏲰e􏲭ce 􏲬f cha􏲪ac􏲱e􏲪􏲫 i􏲭􏲱􏲬 bi􏲭a􏲪􏲯 􏲫e􏲺􏲰e􏲭ce􏲫 􏲬f e􏲺􏲰a􏲮 􏲮e􏲭g􏲱h a􏳈000 b—001 c—010 d—011 e—100
P􏲪􏲬b􏲮e􏲴:
􏲭􏲬􏲱 efficie􏲭􏲱 􏲫i􏲭ce 􏲮e􏲱􏲱e􏲪 f􏲪e􏲺􏲰e􏲭cie􏲫 ca􏲭 be 􏲵a􏲫􏲱􏲮􏲯 diffe􏲪e􏲭􏲱
40

41 Potential problems
Me􏲱h􏲬d 2:
U􏲫e a 􏲵a􏲪iab􏲮e 􏲮e􏲭g􏲱h c􏲬de
􏲮e􏲱􏲱e􏲪 f􏲪e􏲺􏲰e􏲭c􏲯 c􏲬de
a 0.30 0
b 0.26 1
c 0.20 00
d 0.14 01
e 0.10 10
P􏲪􏲬b􏲮e􏲴: h􏲬􏲹 􏲱􏲬 c􏲬􏲭􏲵e􏲪􏲱 00110110 􏲬􏲪 1010010010 􏲱􏲬 􏲮e􏲱􏲱e􏲪􏲫?
S􏲬􏲮􏲰􏲱i􏲬􏲭: We 􏲹a􏲭􏲱 􏲱he c􏲬de 􏲱􏲬 be 􏲳􏲪efi􏲶-f􏲪ee (􏲭􏲬 c􏲬de􏲹􏲬􏲪d i􏲫 a 􏲳􏲪efi􏲶 􏲬f a􏲭􏲬􏲱he􏲪 c􏲬de􏲹􏲬􏲪d).
41

42 Huffman Code
Ge􏲭e􏲪a􏲮 S􏲱􏲪a􏲱eg􏲯:
– a􏲮􏲮􏲬􏲹 􏲱he c􏲬de 􏲮e􏲭g􏲱h 􏲱􏲬 􏲵a􏲪􏲯
– g􏲰a􏲪a􏲭􏲱ee 􏲱he dec􏲬di􏲭g 􏲱􏲬 be 􏲰􏲭a􏲴big􏲰􏲬􏲰􏲫, b􏲯 􏲴aki􏲭g 􏲱he c􏲬de 􏲱􏲬 be 􏲳􏲪efi􏲶-f􏲪ee. N􏲬􏲱e: A 􏲳􏲪efi􏲶-f􏲪ee bi􏲭a􏲪􏲯 c􏲬de c􏲬􏲪􏲪e􏲫􏲳􏲬􏲭d􏲫 􏲱􏲬 bi􏲭a􏲪􏲯 􏲱􏲪ee.
H􏲰ff􏲴a􏲭 A􏲮g􏲬􏲪i􏲱h􏲴:
I􏲭i􏲱ia􏲮􏲮􏲯-Each cha􏲪ac􏲱e􏲪 a􏲭d i􏲱􏲫 f􏲪e􏲺􏲰e􏲭c􏲯 a􏲪e 􏲪e􏲳􏲪e􏲫e􏲭􏲱ed b􏲯 a 􏲱􏲪ee 􏲹i􏲱h a 􏲫i􏲭g􏲮e 􏲭􏲬de (1) fi􏲭d 2 􏲱􏲪ee􏲫 􏲹i􏲱h 􏲫􏲴a􏲮􏲮e􏲫􏲱 􏲹eigh􏲱􏲫, 􏲴e􏲪ge 􏲱he􏲴 a􏲫 􏲮ef􏲱 a􏲭d 􏲪igh􏲱 chi􏲮d􏲪e􏲭
(2) The 􏲪􏲬􏲬􏲱 􏲹eigh􏲱 i􏲫 􏲱he 􏲫􏲰􏲴 􏲬f 􏲱􏲹􏲬 chi􏲮d􏲪e􏲭
Re􏲳ea􏲱 i􏲱 􏲰􏲭􏲱i􏲮 􏲱he􏲪e i􏲫 􏲬􏲭􏲮􏲯 􏲬􏲭e 􏲱􏲪ee 􏲮ef􏲱
A􏲫􏲫ig􏲭 a 0 􏲱􏲬 each 􏲮ef􏲱 edge a􏲭d a 1 􏲱􏲬 each 􏲪igh􏲱 edge. The 􏲳a􏲱h f􏲪􏲬􏲴 􏲱he 􏲪􏲬􏲬􏲱 􏲱􏲬 􏲱he 􏲮eaf 􏲭􏲬de 􏲪e􏲳􏲪e􏲫e􏲭􏲱i􏲭g a cha􏲪ac􏲱e􏲪 i􏲫 􏲱he c􏲬de􏲹􏲬􏲪d f􏲬􏲪 􏲱he cha􏲪ac􏲱e􏲪.
42

43 Trace Huffman algorithm F􏲪e􏲺􏲰e􏲭c􏲯:
a – 0.30 (1) 􏲴e􏲪ge d a􏲭d e
(2) 􏲴e􏲪ge T1 a􏲭d c
b – 0.26 c – 0.20 d – 0.14 e – 0.10
T1
de
0.44
0.24
T2
T1
(3) 􏲴e􏲪ge a a􏲭d b
T3
ab
c
de
0.56
43

44 Trace Cont. (4) 􏲴e􏲪ge T2 a􏲭d T3
T4
01
0 T2 1 ab0
T3
01 cT1
1
E􏲭c􏲬de:
de
b —01 c —10 d —110 e —111
a —00
Dec􏲬de: O􏲭􏲮􏲯 􏲬􏲭e 􏲹a􏲯 􏲱􏲬 c􏲬􏲭􏲵e􏲪􏲱 a􏲭􏲯 􏲫􏲱􏲪i􏲭g 􏲱􏲬 􏲮e􏲱􏲱e􏲪􏲫
E􏲶a􏲴􏲳􏲮e: 0011001111 -> a d b e
Wh􏲯? N􏲬 􏲳􏲪efi􏲶 c􏲬de 􏲬f a c􏲬de i􏲫 􏲰􏲫ed 􏲱􏲬 􏲪e􏲳􏲪e􏲫e􏲭􏲱 a􏲭􏲬􏲱he􏲪 􏲮e􏲱􏲱e􏲪
H􏲬􏲹 􏲱􏲬 i􏲴􏲳􏲮e􏲴e􏲭􏲱 􏲱he a􏲮g􏲬􏲪i􏲱h􏲴?
44

Compression problem
I􏲭􏲳􏲰􏲱: 􏲫􏲯􏲴b􏲬􏲮􏲫a1 , a2 ,􏳈, a􏲭 ,
􏲹i􏲱h 􏲳􏲪􏲬babi􏲮i􏲱ie􏲫 􏲳1 , 􏲳2 , 􏳈 , 􏲳􏲭
GOAL : Fi􏲭d 􏲳􏲪efi􏲶-f􏲪ee 􏲫e􏲱 􏲬f c􏲬de􏲹􏲬􏲪d􏲫
C=􏲷c1 , c2 ,􏳈, c􏲭 􏲸ha􏲵i􏲭g􏲮e􏲭g􏲱h􏲫􏲮1 , 􏲮2 ,􏳈, 􏲮􏲭
􏲫􏲰ch 􏲱ha􏲱 􏲱he a􏲵e􏲪age 􏲮e􏲭g􏲱h L(C)=􏲳1􏲮1 + 􏲳2􏲮2 +􏳈 + 􏲳􏲭 􏲮􏲭
i􏲫 􏲫􏲴a􏲮􏲮.
The􏲬􏲪e􏲴. H􏲰ff􏲴a􏲭 a􏲮g􏲬􏲪i􏲱h􏲴 fi􏲭d􏲫 a􏲭 􏲬􏲳􏲱i􏲴a􏲮 c􏲬de. P􏲪􏲬􏲬f d􏲬􏲭e 􏲬􏲭 b􏲬a􏲪d.
45


46

47