-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeList.java
More file actions
96 lines (82 loc) · 1.79 KB
/
Copy pathTreeList.java
File metadata and controls
96 lines (82 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.TreeMap;
/**
* Class to contain a DoublyLinkedList indexed using a TreeMap
*
* @author G94
*/
public class TreeList<K, V> implements Iterable<V> {
TreeMap<K, Entry<V>> index;
DoublyLinkedList<V> list;
public TreeList() {
index = new TreeMap<>();
list = new DoublyLinkedList<>();
}
public int size() {
return list.size;
}
public void add(K k, V v) {
java.util.Map.Entry<K, Entry<V>> pe = index.lowerEntry(k);
Entry<V> p;
if (pe != null)
p = pe.getValue();
else
p = list.header;
Entry<V> e = p.next;
if (e == null) {
index.put(k, list.add(v));
} else {
if (e.element == v)
return;
index.put(k, list.addAfter(v, p));
}
}
public boolean contains(K k) {
return index.containsKey(k);
}
public V get(K k) {
return index.get(k).element;
}
public V delete(K k) {
Entry<V> entry = index.remove(k);
list.remove(entry);
return entry.element;
}
@Override
public Iterator<V> iterator() {
return list.iterator();
}
public Iterator<V> rangeIterator(K startKey, K endKey) {
java.util.Map.Entry<K, Entry<V>> pe = index.lowerEntry(startKey);
Entry<V> p;
if (pe == null)
p = list.header;
else
p = pe.getValue();
return new RangeIterator(p, index.get(endKey));
}
private class RangeIterator implements Iterator<V> {
Entry<V> p;
Entry<V> e;
public RangeIterator(Entry<V> s, Entry<V> e) {
this.p = s;
this.e = e;
}
@Override
public boolean hasNext() {
return p != e;
}
@Override
public V next() {
if (!hasNext())
throw new NoSuchElementException();
p = p.next;
return p.element;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
}
}