L i n k e d l i s t s L i n k e d l i s t s L - - PowerPoint PPT Presentation

l i n k e d l i s t s l i n k e d l i s t s
SMART_READER_LITE
LIVE PREVIEW

L i n k e d l i s t s L i n k e d l i s t s L - - PowerPoint PPT Presentation

L i n k e d l i s t s L i n k e d l i s t s L i n k e d l i s t s a r e a d a t a s t r u c t u r e d e s i g n e d f o r t o a l i s t s e q u e n t i a l a c c e s


slide-1
SLIDE 1

L i n k e d l i s t s

slide-2
SLIDE 2

L i n k e d l i s t s

L i n k e d l i s t s a r e a d a t a s t r u c t u r e d e s i g n e d f

  • r

s e q u e n t i a l a c c e s s t

  • a

l i s t

  • Mo

v e f

  • r

w a r d s a n d b a c k w a r d s t h r

  • u

g h t h e l i s t ,

  • n

e e l e m e n t a t a t i m e

  • R

e a d

  • r

w r i t e t h e e l e m e n t a t t h e c u r r e n t p

  • s

i t i

  • n
  • I

n s e r t

  • r

d e l e t e e l e m e n t s a t t h e c u r r e n t p

  • s

i t i

  • n
  • a

l l i n O ( 1 ) t i m e

Tie d

  • w

n s i d e : g e t t i n g t

  • a

s p e c i fj c p

  • s

i t i

  • n

i n t h e l i s t t a k e s O ( n ) t i m e

  • L

i n k e d l i s t s a r e b a d f

  • r

r a n d

  • m

a c c e s s

slide-3
SLIDE 3

S i n g l y

  • l

i n k e d l i s t s

A s i n g l y

  • l

i n k e d l i s t i s m a d e u p

  • f

n

  • d

e s , w h e r e e a c h n

  • d

e c

  • n

t a i n s :

  • s
  • m

e d a t a ( t h e n

  • d

e ' s v a l u e )

  • a

l i n k ( r e f e r e n c e ) t

  • t

h e n e x t n

  • d

e i n t h e l i s t

class Node<E> { E data; Node<E> next; } Tie l i s t i t s e l f i s a r e f e r e n c e t

  • t

h e fj r s t n

  • d

e : class List<E> { Node<E> head; }

slide-4
SLIDE 4

S i n g l y

  • l

i n k e d l i s t s

Tie l i s t [ T

  • m

, D i c k , H a r r y ] a s a l i n k e d l i s t :

T

  • m

D i c k H a r r y tom.next == dick tom.data == “Tom” list.head == tom

slide-5
SLIDE 5

M

  • d

i f y i n g a l i n k e d l i s t

// Insert item at front of list void addFirst(E item) // Insert item after another item void addAfter(Node<E> node, E item) // Remove first item void removeFirst() // Remove item after another item void removeAfter(Node<E> node)

slide-6
SLIDE 6

C a l l i n g l i s t . a d d F i r s t ( “ A n n ” )

F i r s t c r e a t e a n e w l i s t n

  • d

e

T

  • m

D i c k H a r r y A n n newNode

slide-7
SLIDE 7

C a l l i n g l i s t . a d d F i r s t ( “ A n n ” )

Tie n s e t newNode.next = list.head

T

  • m

D i c k H a r r y A n n

slide-8
SLIDE 8

C a l l i n g l i s t . a d d F i r s t ( “ A n n ” )

Tie n s e t list.head = newNode D

  • n

e ! addAfter i s v e r y s i m i l a r

T

  • m

D i c k H a r r y A n n

slide-9
SLIDE 9

C a l l i n g l i s t . d e l e t e A f t e r ( t

  • m

)

T

  • r

e m

  • v

e tom.next f r

  • m

t h e l i s t , s e t tom.next = tom.next.next

T

  • m

D i c k H a r r y A n n tom tom.next tom.next.next T

  • m

D i c k H a r r y A n n

slide-10
SLIDE 10

C a l l i n g l i s t . d e l e t e A f t e r ( t

  • m

)

D

  • n

e ! deleteFirst i s v e r y s i m i l a r

T

  • m

D i c k H a r r y A n n tom tom.next tom.next.next

slide-11
SLIDE 11

H e a d e r n

  • d

e s

I t ' s n

  • t

g

  • d

t

  • h

a v e t w

  • v

e r s i

  • n

s

  • f

e a c h l i s t

  • p

e r a t i

  • n

( e . g . addFirst v s addAfter) :

  • Tie

A P I g e t s t w i c e a s b i g

  • C
  • d

e u s i n g t h e l i s t l i b r a r y w i l l n e e d s p e c i a l c a s e s w h e n i t m

  • d

i fj e s t h e f r

  • n

t

  • f

t h e l i s t

  • T

w i c e a s m u c h c

  • d

e t

  • w

r i t e

I d e a : a d d a h e a d e r n

  • d

e , a f a k e n

  • d

e t h a t s i t s a t t h e f r

  • n

t

  • f

t h e l i s t b u t d

  • e

s n ' t c

  • n

t a i n a n y d a t a W e c a n g e t r i d

  • f

addFirst(x) a n d d

  • addAfter(headerNode, x)

i n s t e a d

slide-12
SLIDE 12

L i s t w i t h h e a d e r n

  • d

e

I f w e w a n t t

  • a

d d “ A n n ” b e f

  • r

e “ T

  • m

” , w e c a n d

  • addAfter(head, “Ann”)

Tie h e a d e r n

  • d

e !

T

  • m

D i c k H a r r y A n n T

  • m

D i c k H a r r y

W e c

  • u

l d e v e n g e t r i d

  • f

t h i s l i s t

  • b

j e c t n

  • w
slide-13
SLIDE 13

D

  • u

b l y

  • l

i n k e d l i s t s

I n a s i n g l y

  • l

i n k e d l i s t y

  • u

c a n

  • n

l y g

  • f
  • r

w a r d s t h r

  • u

g h t h e l i s t :

  • I

f y

  • u

' r e a t a n

  • d

e , a n d w a n t t

  • fj

n d t h e p r e v i

  • u

s n

  • d

e , t

  • b

a d ! O n l y w a y i s t

  • s

e a r c h f

  • r

w a r d f r

  • m

t h e b e g i n n i n g

  • f

t h e l i s t

  • Tii

s a l s

  • m

e a n s w e c a n ' t d e l e t e t h e c u r r e n t n

  • d

e ( w

  • u

l d n e e d t

  • u

p d a t e i t s p r e d e c e s s

  • r

' s next fj e l d )

I n a d

  • u

b l y

  • l

i n k e d l i s t , e a c h n

  • d

e h a s a l i n k t

  • t

h e n e x t a n d t h e p r e v i

  • u

s n

  • d

e s Y

  • u

c a n i n O ( 1 ) t i m e :

  • g
  • f
  • r

w a r d s a n d b a c k w a r d s t h r

  • u

g h t h e l i s t

  • i

n s e r t a n

  • d

e b e f

  • r

e

  • r

a f t e r t h e c u r r e n t

  • n

e

  • m
  • d

i f y

  • r

d e l e t e t h e c u r r e n t n

  • d

e

Tie “ c l a s s i c ” d a t a s t r u c t u r e f

  • r

s e q u e n t i a l a c c e s s

slide-14
SLIDE 14

A d

  • u

b l y

  • l

i n k e d l i s t

T

  • m

D i c k H a r r y A n n T

  • m

D i c k H a r r y A n n Tie l i s t i t s e l f l i n k s t

  • t

h e fj r s t a n d l a s t n

  • d

e s list.first = ann list.last = harry tom.next = dick tom.prev = ann

slide-15
SLIDE 15

I n s e r t i

  • n

a n d d e l e t i

  • n

i n d

  • u

b l y

  • l

i n k e d l i s t s

S i m i l a r t

  • s

i n g l y

  • l

i n k e d l i s t s , b u t y

  • u

h a v e t

  • u

p d a t e t h e prev p

  • i

n t e r t

  • .

T

  • d

e l e t e T

  • m

i n t h e l i s t b e l

  • w

:

dick.prev = ann; ann.next = dick;

I n g e n e r a l w e c a n d

  • :

node.next.prev = node.prev; node.prev.next = node.next;

T

  • m

D i c k H a r r y A n n T

  • m

D i c k H a r r y A n n

slide-16
SLIDE 16

I n s e r t i

  • n

a n d d e l e t i

  • n

i n d

  • u

b l y

  • l

i n k e d l i s t s , c

  • n

t i n u e d

T

  • d

e l e t e t h e c u r r e n t n

  • d

e t h e i d e a i s :

node.next.prev = node.prev; node.prev.next = node.next;

B u t t h e r e a r e l

  • t

s

  • f

s p e c i a l c a s e s !

  • Wh

a t i f t h e n

  • d

e i s t h e fj r s t n

  • d

e ? Tii s c

  • d

e c r a s h e s , s i n c e node.prev == null W e a l s

  • n

e e d t

  • u

p d a t e list.first

  • Wh

a t i f t h e n

  • d

e i s t h e l a s t n

  • d

e ?

  • Wh

a t i f t h e l i s t

  • n

l y h a s

  • n

e e l e m e n t s

  • t

h e n

  • d

e i s b

  • t

h t h e fj r s t a n d t h e l a s t n

  • d

e ?

S

  • l

u t i

  • n

: c i r c u l a r l i n k e d l i s t !

slide-17
SLIDE 17

T

  • m

D i c k H a r r y A n n T

  • m

D i c k H a r r y A n n Tie h e a d e r i s j u s t a n

  • r

m a l l i s t n

  • d

e head.next = ann head.prev = harry ann.prev = head harry.next = head Tie l i s t

  • b

j e c t

slide-18
SLIDE 18

C i r c u l a r l y

  • l

i n k e d l i s t w i t h h e a d e r n

  • d

e

A n e x t r a h e a d e r n

  • d

e , “ i n b e t w e e n ” t h e fj r s t a n d l a s t e l e m e n t s i n t h e l i s t W

  • r

k s

  • u

t q u i t e n i c e l y !

  • head.next

i s t h e fj r s t e l e m e n t i n t h e l i s t

  • head.prev

i s t h e l a s t e l e m e n t

  • y
  • u

n e v e r n e e d t

  • u

p d a t e head

  • n
  • n
  • d

e ' s next

  • r

prev i s e v e r null

N

  • s

p e c i a l c a s e s i n i n s e r t i

  • n
  • r

d e l e t i

  • n

!

slide-19
SLIDE 19

S t a c k s a n d l i s t s u s i n g l i n k e d l i s t s

Y

  • u

c a n i m p l e m e n t a s t a c k u s i n g a l i n k e d l i s t :

  • p

u s h : a d d t

  • f

r

  • n

t

  • f

l i s t

  • p
  • p

: r e m

  • v

e f r

  • m

f r

  • n

t

  • f

l i s t

Y

  • u

c a n a l s

  • i

m p l e m e n t a q u e u e :

  • e

n q u e u e : a d d t

  • r

e a r

  • f

l i s t

  • d

e q u e u e : r e m

  • v

e f r

  • m

f r

  • n

t

  • f

l i s t

slide-20
SLIDE 20

A q u e u e a s a s i n g l y

  • l

i n k e d l i s t

W e c a n i m p l e m e n t a q u e u e a s a s i n g l y

  • l

i n k e d l i s t w i t h a n e x t r a rear p

  • i

n t e r : W e e n q u e u e e l e m e n t s b y a d d i n g t h e m t

  • t

h e b a c k

  • f

t h e l i s t :

  • S

e t rear.next t

  • t

h e n e w n

  • d

e

  • U

p d a t e rear s

  • i

t p

  • i

n t s t

  • t

h e n e w n

  • d

e

slide-21
SLIDE 21

W h a t ' s t h e p r

  • b

l e m w i t h t h i s ?

int sum(LinkedList<Integer> list) { int total = 0; for (int i = 0; i < list.size(); i++) total += list.get(i); return total; }

l i s t . g e t i s O ( n ) – s

  • t

h e w h

  • l

e t h i n g i s O ( n

2

) !

slide-22
SLIDE 22

B e t t e r !

int sum(LinkedList<Integer> list) { int total = 0; for (int i: list) total += i; return total; }

R e m e m b e r – l i n k e d l i s t s a r e f

  • r

s e q u e n t i a l a c c e s s

  • n

l y

slide-23
SLIDE 23

L i n k e d l i s t s – s u m m a r y

P r

  • v

i d e s e q u e n t i a l a c c e s s t

  • a

l i s t

  • S

i n g l y

  • l

i n k e d – c a n

  • n

l y g

  • f
  • r

w a r d s

  • D
  • u

b l y

  • l

i n k e d – c a n g

  • f
  • r

w a r d s

  • r

b a c k w a r d s ( d i s a d v a n t a g e : m

  • r

e m e m

  • r

y u s e )

C

  • m

p a r e d t

  • d

y n a m i c a r r a y s :

  • r

a n d

  • m

a c c e s s t a k e s O ( n ) i n s t e a d

  • f

O ( 1 ) t i m e

  • i

n s e r t / d e l e t e a r e O ( 1 ) –

  • n

c e y

  • u

fj n d t h e n

  • d

e

  • w
  • r

s e c

  • n

s t a n t f a c t

  • r

s ( e x t r a m e m

  • r

y n e e d e d f

  • r

l i s t n

  • d

e s , c a c h e

  • u

n f r i e n d l y )

slide-24
SLIDE 24

S k i p l i s t s

slide-25
SLIDE 25

L i n k e d l i s t s a r e b a d a t r a n d

  • m

a c c e s s

W e c a n u s e a s

  • r

t e d l i n k e d l i s t t

  • i

m p l e m e n t a s e t : B u t fj n d i n g a n e l e m e n t t a k e s O ( n ) t i m e N

  • t

i c e i t i s

  • n

l y fj n d i n g t h e r i g h t p l a c e i n t h e l i s t t h a t ' s s l

  • w
  • O

n c e y

  • u

' v e f

  • u

n d t h e r i g h t p l a c e t

  • i

n s e r t / d e l e t e , y

  • u

c a n m

  • d

i f y t h e l i s t i n O ( 1 ) t i m e

3 4 7 9 1 2 1 4 1 5

slide-26
SLIDE 26

B a s i c s k i p l i s t s

Tie i d e a

  • f

s k i p l i s t s : t a k e a l i n k e d l i s t a n d g i v e s

  • m

e n

  • d

e s e x t r a f

  • r

w a r d l i n k s w h i c h s k i p f u r t h e r a h e a d i n t h e l i s t

  • E

a c h n

  • d

e h a s a l e v e l – e . g . a l e v e l 3 n

  • d

e h a s 3 f

  • r

w a r d l i n k s

  • E

a c h l e v e l s k i p s f u r t h e r f

  • r

w a r d t h a n t h e l e v e l b e f

  • r

e

  • Tie

b

  • t

t

  • m

l e v e l l e t s y

  • u

g

  • t

h r

  • u

g h t h e l i s t

  • n

e b y

  • n

e a s i n a n

  • r

m a l l i n k e d l i s t

C a n v i e w t h i s a s s e v e r a l l i n k e d l i s t s , w h i c h s k i p t h r

  • u

g h d i fg e r e n t a m

  • u

n t s

  • f

t h e w h

  • l

e l i s t

3 4 7 9 1 4 1 5 1 8 1 2 2 0 2 3

slide-27
SLIDE 27

B a s i c s k i p l i s t s

Tie i d e a

  • f

s k i p l i s t s : t a k e a l i n k e d l i s t a n d g i v e s

  • m

e n

  • d

e s e x t r a f

  • r

w a r d l i n k s w h i c h s k i p f u r t h e r a h e a d i n t h e l i s t

  • E

a c h n

  • d

e h a s a l e v e l – e . g . a l e v e l 3 n

  • d

e h a s 3 f

  • r

w a r d l i n k s

  • E

a c h l e v e l s k i p s f u r t h e r f

  • r

w a r d t h a n t h e l e v e l b e f

  • r

e

  • Tie

b

  • t

t

  • m

l e v e l l e t s y

  • u

g

  • t

h r

  • u

g h t h e l i s t

  • n

e b y

  • n

e a s i n a n

  • r

m a l l i n k e d l i s t

C a n v i e w t h i s a s s e v e r a l l i n k e d l i s t s , w h i c h s k i p t h r

  • u

g h d i fg e r e n t a m

  • u

n t s

  • f

t h e w h

  • l

e l i s t

3 4 7 9 1 4 1 5 1 8 1 2 2 0 2 3 L e v e l 1 n

  • d

e : 1 f

  • r

w a r d l i n k L e v e l 3 n

  • d

e : 3 f

  • r

w a r d l i n k s Tie l e v e l 2 l i s t

slide-28
SLIDE 28

S k i p l i s t n

  • d

e s

A n

  • d

e i n a s k i p l i s t h a s s

  • m

e d a t a a n d a n a r r a y

  • f

f

  • r

w a r d l i n k s : class SkipNode<E> { E data; SkipNode<E> links[]; } Tie l e v e l i s t h e s i z e

  • f

t h i s a r r a y

9

slide-29
SLIDE 29

B a s i c s k i p l i s t s

W e c a n fj n d t h i n g s e ffjc i e n t l y i n t h e s k i p l i s t b y u s i n g t h e e x t r a l e v e l s t

s k i p a h e a d ”

  • S

t a r t a t t h e h i g h e s t l e v e l

  • f

t h e l i s t

  • G
  • r

i g h t a s f a r a s y

  • u

c a n w i t h

  • u

t g

  • i

n g p a s t t h e n

  • d

e y

  • u

' r e l

  • k

i n g f

  • r
  • Tie

n r e p e a t t h e p r

  • c

e s s

  • n

e l e v e l d

  • w

n

e . g . fj n d i n g 1 5 :

3 4 7 9 1 4 1 5 1 8 1 2 2 0 2 3

slide-30
SLIDE 30

N a i v e s k i p l i s t s

H

  • w

m a n y l e v e l s s h

  • u

l d w e h a v e ? A n d w h a t l e v e l s h

  • u

l d e a c h n

  • d

e h a v e ? I n n a i v e s k i p l i s t s :

  • t

h e l e v e l 1 l i s t c

  • n

t a i n s a l l n

  • d

e s

  • t

h e l e v e l 2 l i s t c

  • n

t a i n s e v e r y s e c

  • n

d n

  • d

e

  • t

h e l e v e l 3 l i s t c

  • n

t a i n s e v e r y f

  • u

r t h n

  • d

e

  • e

a c h l e v e l s k i p s t w i c e a s m a n y n

  • d

e s a s t h e l e v e l b e f

  • r

e

3 4 7 9 1 4 1 5 1 8 1 2 2 0 2 3

slide-31
SLIDE 31

N a i v e s k i p l i s t s

F

  • r

m a l l y , b e t w e e n a n y t w

  • n
  • d

e s

  • f

l e v e l ≥ n + 1 , t h e r e i s a n

  • d

e

  • f

l e v e l n

  • B

e t w e e n a l l l e v e l ≥ 2 n

  • d

e s t h e r e i s a l e v e l 1 n

  • d

e

3 4 7 9 1 4 1 5 1 8 1 2 2 0 2 3

slide-32
SLIDE 32

N a i v e s k i p l i s t s

F

  • r

m a l l y , b e t w e e n a n y t w

  • n
  • d

e s

  • f

l e v e l ≥ n + 1 , t h e r e i s a n

  • d

e

  • f

l e v e l n

  • B

e t w e e n a l l l e v e l ≥ 2 n

  • d

e s t h e r e i s a l e v e l 1 n

  • d

e

  • B

e t w e e n a l l l e v e l ≥ 3 n

  • d

e s t h e r e i s a l e v e l 2 n

  • d

e

3 4 7 9 1 4 1 5 1 8 1 2 2 0 2 3

slide-33
SLIDE 33

N a i v e s k i p l i s t s

F

  • r

m a l l y , b e t w e e n a n y t w

  • n
  • d

e s

  • f

l e v e l ≥ n + 1 , t h e r e i s a n

  • d

e

  • f

l e v e l n

  • B

e t w e e n a l l l e v e l ≥ 2 n

  • d

e s t h e r e i s a l e v e l 1 n

  • d

e

  • B

e t w e e n a l l l e v e l ≥ 3 n

  • d

e s t h e r e i s a l e v e l 2 n

  • d

e

  • B

e t w e e n a l l l e v e l ≥ 4 n

  • d

e s t h e r e i s a l e v e l 3 n

  • d

e

3 4 7 9 1 4 1 5 1 8 1 2 2 0 2 3

slide-34
SLIDE 34

N a i v e s k i p l i s t s

Wh y a r r a n g e t h e n

  • d

e s l i k e t h i s ? B e c a u s e , w h e n s e a r c h i n g i n t h e l i s t . . .

  • Tie

h i g h e s t l e v e l s k i p s t h r

  • u

g h h a l f t h e l i s t

  • Tie

n e x t l e v e l s k i p s t h r

  • u

g h a q u a r t e r

  • a

n d s

  • n

. . .

s

  • s

e a r c h t a k e s O ( l

  • g

n ) t i m e !

3 4 7 9 1 4 1 5 1 8 1 2 2 0 2 3

slide-35
SLIDE 35

N a i v e s k i p l i s t s

B u t u p d a t i n g a n a i v e s k i p l i s t t a k e s O ( n ) t i m e ! F

  • r

e x a m p l e , h e r e w e h a v e i n s e r t e d 1 , a n d t h e p a r t s

  • f

t h e l i s t t h a t c h a n g e d a r e h i g h l i g h t e d i n r e d . . .

3 4 7 9 1 2 1 4 1 5 1 1 8 2 0 2 3

slide-36
SLIDE 36

N a i v e s k i p l i s t s – t h e i n v a r i a n t

E a c h n

  • d

e i n t h e s k i p l i s t h a s a l e v e l

  • L

e v e l 1 c

  • n

t a i n s e v e r y e l e m e n t

  • f

t h e s k i p l i s t

  • L

e v e l 2 c

  • n

t a i n s e v e r y 2

n d

e l e m e n t

  • L

e v e l 3 c

  • n

t a i n s e v e r y 4

t h

e l e m e n t

  • L

e v e l k c

  • n

t a i n s e v e r y 2

k

  • 1

t h e l e m e n t

W e c a n s e a r c h i n O ( l

  • g

n ) t i m e B u t i n s e r t i

  • n

/ d e l e t e t a k e s O ( n ) t i m e

  • H

a v e t

  • u

p d a t e t

  • m

u c h

  • f

t h e l i s t

slide-37
SLIDE 37

P r

  • b

a b i l i s t i c s k i p l i s t s

Tie s

  • l

u t i

  • n

: p r

  • b

a b i l i s t i c s k i p l i s t s !

  • L

e v e l 1 c

  • n

t a i n s e v e r y e l e m e n t

  • f

t h e s k i p l i s t

  • L

e v e l 2 c

  • n

t a i n s r

  • u

g h l y ½

  • f

t h e e l e m e n t s

  • L

e v e l 3 c

  • n

t a i n s r

  • u

g h l y ¼

  • f

t h e e l e m e n t s

  • L

e v e l k c

  • n

t a i n s r

  • u

g h l y 1 / 2

k

  • 1
  • f

t h e e l e m e n t s

O n i n s e r t i

  • n

, w e c h

  • s

e t h e l e v e l

  • f

t h e n e w n

  • d

e a t r a n d

  • m

, m a i n t a i n i n g t h e d i s t r i b u t i

  • n

a b

  • v

e

  • level = 1;

while (coin flip gives heads) level = level + 1;

slide-38
SLIDE 38

P r

  • b

a b i l i s t i c s k i p l i s t s

H e r e i s h

  • w

a p r

  • b

a b i l i s t i c s k i p l i s t m i g h t l

  • k

:

3 4 7 9 1 2 1 4 1 5

slide-39
SLIDE 39

P r

  • b

a b i l i s t i c s k i p l i s t s

I n s e r t i n g 1 . F i r s t c h

  • s

e t h e l e v e l :

  • L

e v e l 1 : y e s

  • L

e v e l 2 : c

  • i

n fm i p , h e a d s , y e s

  • L

e v e l 3 : c

  • i

n fm i p , t a i l s , n

  • W

e m a k e i t a l e v e l 2 n

  • d

e :

3 4 7 9 1 2 1 4 1 5 1

slide-40
SLIDE 40

P r

  • b

a b i l i s t i c s k i p l i s t s

N e x t s t e p : fj n d t h e p r e d e c e s s

  • r

l e v e l 2 n

  • d

e ( t h e g r e a t e s t l e v e l 2 n

  • d

e t h a t ' s l e s s t h a n t h e n e w n

  • d

e )

  • Tii

s n

  • d

e s h

  • u

l d h a v e a l i n k t

  • t

h e n e w n

  • d

e

3 4 7 9 1 2 1 4 1 5 1

slide-41
SLIDE 41

P r

  • b

a b i l i s t i c s k i p l i s t s

N

  • w

w e i n s e r t t h e n e w n

  • d

e i n t

  • t

h e l e v e l 2 l i s t , g

  • d
  • w

n t

  • l

e v e l 1 a n d r e p e a t t h e p r

  • c

e s s

3 4 7 9 1 2 1 4 1 5 1

slide-42
SLIDE 42

P r

  • b

a b i l i s t i c s k i p l i s t s

N

  • w

w e i n s e r t t h e n

  • d

e i n t

  • t

h e l e v e l 1 l i s t , a n d w e ' r e fj n i s h e d

3 4 7 9 1 2 1 4 1 5 1

slide-43
SLIDE 43

P r

  • b

a b i l i s t i c s k i p l i s t s

D

  • n

e !

3 4 7 9 1 2 1 4 1 5 1

slide-44
SLIDE 44

P r

  • b

a b i l i s t i c s k i p l i s t s

D e l e t i

  • n

: s i m p l y r e m

  • v

e t h e n

  • d

e f r

  • m

t h e l i s t – e . g . , d e l e t i n g 7 , a l e v e l 2 n

  • d

e :

  • F

i n d l e v e l 2 p r e d e c e s s

  • r

3 4 7 9 1 2 1 4 1 5 1

slide-45
SLIDE 45

P r

  • b

a b i l i s t i c s k i p l i s t s

D e l e t i

  • n

: s i m p l y r e m

  • v

e t h e n

  • d

e f r

  • m

t h e l i s t – e . g . , d e l e t i n g 7 , a l e v e l 2 n

  • d

e :

  • F

i n d l e v e l 2 p r e d e c e s s

  • r
  • R

e m

  • v

e n

  • d

e f r

  • m

l e v e l 2

3 4 7 9 1 2 1 4 1 5 1

slide-46
SLIDE 46

P r

  • b

a b i l i s t i c s k i p l i s t s

D e l e t i

  • n

: s i m p l y r e m

  • v

e t h e n

  • d

e f r

  • m

t h e l i s t – e . g . , d e l e t i n g 7 , a l e v e l 2 n

  • d

e :

  • F

i n d l e v e l 1 p r e d e c e s s

  • r

3 4 7 9 1 2 1 4 1 5 1

slide-47
SLIDE 47

P r

  • b

a b i l i s t i c s k i p l i s t s

D e l e t i

  • n

: s i m p l y r e m

  • v

e t h e n

  • d

e f r

  • m

t h e l i s t – e . g . , d e l e t i n g 7 , a l e v e l 2 n

  • d

e :

  • F

i n d l e v e l 1 p r e d e c e s s

  • r
  • R

e m

  • v

e n

  • d

e f r

  • m

l e v e l 1

3 4 7 9 1 2 1 4 1 5 1

slide-48
SLIDE 48

P r

  • b

a b i l i s t i c s k i p l i s t s

D

  • n

e ! Q u e s t i

  • n

: w h a t h a p p e n s i f y

  • u

d e l e t e a l l t h e n

  • d

e s e x c e p t t h e l e v e l 1 n

  • d

e s ?

3 4 9 1 2 1 4 1 5 1

slide-49
SLIDE 49

P r

  • b

a b i l i s t i c s k i p l i s t s

D e l e t i

  • n

i s d a n g e r

  • u

s . . .

  • i

f y

  • u

d e l e t e a l l n

  • d

e s w i t h l e v e l > 1 , i t d e g e n e r a t e s t

  • a

l i n k e d l i s t !

B u t , t

  • d
  • t

h a t y

  • u

h a v e t

  • b

e e x t r e m e l y u n l u c k y !

  • Wh

e n y

  • u

d e l e t e a n

  • d

e , i t h a s ½ c h a n c e

  • f

b e i n g l e v e l 2 , ¼ c h a n c e

  • f

b e i n g l e v e l 4 , e t c . , s

  • y
  • u

d

  • n

' t b r e a k t h e p r

  • b

a b i l i s t i c b e h a v i

  • u

r

  • Tie

p r

  • b

a b i l i t y d i s t r i b u t i

  • n
  • f

l e v e l s i s t h e s a m e b e f

  • r

e a n d a f t e r

S

  • t

h i s i s fj n e , a s l

  • n

g a s t h e u s e r

  • f

t h e d a t a s t r u c t u r e c a n ' t s e e t h e l e v e l

  • f

e a c h n

  • d

e

  • O

t h e r w i s e t h e p r

  • b

a b i l i s t i c a r g u m e n t b r e a k s d

  • w

n !

slide-50
SLIDE 50

P r

  • b

a b i l i s t i c s k i p l i s t s – s u m m a r y

G i v e e a c h n

  • d

e a r a n d

  • m

l e v e l w h e n y

  • u

c r e a t e i t

  • N
  • d

e s w i t h h i g h e r l e v e l s a l l

  • w

y

  • u

t

  • f

a s t f

  • r

w a r d t h r

  • u

g h t h e l i s t

I n s e r t i

  • n

, d e l e t i

  • n

, l

  • k

u p : O ( l

  • g

n ) e x p e c t e d c

  • m

p l e x i t y C

  • d

e i s p r e t t y s i m p l e ! C a n a l s

  • b

e u s e d t

  • i

m p l e m e n t a s e q u e n c e ( a r r a y

  • l

i k e ) d a t a t y p e

slide-51
SLIDE 51

D e t e r m i n i s t i c s k i p l i s t s

P r

  • b

a b i l i s t i c s k i p l i s t s a r e f a s t , b u t t h e l a c k

  • f

p e r f

  • r

m a n c e g u a r a n t e e i s a b i t w

  • r

r y i n g

  • e

. g . , i f a n a t t a c k e r c a n s e e t h e r a n d

  • m

n u m b e r s e e d , t h e y c a n b r e a k t h e p e r f

  • r

m a n c e

D e t e r m i n i s t i c s k i p l i s t s h a v e O ( l

  • g

n ) t i m e c

  • m

p l e x i t y w h a t e v e r t h e s i t u a t i

  • n
  • D
  • w

n s i d e : d e l e t i

  • n

i s a b i t h a r d e r ( w e s k i p i t )

I n s p i r e d b y 2

  • 3

t r e e s !

slide-52
SLIDE 52

D e t e r m i n i s t i c s k i p l i s t s

I n a n a i v e s k i p l i s t , b e t w e e n e a c h l e v e l n + 1 n

  • d

e , t h e r e i s

  • n

l y

  • n

e l e v e l n n

  • d

e : I n a d e t e r m i n i s t i c s k i p l i s t , t h i s c a n b e e i t h e r

  • n

e

  • r

t w

  • n
  • d

e s :

slide-53
SLIDE 53

D e t e r m i n i s t i c s k i p l i s t s

T

  • i

n s e r t i n t

  • a

d e t e r m i n i s t i c s k i p l i s t , fj r s t a d d a l e v e l 1 n

  • d

e : I f t h i s c r e a t e s 3 l e v e l n n

  • d

e s i n a r

  • w

, l i f t u p t h e m i d d l e

  • n

e t

  • l

e v e l n + 1 : Tii s m i g h t c r e a t e t h r e e l e v e l n + 1 n

  • d

e s i n a r

  • w

, s

  • c
  • n

t i n u e u p !

1 2 5 3 4 1 2 5 3 4

slide-54
SLIDE 54

I n s e r t i

  • n

e x a m p l e

I n s e r t i n g 5 i n t

  • t

h i s s k i p l i s t : F i r s t i n s e r t i t a t l e v e l 1 : W e ' v e g

  • t

t h r e e l e v e l 1 n

  • d

e s w i t h

  • u

t a l e v e l 2 n

  • d

e s

  • p

r

  • m
  • t

e 4 t

  • l

e v e l 2

3 4 7 9 1 2 1 4 1 5 1 1 8 2 3 4 7 9 1 2 1 4 1 5 1 1 8 2 5

slide-55
SLIDE 55

I n s e r t i

  • n

e x a m p l e

4 h a s b e e n p r

  • m
  • t

e d t

  • l

e v e l 2 : W e ' v e g

  • t

t h r e e l e v e l 2 n

  • d

e s ( 4 , 7 , 1 ) w i t h

  • u

t a l e v e l 3 n

  • d

e s

  • p

r

  • m
  • t

e 7 t

  • l

e v e l 3 : D

  • n

e !

3 4 7 9 1 2 1 4 1 5 1 1 8 2 5 3 4 7 9 1 2 1 4 1 5 1 1 8 2 5

slide-56
SLIDE 56

R e l a t i

  • n

t

  • 2
  • 3

t r e e s

A d e t e r m i n i s t i c s k i p l i s t . . . . . . a n d t h e c

  • r

r e s p

  • n

d i n g 2

  • 3

t r e e :

3 4 7 9 1 2 1 4 1 5 1 1 8 2 0 2 3 34 9 71 1 2 1 4 1 8 1 5 2 2 3 L e v e l n s k i p l i s t n

  • d

e = l e v e l n t r e e n

  • d

e l e v e l 3 l e v e l 2 l e v e l 1

slide-57
SLIDE 57

D e t e r m i n i s t i c s k i p l i s t s – s u m m a r y

A l l

  • w

e i t h e r 1

  • r

2 l e v e l n n

  • d

e s b e t w e e n e a c h l e v e l n + 1 n

  • d

e s

  • C

a n b e s e e n a s 2

  • 3

t r e e s , i n f a c t i n c r e a s i n g t h e l e v e l i s v e r y s i m i l a r t

  • s

p l i t t i n g t h e n

  • d

e

Wh a t a b

  • u

t d e l e t i

  • n

?

  • A

l g

  • r

i t h m i s i n s p i r e d b y 2

  • 3

d e l e t i

  • n
  • U

n f

  • r

t u n a t e l y g e t s r a t h e r c

  • m

p l i c a t e d : (

S t i l l , O ( l

  • g

n ) c

  • s

t f

  • r

a l l

  • p

e r a t i

  • n

s , w i t h r e l a t i v e l y l i t t l e c

  • d

e B u t m

  • s

t s k i p l i s t s a r e t h e p r

  • b

a b i l i s t i c k i n d !

slide-58
SLIDE 58

S k i p l i s t s v e r s u s t r e e s

S k i p l i s t a d v a n t a g e s :

  • c
  • d

e i s s i m p l e r ( e s p e c i a l l y d e l e t i

  • n

i n t h e p r

  • b

a b i l i s t i c v e r s i

  • n

)

  • e

a s y t

  • i

t e r a t e t h r

  • u

g h t h e m e m b e r s

  • f

t h e l i s t

D i s a d v a n t a g e s :

  • m

u s t b e i m p l e m e n t e d a s a m u t a b l e s t r u c t u r e ( b a d i n a f u n c t i

  • n

a l l a n g u a g e ,

  • r

i f y

  • u

w a n t t

  • k

e e p

  • l

d v e r s i

  • n

s a r

  • u

n d )

  • o

n l y h a s p r

  • b

a b i l i s t i c b e h a v i

  • u

r u n l e s s y

  • u

u s e t h e m

  • r

e c

  • m

p l i c a t e d v e r s i

  • n