ハイ、私はほとんど C と C++ に精通していますが、最近は Java を試しています。
私の問題は、その行if (parent.leftChild == temp)
が決して真実ではないということです。(そしてparent.leftChild.Key = temp.key
残りの内容は同じです)が、問題は、Eclipseのデバッガーでのparent.leftChildのID = ... 5792であるのに対し、tempのIDは... 3632であるという印象を受けています。
誰かがさらに説明できることを望んでいました。私のコードの回避策は、常に if ステートメントを に変更することですが、有効if (parent.leftChild.key = temp.key)
ではありませんか?parent.left == temp
class Node{
int key;
char color;
Node leftChild;
Node rightChild;
Node parent;
//...constructors..//
}
private Node GetParent(Node node){
if(node != null)
return node.parent;
else
return null;
}
private void RemoveNodeFromTree(Node myNode){
Node temp = new Node(myNode);
//traverse
if(temp.leftChild!= null){
temp = temp.leftChild;
while(temp.rightChild!= null)
temp = temp.rightChild;
myNode.key = temp.key;
}
else if(temp.rightChild != null)
myNode.key = temp.rightChild.key;
Node parent = GetParent(temp);
Node childL = temp.leftChild;
Node childR = temp.rightChild;
//have parent point to the proper new node.
//parent points to left if it exists, then it tries right.
//if both are null, point to right anyway
if(parent !=null ){
//replace temp with it's left child
if(childL!= null){
if (parent.leftChild == temp)
parent.leftChild = childL;
else
parent.rightChild = childL;
childL.parent = parent;
childL.color = 'B';
if(childL.color == 'B' && temp.color == 'B')
DoubleBlackRestructure(childL, parent);
}
else //replace temp with it's right child
{
if (parent.leftChild == temp)
parent.leftChild = childR;
else
parent.rightChild = childR;
if(childR!= null)
childR.parent = parent;
if((childR == null || childR.color == 'B') && temp.color == 'B')
{
if(childR != null)
childR.color = 'B';
DoubleBlackRestructure(childR, parent);
}
else if (childR != null)
childR.color = 'B';
}
}
else
myNode = null;
temp = null;
}