在我的 BST 实现中,我的 findNode 函数没有将父位置返回给其调用函数
In my BST implementation,my findNode function not returning parent location to its calling function
在实现二叉搜索树时,我尝试拥有一个自定义查找成员函数(findNode),它可以为我提供节点位置以及父位置,但它未能将其返回给调用函数。
在实现二叉搜索树时,我尝试使用自定义查找成员函数(findNode),它可以为我提供节点位置以及父位置。 如果我在 findNode 内部检查,它能够找到父位置。但它未能将其返回到调用函数。
#include<iostream>
using namespace std;
struct node
{
struct node* left;
int data;
struct node* right;
};
typedef struct node myNode;
class myTree
{
myNode* root;
void inorderUtil(myNode* rt);
public:
myNode* createNode(int num);
myTree():root(NULL){};
void inorder();
//void postorder();
//void preorder();
void findNode(int num,myNode* par,myNode* loc);
void insertNode(int num);
void displayroot(){cout<<root->data<<endl;}
};
void myTree::inorder()
{
inorderUtil(root);
}
void myTree::inorderUtil(myNode* rt)
{
if(rt != NULL)
{
inorderUtil(rt->left);
cout<<rt->data<<endl;
inorderUtil(rt->right);
}
}
myNode* myTree::createNode(int num)
{
myNode* nd= new myNode();
nd->data=num;
nd->left=NULL;
nd->right=NULL;
return nd;
}
void myTree::findNode(int num,myNode* par,myNode* loc)
{
cout<<"Searching for "<<num<<endl;
if(root==NULL)
{
par=NULL;
loc=NULL;
//cout<<"tree is empty"<<endl;
return;
}
if(root->data==num)
{
par=NULL;
loc=root;
return;
}
//cout<<"tree is non empty and data not at root"<<endl;
myNode* r=root;
par=root;
loc=NULL;
while((r->left != NULL) || (r->right != NULL))
{
if(r->left!=NULL && (r->left->data ==num))
{
par=r;
loc=r->left;
break;
}
else if(r->right!=NULL && (r->right->data ==num))
{
par=r;
loc=r->right;
break;
}
else
{
par = r;
if(num < r->data)
r = r->left;
else
r = r->right;
}
}
cout<<"parent is ="<<par->data<<endl;
}
void myTree::insertNode(int num)
{
myNode* par=NULL;
myNode* loc=NULL;
findNode(num,par,loc);
if(par !=NULL)
{
cout<<"in insert parent is ="<<par->data<<endl;
}
if(loc != NULL)
{
cout<<num<<" is already in tree, Aborting insertion "<<endl;
}
else
{
myNode *nd= createNode(num);
if(par == NULL)
{
root=nd;
}
else
{
if(par->left == NULL && par->data >num)
par->left =nd;
if(par->right == NULL && par->data <num)
par->right =nd;
}
}
}
int main()
{
myTree tr;
tr.insertNode(50);
tr.displayroot();
myNode* par=NULL;
myNode* loc=NULL;
tr.findNode(40,par,loc);
if(par!=NULL)
{
cout<<"parent from main is=="<<par->data<<endl;
}
return 0;
}
它也应该打印在下面几行:- 主父是==
您似乎忘记了默认情况下C++中的参数是按值传递的。 这意味着参数的值被复制到函数的局部变量中。
如果你想给调用方一个值,要么返回它(例如std::pair<myNode*, myNode*>
) 或通过引用传递指针(例如myNode*& par
)。
相关文章:
- 为什么我的删除节点函数实际上没有删除节点?
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- 模板,函数使用错误的构造函数来复制我的对象
- 为什么我的递归函数按降序打印,然后按升序打印?
- 为什么我的模板化函数需要从一个迭代器转换到另一个迭代器?
- 在哪里放置我的函数?进入我的母语 Gui 还是进入我的演示者?
- 为什么我的数组双精度函数不起作用?
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- 为什么我的双向链表删除函数会删除多个节点?
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 如何返回实际值(在我的例子中是无符号字符数组)而不是来自 C++ 函数的指针?
- 为什么我的运算符 + 重载尽管是通过引用传递的,但仍调用我的复制构造函数?
- 为什么在尝试测量函数返回所需的时间时,我的运行时编号是错误的?
- 查找定义我的 C/C++ 函数/宏的文件比'grep'更简单的方法
- 为什么我的 while 函数没有终止?
- 为什么我的 constexpr 对象在我的函数中不是 constexpr?
- 我关于函数"Assert"的C++代码有问题
- 如何检测除整数以外的任何内容是否传递给我的类构造函数?
- 链表堆栈类的复制构造函数:我的正在向后复制
- 为什么在这种情况下没有调用复制构造函数-我的推理正确吗?