Week04 4 Amortized Conc Tree Appends

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

KQ`iBx2/ *QM+@h`22 TT2M/b

S`HH2H S`Q;`KKBM; BM a+H


H2FbM/` S`QFQT2+
*QMbiMi hBK2 TT2M/b BM *QM+@h`22b

G2iǶb mb2 *QM+@h`22b iQ BKTH2K2Mi  CombinerX


>Qr +QmH/ r2 BKTH2K2Mi += K2i?Q/\

var xs: Conc[T] = Empty


def +=(elem: T) {
xs = xs <> Single(elem)
}
*QMbiMi hBK2 TT2M/b BM *QM+@h`22b

G2iǶb mb2 *QM+@h`22b iQ BKTH2K2Mi  CombinerX


>Qr +QmH/ r2 BKTH2K2Mi += K2i?Q/\

var xs: Conc[T] = Empty


def +=(elem: T) {
xs = xs <> Single(elem)
}

h?Bb iF2b P(HQ; M) iBK2 Ĝ +M r2 /Q #2ii2` i?M i?i\


*QMbiMi hBK2 TT2M/b BM *QM+@h`22b

hQ +?B2p2 P(1) TT2M/b rBi? HQr +QMbiMi 7+iQ`b- r2 M22/ iQ 2ti2M/ i?2
*QM+@h`22 /i bi`m+im`2X
q2 rBHH BMi`Q/m+2  M2r Append MQ/2 rBi? /Bz2`2Mi b2KMiB+b,

case class Append[T](left: Conc[T], right: Conc[T]) extends Conc[T] {


val level = 1 + math.max(left.level, right.level)
val size = left.size + right.size
}
*QMbiMi hBK2 TT2M/b BM *QM+@h`22b

PM2 TQbbB#H2 appendLeaf BKTH2K2MiiBQM,

def appendLeaf[T](xs: Conc[T], y: T): Conc[T] = Append(xs, new Single(y))


*QMbiMi hBK2 TT2M/b BM *QM+@h`22b

PM2 TQbbB#H2 appendLeaf BKTH2K2MiiBQM,

def appendLeaf[T](xs: Conc[T], y: T): Conc[T] = Append(xs, new Single(y))

*M r2 biBHH /Q P(HQ; M) +QM+i2MiBQM\ AX2X +M r2 2HBKBMi2 Append


MQ/2b BM P(HQ; M) iBK2\
*QMbiMi hBK2 TT2M/b BM *QM+@h`22b

PM2 TQbbB#H2 appendLeaf BKTH2K2MiiBQM,

def appendLeaf[T](xs: Conc[T], y: T): Conc[T] = Append(xs, new Single(y))

*M r2 biBHH /Q P(HQ; M) +QM+i2MiBQM\ AX2X +M r2 2HBKBMi2 Append


MQ/2b BM P(HQ; M) iBK2\
h?Bb BKTH2K2MiiBQM #`2Fb i?2 P(HQ; M) #QmM/ QM i?2 +QM+i2MiBQMX
*QmMiBM; BM  "BM`v LmK#2` avbi2K
*QmMiBM; BM  "BM`v LmK#2` avbi2K
*QmMiBM; BM  "BM`v LmK#2` avbi2K
*QmMiBM; BM  "BM`v LmK#2` avbi2K
*QmMiBM; BM  "BM`v LmK#2` avbi2K
*QmMiBM; BM  "BM`v LmK#2` avbi2K

! hQ +QmMi mT iQ M BM i?2 #BM`v MmK#2` bvbi2K- r2 M22/ P(M) rQ`FX


*QmMiBM; BM  "BM`v LmK#2` avbi2K

! hQ +QmMi mT iQ M BM i?2 #BM`v MmK#2` bvbi2K- r2 M22/ P(M) rQ`FX


!  MmK#2` M `2[mB`2b P(HQ; M) /B;BibX
*QmMiBM; BM  "BM`v LmK#2` avbi2K
*QmMiBM; BM  "BM`v LmK#2` avbi2K

! hQ // M H2p2b iQ M Append HBbi- r2 M22/ P(M) rQ`FX


! aiQ`BM; M H2p2b `2[mB`2b P(HQ; M) Append MQ/2bX
"BM`v LmK#2` _2T`2b2MiiBQM

! 0 /B;Bi +Q``2bTQM/b iQ  KBbbBM; i`22


! 1 /B;Bi +Q``2bTQM/b iQ M 2tBbiBM; i`22
*QMbiMi hBK2 TT2M/b BM *QM+@h`22b

def appendLeaf[T](xs: Conc[T], ys: Single[T]): Conc[T] = xs match {


case Empty => ys
case xs: Single[T] => new <>(xs, ys)
case _ <> _ => new Append(xs, ys)
case xs: Append[T] => append(xs, ys)
}
*QMbiMi hBK2 TT2M/b BM *QM+@h`22b

@tailrec private def append[T](xs: Append[T], ys: Conc[T]): Conc[T] = {


if (xs.right.level > ys.level) new Append(xs, ys)
else {
val zs = new <>(xs.right, ys)
xs.left match {
case ws @ Append(_, _) => append(ws, zs)
case ws if ws.level <= zs.level => ws <> zs
case ws => new Append(ws, zs)
}
}
}
*QMbiMi hBK2 TT2M/b BM *QM+@h`22b

q2 ?p2 BKTH2K2Mi2/ M BKKmi#H2 /i bi`m+im`2 rBi?,

! P(1) TT2M/b
! P(HQ; M) +QM+i2MiBQM

L2ti- r2 rBHH b22 B7 r2 +M BKTH2K2Mi  KQ`2 2{+B2Mi- Kmi#H2 /i


*QM+@i`22 p`BMi- r?B+? +M BKTH2K2Mi  CombinerX

You might also like