二进制搜索树叶数问题

Binary Search Tree leaf count issue

本文关键字:问题 叶数 搜索树 二进制      更新时间:2023-10-16

我迫切需要帮助来查找代码中的问题,我确信它已经缩小到countLeaves函数。无论我如何修改,我似乎都无法将其打印出来。我对C++很陌生,但我真的很感激任何人能为我提供的东西!我将按照这个顺序发布标题、函数和main。

#include <iostream>
//#include<stack>
//#include<queue>
#ifndef BSTFunction
#define BSTFunction
using namespace std;
typedef int num;
class Node{
public:
num info;
Node* left;
Node* right;
Node(); // Valuetype to num
Node(num);
};
class BST{
public:
Node* findNode(num);
Node* findParent(num);
Node* findrightnode(Node*);
void inorder(Node*);
Node* root;
Node* curr;
//Was public:
BST();
void insert(num);
void inorderTraversal(); //was traverse
num search();
void custom_print();
int countLeaves(Node* T);
};
#endif

函数.cpp

#include <iostream>
#include <queue>
#include "BSTFunction.hpp"
Node::Node(){
left=right=NULL;
}
Node::Node(num val){
info=val;
left=right=NULL;
}
//constructor
BST::BST(){
root=curr=NULL;
}
//insert a node with value val in tree
void BST::insert(num val){
if(root==NULL)
root = new Node(val);
else{
Node* p =findNode(val);
if(p==0) {
//cout<<"fine1";
Node* parent=root;
if (p != root)
parent = findParent(val);
if(val>parent->info) parent->right=new Node(val);
else parent->left=new Node(val);
}//cout<<"fine2";
}
}
//remove the node if value is val
//fins node with a value key
Node* BST::findNode(num key){
Node* p =root;
while((p!=NULL)&&(p->info!=key)){
if(key<p->info)p=p->left;
else p=p->right;
}
return p;
}
//find parent of a node with value key
Node* BST::findParent(num key){
Node* p =root;
Node* q=0;
while((p!=NULL)&&(p->info!=key)){
q=p;
if(key<p->info)p=p->left;
else p=p->right;
}
return q;
}
//finds the most right of a node p(means immediate succesor of p in inorder representation)
//Node* BST::findrightnode(Node* p){
//    Node* righty=p;
//    while(righty->right!=NULL)
//        righty=righty->right;
//    return righty;
//}
void BST::inorder(Node* p){
if(p!=NULL){
inorder(p->left);
cout<<p->info<<" ";
inorder(p->right); }
}
void BST::inorderTraversal(){
cout<<endl<<"Inorder: ";
inorder(root);
cout<<endl;
}
//to print tree hightwise i.e. all nodes at h1, then all nodes at h2, then at h3
void BST::custom_print(){
//Node* temp;
if(root==NULL)
return;
queue<Node*> Q;
Q.push(root);
//Q.push(NULL);
while(!Q.empty()){
curr=Q.front();
cout<<curr<<" ";
Q.pop();
Q.push(curr->left);
Q.push(curr->right);
}
}

int BST::countLeaves(Node *T)
{
if(T ==NULL)      //if T is empty, return0
{
return(0);
}
else if(T -> left == NULL && T-> right == NULL)      //if T has0 children, then it is a leaf
{
return(1);
}
else
{
return countLeaves(T -> left) + countLeaves(T -> right);  //recursive call to find more leaves
}
}

主要.cpp

#include<iostream>
#include "BSTFunction.hpp"
int main()
{
BST leaves;
leaves.insert(24);
leaves.insert(43); //The code will take all of these numbers entered into the main function and put them in traversal order, much like it could under any order (post or pre) if needed. (Note to self: Not needed for this assignment)
leaves.insert(82);
leaves.insert(22);
leaves.insert(12);
leaves.insert(92);
leaves.insert(68);
leaves.insert(20);
leaves.insert(4);
cout << "These are the in order leaves for the Bianary Search Tree. " << endl;
leaves.inorderTraversal();
cout << "The number of leaves are: " << endl;
leaves.countLeaves()
//leaves.custom_print();
return 0;
}

代码中的问题是countLeaves()函数-中有一个参数

int BST::countLeaves(Node *T)

当您从main调用此函数时,它没有给CCD_ 2的参数。它抛出了一个错误,因为它没有接收任何参数。

对于解决方案,您必须在main中创建一个Node对象,并将其作为参数发送。你将不得不担心你将要做什么以及如何做这一切。在逻辑和语法上似乎都有一些错误。(我评论了您的countLeaves()调用,它抛出了许多错误。建议使用调试器。如果您现在不能使用调试器,请尝试打印值和"函数输入"打印语句,这样可以更容易地发现程序中的错误。

希望这有帮助。