Mercurial > hg > Members > tatsuki > TreeMap
changeset 9:5da70bcfb824
change delete method
author | tatsuki |
---|---|
date | Thu, 16 Apr 2015 21:03:44 +0900 |
parents | 97225df15574 |
children | 6304463eefbf |
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/Node.java src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/TreeMap.java |
diffstat | 3 files changed, 51 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/BlackNode.java Tue Apr 07 00:05:06 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/BlackNode.java Thu Apr 16 21:03:44 2015 +0900 @@ -31,28 +31,66 @@ public Node deleteBalance(Node<K, V> parent) { if (rebuildFlag) { Rotate editNodeSide; - if (0 > (parent.getKey().hashCode() - key.hashCode())) //自身がどちらの子かを調べる + DeleteRebuildFlag flag; + if (0 > compare(parent)) { //自身がどちらの子かを調べる editNodeSide = Rotate.R;//右の子 - else + flag = parent.right().childRebuildDelete(Rotate.R); + + if (parent.right().isBlack()) { + boolean rightChild = this.right().checkColor(); + boolean leftChild = this.left().checkColor(); + + if (!rightChild && !leftChild) + return DeleteRebuildFlag.allBlack; + + if (rightChild) + return DeleteRebuildFlag.five; + else + return DeleteRebuildFlag.six; + + } else { + //red + } + + } else { editNodeSide = Rotate.L;//左の子 + flag = parent.right().childRebuildDelete(Rotate.L); - DeleteRebuildFlag flag = parent.RebuildDelete(editNodeSide); + if (parent.left().isBlack()) { + boolean rightChild = this.right().checkColor(); + boolean leftChild = this.left().checkColor(); + + if (!rightChild && !leftChild) + return DeleteRebuildFlag.allBlack; + + if (leftChild) + return DeleteRebuildFlag.five; + else + return DeleteRebuildFlag.six; + } else { + //red + } + } + + + if (flag == DeleteRebuildFlag.allBlack) { + if (parent.isBlack()) + return rebuildThree(parent, editNodeSide); + else + return rebuildFour(parent, editNodeSide); + } switch (flag) { case two: return rebuildTwo(parent, editNodeSide); - case three: - return rebuildThree(parent, editNodeSide); - case four: - return rebuildFour(parent, editNodeSide); case five: return rebuildfive(parent, editNodeSide); case six: return rebuildsix(parent, editNodeSide); } } - if (0 > (parent.getKey().hashCode() - this.getKey().hashCode())) + if (0 > (compare(parent))) return parent.createNode(parent.getKey(), parent.getValue(), parent.left(), this); else return parent.createNode(parent.getKey(), parent.getValue(), this, parent.right()); @@ -60,6 +98,7 @@ } + @Override protected boolean exitNode() { return true;
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/Node.java Tue Apr 07 00:05:06 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/Node.java Thu Apr 16 21:03:44 2015 +0900 @@ -11,7 +11,6 @@ protected V value; protected Node<K, V> right; protected Node<K, V> left; - protected boolean rebuildFlag = false; public Node(K key, V value) { this.key = key; @@ -29,6 +28,9 @@ return left; } + public int compare(Node<K, V> parent) { + return (parent.getKey().hashCode() - key.hashCode()); + } public Optional<V> get(K key) { @@ -55,10 +57,6 @@ return right; } - public boolean getRebuildFlag() { - return rebuildFlag; - } - public K getKey() { return key; } @@ -131,11 +129,6 @@ } - public void setRebuildFlag(boolean flag) { - this.rebuildFlag = flag; - - } - protected Node<K, V> rebuildTwo(Node<K, V> parent, Rotate side) { // case2 if (side == Rotate.L) { // rotate Left
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/TreeMap.java Tue Apr 07 00:05:06 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/TreeMap/TreeMap.java Thu Apr 16 21:03:44 2015 +0900 @@ -52,6 +52,7 @@ return root == null; } + public TreeMap<K,V> delete(K key) { Node node = root.delete(key,null);