저는 이진 트리의 잎수를 0으로 늘 렸습니다. 내 코드에 어떤 문제도 보이지 않으며 약간의 피드백을 원합니다. 감사합니다! :)이진 트리 코드의 잎 수를 얻으려면 어떻게해야합니까?
MAIN.CPP
int main()
{
BinaryTree <int> a;
a.insertNode(13);
a.insertNode(28);
a.insertNode(8);
a.insertNode(14);
cout << a.numNodes() << endl;
cout << a.numLeafNodes() << endl;
cout << a.height() << endl;
cout << a.getWidth() << endl;
system("PAUSE");
}
이진 트리 클래스 헤더. 이것은 함수가있는 클래스입니다.
template<class T>
class BinaryTree
{
private:
struct TreeNode
{
T value;
TreeNode *left;
TreeNode *right;
};
TreeNode *root;
void insert(TreeNode *&, TreeNode *&);
int countNodes(TreeNode *&nodePtr);
void countLeaves(TreeNode* nodePtr);
int getTreeHeight(TreeNode* nodePtr);
int width(TreeNode* nodePtr);
public:
BinaryTree()
{
root = nullptr;
}
void insertNode(T);
int numNodes();
int numLeafNodes();
int height();
int getWidth();
};
내 잎 수를 얻기위한 함수. 문제가 확실하지 않은 곳입니다.
는template <class T>
int BinaryTree<T>::numLeafNodes()
{
int leafCount = 0;
countLeaves(root);
return leafCount;
}
template <class T>
void BinaryTree<T>::countLeaves(TreeNode* nodePtr)
{
if (nodePtr)
{
countLeaves(nodePtr->left);
countLeaves(nodePtr->right);
if (nodePtr->left == NULL && nodePtr->right == NULL)
{
int leafCount = 0;
leafCount = leafCount + 1;
}
}
}
일부 항목이 변경되었지만 여전히 작동하지 않습니다. 오류 메시지가 나타나지 않고 컴파일이 끝나는 중간에 멈추고 처리되지 않은 예외 메시지가 표시됩니다. –