bonnate   3년 전

자료구조를 공부중입니다.

이진트리의 멤버함수 중에서 노드의 개수, 단말노드의 개수, 트리의 높이를 구하는데

이 세 함수를 BinaryTree가 아닌 BinaryNode에 구현을 하라고 합니다.

소스코드와같이 Count_essential()은 정상적으로 작동하나

count_leaf_essential()과 height_essential()은 의도하지 않은 값이 나옵니다.

어차피 매개변수 root는 BinaryNode에서는 this일텐데.... 검색해도 안나오고 어떻게 접근해야할까요?

int count_leaf_essential() // BinaryTree의 root가 매개변수로 들어옴
{
if (NULL) return 0;

if (isLeaf()) return 1;

else return getLeft()->count_essential() + getRight()->count_essential();
}

int height_essential() BinaryTree의 root가 매개변수로 들어옴
{
BinaryNode* node = this;
if (node == NULL)

return 0;

int hLeft = node->getLeft()->height_essential();

int hRight = node->getLeft()->height_essential();

return (hLeft > hRight) ? hLeft + 1 : hRight + 1;
}

댓글을 작성하려면 로그인해야 합니다.