我非常需要帮助才能在我的代码中发现问题,我敢肯定它的范围已缩小到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

    }
}

Main.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)



至于解决方案,则必须在主目录中创建一个Node对象并将其作为参数发送。您将不得不担心如何以及如何进行所有这些操作。逻辑和语法上似乎都存在一些错误。 (我评论了您的countLeaves()调用,它引发了许多错误。
推荐使用调试器。
如果当前无法使用调试器,请尝试打印值和“函数输入”的打印语句,以使查找程序中的错误更加容易。

希望这会有所帮助。

09-27 20:12