T ee
Baed ide b Ha Zh
Read Seci 12, 13, 6, 18, 16.3
1
2 Trees
Li e ae & e chid (a )
Tee e ae & e e chide
Gah e e ae ad e e chide. Tee (ah defiii): ceced accic gah
Tee (idcie defiii, e ef i c. ciece):
1) A ige de i a ee (caed he f he ee)
2) A de (caed he ) +ik a fiie be f ee ae a ee
Nde (ece he ) hae e ae ad a # f chide ( f he bee aached he )
a
d
j
bc efghi
2
3 Binar Trees: each node has at most 2 children
bic ca eede
Objec daa; eede ef; eede igh; bic eede() ;
bic eede(Objec j)
daa = j; ef = ; igh = ;
Tree operations:
ie(,e);
deee(,e);
fid(,e)
ee aea: ide, de, ede, beadh-fi
3
4 Tree traversals
Ide – Pede – Pde –
ef, , igh , ef, igh ef, igh,
a
b
cd
bic id Ide(eede ) if( != )
bic id Pde(eede ) if( != )
Pde(.ef); Pde(.igh); Se..i(“ “ + .daa);
outpu: c d b a
Ide(.ef); Se..i(“ “ + .daa);
Ide(.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); Sstem.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’);
Qei: ha kid f daa ce i eeded hee, ee ack?
Agih:
ce = hie(ce)
Se..i(ce.daa) if(ce.ef) E(ce.ef) if(ce.igh)E(ce.igh) ce = De(ee)
6 Level order (or breadth-first order) traversal Taee he ee ee b ee, each ee 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) Operaion:
Search, Insert, Delete, Min, Max Also: Succesor, Predecessor
23
19 33
15 25 21 43
7
8 Agih Fid
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
bic eede ie(eede , i k) if ( == )
= e eede(); .daa = k; .ef=; .igh=; e ;
ee
if( k > .daa)
.igh = ie(.igh,k);
e ; ee.ef = ie(.ef, k);
e ;
9
10 Lk f he ae 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 deleion operaion:
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 deee i
20
20
10 30
3 13 (2) The de ha e chid: cec i chid i ae
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
cec i chid ih i ae
13
46 58
40 58
49
49 eace i ae ih he ae
Compleit for BST
– heigh f a ee: be f edge a ge ah -> eae – Fid, Ie, Deee O(h), hee h i he heigh f he ee.
– h ca a a (a a fci f = be f de), deedig h e baaced he BST i.
– h ca be O(g ) : e a ha he ee i balanced
– h ca be O()
Wha i he eai beee h ad ?
h +1 2h+1 – 1
The ef ieai: bi (h?)
Righ ieai: Pf b idci .
We i ee ae h aiai he ee baaced hie dig he ee eai.
14
Search problems: e f he c aicai f ce. If ifai i aic, he e bia each k i O( g ) ie.
Ne ha ie ad deee d be (O())
If ifai i dnamic:
e a iee search, insert, ad delete, idea a i
O( g ) ie.
aach: e bia each ee; e hae ee ha each, ie, ad
deee ae de i O(h) ie, hee h i he ee’ heigh.
b he ee ca g ibaaced ad h ca be Oega().
Thee ae aia f bia each ee ha d bece baaced: AVL trees (e i ee de he heigh diffeece beee he igh bee ad ef bee), red-black trees (e i ee de he c ed back), spla trees ( ea age eeded, b eai ae i O( g ) i he aied ee ( he -cae ee).
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-downs 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/successors 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 dont care Red Node
30
Bottom-Up Deletion Case 1
! Vs 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
! Vs sibling, S, is Black and has two Black children.
” RecolorStobeRed
” PabsorbsVsextrablackness
! If P is Red, were 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
! Ss right child is RED (Left child either color)
” RotateSaroundP
” Swap colors of S and P,
and color Ss right child Black
! This is the terminal case – were done
35
Case 3 diagrams
P
S
S P
V+ S
Swap colors of S & P Color Ss right child Black
Rotate S around P
V+
P V
36
Bottom-Up Deletion Case 4
! S is Black, Ss right child is Black and Ss left child is Red
” Rotate Ss left child around S
” SwapcolorofSandSsleftchild ” Nowincase3
37
Case 4 Diagrams
P
S
Rotate Ss
left around S V+
P
V+
P
V+
S
S
Swap colors of S and Ss 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 tetbook)
Bia each ee ae aiae f daa ed eea e: each eica e i he
ee ie a echaica ee f he dik head.
Me eai: 500 MIPS achie, hee ae 500 ii f ici e ecd Dik: 3600 /i 1 ai i 1/60 ec = 16.7 .
aeage e d haf a i8.3
S =120 . e ecd
S: ie f e dik acce = ie f 4 * 106 e eai
Idea: e ‘ia ee’ iead f bia ee; a each de e eed che cie ag e ha chide.
Nde ae fae, b ee ae hae.
Me k a a de (hi i i iea e), b fee de ae iied (each e iied de ica iie a diffee dik acce).
Thi idea i ieeed ia B-ee.
Thee ae e aia f B-ee. We dic e f he: B+ – ee
18
Definition of B trees of order m:
he i eihe a eaf ha beee 2 ad chide
A -eaf de (ece he ) ha beee /2 ad chide if i ha j chide, i cai j-1 ke ae ( gide he each)
A eae ae a he ae ee, each eaf cai beee /2 ad aca ae ed i a ed -aa (e i he aa a be e)
19
A B-ee f de ih ae ha heigh h g(/2)/g(/2).
Seach: Obi (j f he gidig ke).
Ie ad Deee: hee ae a cae. I’ j iae b eae. A he eai k i O(h).
20
Ie ad Deee i a B-ee
21
22
23
Wih 99 deeed he be f ae i he eaf ge be i.; eae ae eged; he ae ha chide be he i; he a chid i aded f he ef ibig
24
25 Heaps (Chapter 6 in the tetbook)
Main applicaion: 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 aa (de he ega ce
f he bia 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) Iei
2) DeeeMi
3) BidHea (gaie a abia aa a a hea)
(1)
Insertion:
find the left-most open position and insert the new element NewElem while ( NewElem < its parent )
echange them.
54
10 4 10 5
20 40 9 20 40 9
5
9
10
20 40 4
Qei: I i ibe ha e eee a he ae ee b diffee bach a be ae ha he e ed ae?
27
28 Deee Mi
- Reace he de b he igh- de a he b ee - Whie ( e > i chide )
Echage i ih he ae f i chide
The compleit analsis: deei & iei O ( g )
Rea:
1) f eai = 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
BidHea eai
Bid a hea f a abia aa
B cede: a ih bee haig a ee h-1 ad ake he hea; he e bee ih a ee h-2 ad ake he hea, ad i ee 0 i eached.
29
30
31
32
Tie cei: 1*2h-1 + 2* 2h-2 + 3*2h-3 + . + h 20 < 2 * 2h
Sice h = g , ie cei i O().
33
34 Heap Questions
(1) D eed ecif hich eee f a hea be deeed? (2) Wha i he diffeece beee a bia each ee ad a hea?
34
Hea- Ie (edcde) iHeaIe(A, ke)
A.hea_ie = A.hea_ie+1; A[A.hea_ie] = ke; i=A.hea_ie;
hie (i > 1 ad A[ae[i]] > A[i])
a(A[i],A[ae(i)]);
i = ae(i);
35
HEAPIFY a eee (e i d i he hea)
iHeaif(A,i) //e-d A[i] l = ef(i); = igh(i);
if(l <=A. hea_ie ad A[l] < A[i])
ae = l;
ee ae = i;
if( <=A. hea_ie ad
A[] < A[ae]) ae = ;
if(ae != i)
a(A[i], A[ae]);
iHeaif(A, ae)
36
DELETE i a hea
heaEac(A)
if(A.hea_ie) < 1) e “hea i e”
ee
i= A[1];
A[1]=A[A.hea_ie]; //e a eee i he A.Hea_ie --;
iHeaif(A,1); // e d he
e i;
37
BUILD HEAP - edcde
bidMiHea(A)
// A i iiia a abia aa
// cede ce i i a hea
i = A.hea_ie/2; hie(i > 0)
iHeaif(A,i);
i–;
38
Hea Aicai
(1) priorit queues (2) Heap Sort:
(a) BidHea f he iiia aa O()
(b) Deee-Mi ie O(g ) = O( g )
Tie cei : O( g )
39
40 Compression Huffman coding Section 16.3
Daa cei:
ci f hae:
(1) Ecdig (cei) (2) Decdig (decei)
Eae:
Ce a eece f chaace i bia eece f ea egh a000 b—001 c—010 d—011 e—100
Pbe:
efficie ice ee feecie ca be a diffee
40
41 Potential problems
Mehd 2:
Ue a aiabe egh cde
ee feec cde
a 0.30 0
b 0.26 1
c 0.20 00
d 0.14 01
e 0.10 10
Pbe: h ce 00110110 1010010010 ee?
Si: We a he cde be efi-fee ( cded i a efi f ahe cded).
41
42 Huffman Code
Geea Saeg:
– a he cde egh a
– gaaee he decdig be abig, b akig he cde be efi-fee. Ne: A efi-fee bia cde ced bia ee.
Hffa Agih:
Iiia-Each chaace ad i feec ae eeeed b a ee ih a ige de (1) fid 2 ee ih ae eigh, ege he a ef ad igh chide
(2) The eigh i he f chide
Reea i i hee i e ee ef
Aig a 0 each ef edge ad a 1 each igh edge. The ah f he he eaf de eeeig a chaace i he cded f he chaace.
42
43 Trace Huffman algorithm Feec:
a – 0.30 (1) ege d ad e
(2) ege T1 ad c
b – 0.26 c – 0.20 d – 0.14 e – 0.10
T1
de
0.44
0.24
T2
T1
(3) ege a ad b
T3
ab
c
de
0.56
43
44 Trace Cont. (4) ege T2 ad T3
T4
01
0 T2 1 ab0
T3
01 cT1
1
Ecde:
de
b —01 c —10 d —110 e —111
a —00
Decde: O e a ce a ig ee
Eae: 0011001111 -> a d b e
Wh? N efi cde f a cde i ed eee ahe ee
H iee he agih?
44
Compression problem
I: ba1 , a2 ,, a ,
ih babiiie 1 , 2 , ,
GOAL : Fid efi-fee e f cded
C=c1 , c2 ,, c haigegh1 , 2 ,,
ch ha he aeage egh L(C)=11 + 22 + +
i a.
Thee. Hffa agih fid a ia cde. Pf de bad.
45
–
46
47