이진 검색 트리를 만드는 중이고 각 노드의 높이를 기록하고 합계하는 함수를 만들고 싶습니다. 재귀를 사용하려고합니다.이진 탐색 트리의 전체 높이
어려운 점은 각 노드에 높이를 지정하고 돌아가서 합산하는 것입니다. 한 번에 높이를 지정하고 기록 할 수 없다면? 미리 감사드립니다.
편집 : 미래에 이것을 보게 될 사람을 위해 나를 위해 일한 것을 보여주는 최종 코드. 도움을 주셔서 감사합니다.
BST.h
int totalheight(node);
int getHeight(node);
class BST {
Node root;
public:
BST { root = NULL; }
int totalheight()
{ return ::totalheight(root);
};
BST.cpp
int totalHeight(BSTNode* node)
{
if (node == NULL)
return -1;
int leftHeight = getheight(node->left);
int rightHeight = getheight(node->right);
int totalheight = 1 + leftHeight + rightHeight; // +1 to count the root
return totalheight;
}
int getheight(BSTNode* node)
{
if (node == NULL)
return 0;
return 1 + max(getheight(node->left), getheight(node->right));
}
main.cpp
int main() {
BST tree; // and various inserts
tree.totalheight();
} // main
당신이 코드를 조금을 정렬 할 수 있습니다 여기에
전반적인 기능입니다? 'totalheigh (BSTNode *)','findheight()','getheight()'... 매개 변수가없는'totalheight()'... 조금 혼란 스럽습니다. – Angew당신은 주로 이름 짓기와 구문 문제를 겪고 있고 전략적 장소에서'+ 1'을 잊어 버린 것처럼 보입니다. – molbdnilo
고정, 위 참조. 메인 파일에서 헤더 파일을 어떻게 호출하는지 보려면 헤더 파일을 포함 시켰습니다. – Dalkurac