이진 검색 트리를 구축 중입니다. 이제 트리에 노드를 추가하는 데 문제가 있습니다.이진 검색 트리에 노드 추가 C++
void BinaryTree::add(int value, Node* node) {
if(!node)
node = new Node(value);
else if(node->key < value)
this->add(value, node->rightNode);
else if(node->key > value)
this->add(value, node->leftNode);
}
이 코드는 내가 전화 할 때 작동하지 않습니다
BinaryTree test;
test.add(4, test.root);
test.add(1, test.root);
test.add(5, test.root);
test.add(2, test.root);
test.add(3, test.root);
test.add(7, test.root);
test.add(6, test.root);
를 처음 추가 호출 후, 트리 '테스트'의 뿌리는 여전히 비어 있습니다. add를 호출하고 노드가 트리의 올바른 위치로 이동하면 코드가 업데이트되도록 코드를 변경해야합니까? 대단히 감사합니다!
참조로 '노드 *'를 전달할 수 있습니다. –
예, 감사합니다. @ShafikYaghmour – Ra1nWarden