CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip...

39
CS350: Data Structures © James Moscola James Moscola Department of Engineering & Computer Science York College of Pennsylvania CS350: Data Structures Skip Lists

Transcript of CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip...

Page 1: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures © James Moscola

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATA

LOG

2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATA

LOG

2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATALO

G 2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

James MoscolaDepartment of Engineering & Computer ScienceYork College of Pennsylvania

CS350: Data StructuresSkip Lists

Page 2: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Introduction

• A data structure used for storing a sorted list of elements using layers of linked lists

- Bottom layer is a standard, ordered linked list- Upper layer linked lists create ‘shortcuts’ or ‘fast lanes’ from one location of

the list to another• Higher levels traverse greater portions of the list than lower levels

2

NULL

NULL

NULL

6 10 1383 51

head

Page 3: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Introduction

• The example skip list below shows the skip list divided perfectly in halves, quarters, etc.

3

NULL

NULL

NULL

6 10 1383 51

head

Page 4: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Introduction

• In practice, skip list node heights are distributed randomly throughout the skip list - Helps us avoid the need to ‘rebalance’ the list when insertions or

deletions occur

4

NULL

NULL

NULL

6 10 1383 51

head

Page 5: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip Lists

• Operations on skip lists are comparable in efficiency to balanced binary search trees (e.g. AVL trees, Red-Black trees) - Insertion, Deletion, and Search operations all run in O(log N) time

5

Page 6: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures 6

Skip List Search

Page 7: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• To search for an element in a skip list: - Start at the highest level at the head of the skip list and begin moving

horizontally through the list• If the data in the next node is less than the desired value, move to

the next node and continue the search• If the data in the next node is greater than or equal to the desired

value, drop down one level and continue the search• When you reach the bottommost level, move forward one node• If the data in the current node is the desired data then it was

found, otherwise, the data is not in the list

7

Page 8: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Example -- find the node with a key value 6

8

NULL

NULL

NULL

6 10 1383 51

head

Page 9: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Example -- find the node with a key value 6

9

NULL

NULL

NULL

6 10 1383 51

head

(2) if node.next.data < desired value move to next node otherwise, drop down one level at current node.

Since 5 < 6, move to node 5 and continue the search.

currentNode

(1) Start at highest level of the head node and check data value at node.next

Page 10: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Example -- find the node with a key value 6

10

NULL

NULL

NULL

6 10 1383 51

head

(3) if node.next.data < desired value move to next node otherwise, drop down one level at current node.

Since 10 !< 6, stay at node 5,

drop down one level and continue the search.

currentNode

Page 11: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Example -- find the node with a key value 6

11

NULL

NULL

NULL

6 10 1383 51

head

(4) if node.next.data < desired value move to next node otherwise, drop down one level at current node.

Since 8 !< 6, stay at node 5,

drop down one level and continue the search.

currentNode

Page 12: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Example -- find the node with a key value 6

12

NULL

NULL

NULL

6 10 1383 51

head

(5) if node.next.data < desired value move to next node otherwise, drop down one level at current node.

Since 6 !< 6, stay at node 5,

drop down one level and continue the search. Already at bottommost level, so move to next node.

currentNode

Page 13: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Example -- find the node with a key value 6

13

NULL

NULL

NULL

6 10 1383 51

head

(6) Check contents of current node to see if desired data was found.

In this example, it was.

currentNode

Page 14: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Another Example -- find the node with a key value 8

14

NULL

NULL

NULL

6 10 1383 51

head

Page 15: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Another Example -- find the node with a key value 8

15

NULL

NULL

NULL

6 10 1383 51

head

currentNode

(2) if node.next.data < desired value move to next node otherwise, drop down one level at current node.

Since 5 < 8, move to node 5 and continue the search.

(1) Start at highest level of the head node and check data value at node.next

Page 16: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Another Example -- find the node with a key value 8

16

NULL

NULL

NULL

6 10 1383 51

head

(3) if node.next.data < desired value move to next node otherwise, drop down one level at current node.

Since 10 !< 8, stay at node 5,

drop down one level and continue the search.

currentNode

Page 17: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Another Example -- find the node with a key value 8

17

NULL

NULL

NULL

6 10 1383 51

head

(4) if node.next.data < desired value move to next node otherwise, drop down one level at current node.

Since 8 !< 8, stay at node 5,

drop down one level and continue the search.

currentNode

Page 18: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Another Example -- find the node with a key value 8

18

NULL

NULL

NULL

6 10 1383 51

head

currentNode

(5) if node.next.data < desired value move to next node otherwise, drop down one level at current node.

Since 6 < 8, move to node 6 and continue the search.

Page 19: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Another Example -- find the node with a key value 8

19

NULL

NULL

NULL

6 10 1383 51

head

currentNode

(6) if node.next.data < desired value move to next node otherwise, drop down one level at current node.

Since 8 !< 8, stay at node 6,

drop down one level and continue the search. Already at bottommost level, so move to next node.

Page 20: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Another Example -- find the node with a key value 8

20

NULL

NULL

NULL

6 10 1383 51

head

(7) Check contents of current node to see if desired data was found.

In this example, it was.

currentNode

Page 21: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Example -- find the node with a key value 7

- In this example, the desired element does not exist in the list- Search the list just as before, but when a value is found that exceed

that desired value, AND there are no more levels to drop, then the desired element doesn’t exist

21

NULL

NULL

NULL

6 10 1383 51

head

Page 22: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Searching

• Example -- find the node with a key value 15

- Another example where the desired element does not exist in the list- Start with a normal search, if search reaches bottom-most level and

next node is null, then element does not exist in list• If not at bottom-most level and node.next == NULL, then drop a

level and continue search

22

NULL

NULL

NULL

6 10 1383 51

head

Page 23: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures 23

Skip List Insertion

Page 24: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Insertion

• When inserting a node into a skip list, first perform a search to determine where the new node must be inserted

- Search for the value to be inserted- At each level, remember the rightmost node that is to the left where the new node will be

inserted• i.e. Keep track of where the level is decremented during the search

- If the value is found to already exist, don’t insert duplicate

• The height of a newly inserted node is chosen at random - Flip a coin, if heads, then add an additional level to the newly inserted node, if tails then don’t

add anymore levels• Probability of 1/2 that newly inserted node will only be at the bottom-most level• Probability of 1/2 that newly inserted node will have one additional level (two total)• Probability of 1/4 that newly inserted node will have two additional levels (three total)• Probability of 1/8 that newly inserted node will have three additional levels (four total)• etc.

24

Page 25: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Insertion

• Example -- insert the node with a key value 7

• Start with a search to determine where the node should be inserted

258

NULL

NULL

NULL

6 10 1383 51

head

Page 26: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Insertion

• Example -- insert the node with a key value 7

26

NULL

NULL

NULL

6 10 1383 51

head

(1) Start at highest level of the head node and check data value at node.next

(2) if node.next.data < new value move to next node otherwise, drop down one level at current node.

Since 5 < 7, move to node 5 and continue the search.

currentNode

Page 27: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Insertion

• Example -- insert the node with a key value 7

27

NULL

NULL

NULL

6 10 1383 51

head

currentNode

(3) if node.next.data < new value move to next node otherwise, drop down one level at current node.

Since 10 !< 7, stay at node 5,

drop down one level and continue the search. Record that level was decremented in node 5.

Page 28: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Insertion

• Example -- insert the node with a key value 7

28

NULL

NULL

NULL

6 10 1383 51

head

currentNode

(4) if node.next.data < new value move to next node otherwise, drop down one level at current node.

Since 8 !< 7, stay at node 5,

drop down one level and continue the search. Record that level was decremented in node 5.

Page 29: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Insertion

• Example -- insert the node with a key value 7

29

NULL

NULL

NULL

6 10 1383 51

head

currentNode

(5) if node.next.data < new value move to next node otherwise, drop down one level at current node.

Since 6 < 7, move to node 6 and continue the search.

Page 30: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Insertion

• Example -- insert the node with a key value 7

30

NULL

NULL

NULL

6 10 1383 51

head

currentNode

(6) if node.next.data < new value move to next node otherwise, drop down one level at current node.

Since 8 !< 7, stay at node 6,

drop down one level and continue the search. Record that level was decremented in node 6.

Page 31: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Insertion

• Example -- insert the node with a key value 7

31

NULL

NULL

NULL

6 10 1383 51

head

currentNode

(7) At bottommost level, and can no longer decrement level. Move to next node to finalize.

Page 32: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Insertion

• Example -- insert the node with a key value 7

32

NULL

NULL

NULL

6 10 1383 51

head

currentNode

(8) if the current node contains the same value as the new value, do nothing otherwise, insert the new node to the left of the current node.

Since 8 != 7, must insert new node to left of node 8. All pointers that MAY need to be updated have been recorded

(those marked by orange dots)

Page 33: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Insertion

• Example -- insert the node with a key value 7 - To insert node 7:

• Randomly pick height for node 7• Markers were left behind to remember the rightmost node at each level to the left of the

insertion location ... use them to update next pointers (all blue pointers must be updated)• Note that the topmost level of node 5 need not be updated since the newly created node

7 is shorter than node 5

33

NULL

NULL

NULL

6 10 1383 51

head

7

Page 34: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures 34

Skip List Deletion

Page 35: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Deletion

• For each level of which a skip list node is a part, splice the node out in the same fashion as a standard linked list

• Example -- delete the node with a key value 10

35

NULL

NULL

NULL

6 10 1383 51

head

Page 36: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Deletion

• For each level of which a skip list node is a part, splice the node out in the same fashion as a standard linked list

• Example -- delete the node with a key value 10

36

NULL

NULL

NULL

6 10 1383 51

head

First find the node to be deleted using the same search algorithm used for finding nodes.

Just like with insert, keep track of where the level is decremented during the search since those pointers will need to be updated.

Page 37: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Deletion

• For each level of which a skip list node is a part, splice the node out in the same fashion as a standard linked list

• Example -- delete the node with a key value 10

37

NULL

NULL

NULL

6 10 1383 51

headAfter finding the node to be deleted, all pointers that

need to be updated will be marked.

Page 38: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Deletion

• For each level of which a skip list node is a part, splice the node out in the same fashion as a standard linked list

• Example -- delete the node with a key value 10

38

NULL

NULL

NULL

6 10 1383 51

head

Splice the node out of the skip list. All blue pointers need to be updated since the node they currently point to is getting deleted.

Page 39: CS350: Data Structures Skip Lists - GitHub Pages · 2017. 12. 9. · CS350: Data Structures Skip List Searching • Example -- find the node with a key value 6 9 NULL NULL NULL 1

CS350: Data Structures

Skip List Deletion

• For each level of which a skip list node is a part, splice the node out in the same fashion as a standard linked list

• Example -- delete the node with a key value 10

39

NULL

NULL

NULL

6 1383 51

head Pointers have been updated.