gpt4 book ai didi

c++ - 二叉搜索树叶数问题

转载 作者:行者123 更新时间:2023-11-28 01:27:31 24 4
gpt4 key购买 nike

我迫切需要帮助来找出我的代码中的问题,我确信它已缩小到 countLeaves 函数。无论我如何更改它,我似乎都无法将其打印出来。我是 C++ 的新手,但我真的很感激任何人能提供给我的任何东西!我将按顺序发布标题、函数和主要内容。

#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)

When you call this function from your main, it doesn't have an argument to give to countLeaves(). It throws an error as it doesn't receive any parameter.

至于解决方案,您必须在 main 中创建一个 Node 对象并将其作为参数发送。你将不得不担心你将要做什么以及如何做这一切。似乎在逻辑和语法上都存在一些错误。 (我评论了你的 countLeaves() 调用,它抛出了很多错误。推荐使用调试器。如果您目前无法使用调试器,请尝试打印值和“已输入函数”打印语句,以便更容易找到程序中的错误。

希望这对您有所帮助。

关于c++ - 二叉搜索树叶数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53112468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com