






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The implementation of sortedlist and skiplist, two variations of linked lists. Sortedlist is a sorted version of a linkedlist that implements the sorted, bag, and findmin interfaces. It uses composition instead of inheritance to maintain the sorted property. The document also explains the merge process of merging two sorted collections using sortedlist. Furthermore, the concept of self-organizing list is introduced as a way to reduce the average search time. Skiplist is another variation of a linked list that uses an approximate tree structure to reduce search time to log n. The algorithm for inserting elements into a skiplist and provides an example.
Typology: Exams
1 / 11
This page cannot be seen from the preview
Don't miss anything!







jump directly to the n th^ element
temporary data buffer
Sorted List public class SortedList implements Sorted, Bag, FindMin { private LinkedList list = new LinkedList(); protected Comparator test; public SortedList(Comparator t) { test = t; } public SortedList() { test = new DefaultComparator; } ... }
Sorted List: Bag Interface // Implementation of the Bag interface. public void addElement(Object obj) { DoubleLink link = list.head; while (link != list.tail) { if (test.compare(obj, link.val) < 0) { // Skip smaller elements. link.insert(obj); return; } link = link.next } link.insert(obj); }
Merge Sort
B: 1 - 1 - 3 - 7 - 9 - 13 - 14
2
A 4 5 8 10 sentinel Call insert at …
Merge Sort
public void merge(Sorted data) { DoubleLink link = list.head; Enumeration e = data.elements(); while (e.hasMoreElements()) { Object obj = e.nextElement(); while (link != list.tail && test.compare(link.val, obj) < 0) link = link.next; link.insert(obj); } }
List Variations (cont.)
List Variations: Self-Organizing List
List Variations: Skip List
Skip List
70
2 54 70 87
2 14 54 70 83 87
2 14 18 42 54 70 78 83 83 87 94
1 2 6 10 14 18 34 41 42 52 54 70 71 78 83 83 84 87 94 90
top
tail
Skip List public class SkipList implements Sorted, Bag { private SkipLink top; private LinkedList list = new LinkedList(); protected Comparator test; public SkipList(Comparator t) { test = t; top = new SkipLink(null, list.tail); } // Implementation of the Collection interface. public boolean isEmpty() { return list.isEmpty(); } public int size() { return list.size(); } public Enumeration elements() { return list.elements(); } ... }
list list.tail
top
Skip List: Add Element
(a) Slide left until element value is greater than previous list element (b) If not at bottom list, move down one level and repeat 1(a) using recursive call
(a) Flip coin to determine if new link added at this level (b) If yes, insert new link and repeat (by returning the new link to the next higher level) (c) If no, stop (by returning null to next higher level)
Skip List: Example
8 17 23 44
5 8 12 17 23 32 44 tail
Skip List: Algorithmic Complexity – Insert
Skip List: Bag Interface – Search & Remove public boolean containsElement(Object obj) { Doublelink p = top; while (p != null) { p = slideLeft(p, obj); if (p.val != null && obj.equals(p.val)) return true; if (p instanceof SkipLink) p = ((SkipLink)p).down; else p = null; } return false; }
Complexity Comparison
SkipList O(log n ) O(log n ) O(log n )
SortedList O( n ) O( n ) O( n )
LinkedList O(1) O( n ) O( n )
SortedVector O( n ) O(log n ) O( n )
Container Insert Search Remove