BST c++ 中的指针帮助
Pointer help in BST c++
我的BST有一个名为pTree的根。它由以下人员声明:
KnightTree* tree;
现在我需要编写一个带有指向根(在本例中为"树")的新指针的函数,如果我使用 pTree=pTree->pLeftChild; 或 pTree=pTree->pRightChild;在调用下面的函数之前,我声明:
KnightTree* treeroot=tree;
然后我调用该函数:
ReadNLR(tree,treeroot);
函数是这样的:
void ReadNLR(KnightTree*&tree,KnightTree* treeroot)
{
if(tree !=NULL)
{
cout<<tree->key<<" is at the depth of "<<NodeDepth(treeroot,tree)<<endl;
cout<<treeroot->key<<endl;
ReadNLR(tree->pLeftChild,treeroot);
ReadNLR(tree->pRightChild,treeroot);
}
}
我的想法是进行NLR读取,每个节点读取都将打印出其深度。但是我在这里遇到的问题是树根就像树的副本一样,它们是相同的,因此深度始终为 1(从节点到根的距离加 1)。如何从初始树根声明即使树更改也不会更改的树根?谢谢,对不起我的英语!
乍一看,您的代码看起来不错,只是树参数没有理由引用指针。我的猜测是NodeDepth
有问题.建议是随时跟踪级别,而不是重新计算它。您的函数对树执行深度优先扫描,如下所示:
void ReadNLR(KnightTree* tree, int level)
{
if (tree == NULL)
return;
cout << tree->key << " is at the depth of " << level;
ReadNLR(tree->pLeftChild, level + 1);
ReadNLR(tree->pRightChild, level + 1);
}
您可以通过调用带有 root
和 0
作为参数的 ReadNLR
来开始打印过程。
您还可以使用队列逐级打印树 - 如果您仍然需要帮助,我会尽快发布。
相关文章:
- 在指针的帮助下,文本文件中单词的频率
- C++指针/数组(我现在完全困惑,真的需要帮助)
- 指针帮助:取消指向访问其内部数据的结构的指针
- 函数和指针帮助:程序生成,但崩溃,没有任何特定错误
- C 指针节点帮助 - 将参数复制到链接的列表节点更改该节点的不同部分
- 作业帮助,分割故障,双免费或腐败,免费():无效的指针
- C++中的指针帮助
- 需要帮助了解指针变量分配
- BST c++ 中的指针帮助
- C 帮助(ISO C 禁止指针和整数之间的比较.)
- C++ 指针帮助缺少参数列表使用 &帐户::获取余额
- 请帮助我理解上述代码行为。了解 void 指针和双 void 指针操作
- 在指针的帮助下交换的情况下无法生成错误
- Char 数组解释为构造函数中的 char 指针请帮助
- C++ 概念帮助,指针
- 如何在 c++ 中将包含指针变量的对象写入和读取到文件中?帮助我使用以下代码
- 在结构指针方面需要帮助
- 在下面的例子中,如何在C++中使用智能指针有帮助
- 将节点附加到C++树(参考和指针帮助)
- 向指针添加"const"可以帮助优化吗?