두 개의 이진 트리 사이에서 노드를 바꿔 쓰는데 문제가 있습니다.이진 트리 스와핑 방법
현재 노드를 트리의 전달 된 노드로 바꿔 쓰려고하는데 어떻게 될지 알 수 없습니다. 노드의 부모는 교환 할 수 있지만 노드 자체는 교환 할 수 없습니다. 누군가 내게 어떤 방향을 줄 수 있습니까?
public void swap(Node node) {
if(this.equals(this.parent.leftChild)){
Node tempNodeR = node.parent.rightChild;
System.out.println("Is a left child");
node.parent.rightChild = this.parent.leftChild;
this.parent.leftChild = tempNodeR;
}
else{
Node tempNodeL = node.parent.leftChild;
System.out.println("Is a right child");
node.parent.leftChild = this.parent.rightChild;
this.parent.rightChild = tempNodeL;
}
}
Calling node2.swap(node4):
Given Tree:
1 3
/ \
2 4
Resulting Tree (unchanged):
1 3
/ \
2 4
Expected Tree:
1 3
/ \
4 2
실제로 나무입니까? – Joe