고가, 저는 주로 C & C++에 익숙하지만 최근에 Java에서 손을 썼습니다.Java에서 객체 비교 문제 (Context is RB Tree)
제 문제는 라인 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;
}