Mercurial > hg > Members > tatsuki > TreeMap
changeset 2:d8f78957698f
add getLoop
author | tatsuki |
---|---|
date | Sun, 29 Mar 2015 20:57:39 +0900 |
parents | 969798e3d330 |
children | 090acf24fd3d |
files | src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/BlackNode.java src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/EmptyNode.java src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/Node.java src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/RedNode.java src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/TreeMap.java src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/test/TreeMapTest.java |
diffstat | 6 files changed, 46 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/BlackNode.java Thu Mar 26 12:10:56 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/BlackNode.java Sun Mar 29 20:57:39 2015 +0900 @@ -16,6 +16,11 @@ } @Override + protected boolean exitNode() { + return true; + } + + @Override public Node<K,V> createNode(K key, V value, Node<K, V> left, Node<K, V> right) { return new BlackNode<K, V>(key, value, left, right); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/EmptyNode.java Thu Mar 26 12:10:56 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/EmptyNode.java Sun Mar 29 20:57:39 2015 +0900 @@ -14,6 +14,11 @@ } @Override + protected boolean exitNode() { + return false; + } + + @Override public Node<K, V> clone() { return new EmptyNode<K, V>(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/Node.java Thu Mar 26 12:10:56 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/Node.java Sun Mar 29 20:57:39 2015 +0900 @@ -28,9 +28,11 @@ return left; } + public Optional<V> get(Comparable<? super K> key) { int result = key.compareTo(getKey()); + if (result > 0) return right().get(key); @@ -38,11 +40,32 @@ return left().get(key); else if (result == 0) - return Optional.ofNullable((V) getValue()); + return Optional.ofNullable(getValue()); return Optional.ofNullable(null); } + public Optional<V> getLoop(Comparable<? super K> key) { + + Node<K, V> cur = this; + do { + int result = key.compareTo(cur.getKey()); + + if (result > 0) + cur = cur.right(); + + else if (result < 0) + cur = cur.left(); + + else if (result == 0) + return Optional.ofNullable(cur.getValue()); + + } while (cur.exitNode()); + + return Optional.ofNullable(null); + } + + public Node<K, V> right() { return right; } @@ -75,6 +98,9 @@ public abstract Node clone(); + + protected abstract boolean exitNode(); + public abstract Node<K, V> createNode(K key, V value, Node<K, V> left, Node<K, V> right); abstract Node<K, V> balance();
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/RedNode.java Thu Mar 26 12:10:56 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/RedNode.java Sun Mar 29 20:57:39 2015 +0900 @@ -19,6 +19,11 @@ } @Override + protected boolean exitNode() { + return true; + } + + @Override public Node<K,V> createNode(K key, V value, Node<K, V> left, Node<K, V> right) { return new RedNode<K,V>(key, value, left, right); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/TreeMap.java Thu Mar 26 12:10:56 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/TreeMap.java Sun Mar 29 20:57:39 2015 +0900 @@ -30,7 +30,10 @@ public Optional<V> get(K key) { return root.get((Comparable<? super K>) key); + } + public Optional<V> getLoop(K key) { + return root.getLoop((Comparable<? super K>) key); } public TreeMap put(K key, V value) {
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/test/TreeMapTest.java Thu Mar 26 12:10:56 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/test/TreeMapTest.java Sun Mar 29 20:57:39 2015 +0900 @@ -22,7 +22,7 @@ for (int count = 100; count > -10; count--) { - Optional<Integer> op = map.get(count); + Optional<Integer> op = map.getLoop(count); if (op.isPresent()) System.out.println(op.get()); }