BST前序遍历并将树内容写入临时数组

发布于 2024-09-08 08:00:10 字数 507 浏览 2 评论 0原文

我正在尝试将二叉搜索树的内容写入临时数组,以便在 main 中使用。但是我不知道该怎么做...我尝试过这样的事情:

void Book::preorder(TreeNode *ptr, Person &temp[], int x)
{

 if(ptr!=NULL)
 {
  temp[x].name=ptr->item.name;
  x++;
  preorder(ptr->left, temp, x);
  preorder(ptr->right, temp, x);
 }

}

并且,它给出了以下错误:

将“temp”声明为引用数组

“((Book*)this->Book::temp[x]”中的“operator[]”不匹配

没有匹配的函数来调用 'Book::preorder(TreeNode*&, Person&, int&)'

I'm trying to write binary search tree's content to temporary array in order to use in main. However I'm not sure how to do it... I have tried something like this:

void Book::preorder(TreeNode *ptr, Person &temp[], int x)
{

 if(ptr!=NULL)
 {
  temp[x].name=ptr->item.name;
  x++;
  preorder(ptr->left, temp, x);
  preorder(ptr->right, temp, x);
 }

}

And, it gives following errors:

declaration of 'temp'a as array of references

no match for 'operator[]' in '((Book*)this->Book::temp[x]'

no matching function for call to 'Book::preorder(TreeNode*&, Person&,
int&)'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉城已无爱 2024-09-15 08:00:10

试试这个:

void Book::preorder(TreeNode *ptr, Person temp[], int x)
{

 if(ptr!=NULL)
 {
  temp[x].name=ptr->item.name;
  x++;
  preorder(ptr->left, temp, x);
  preorder(ptr->right, temp, x);
 }

}

Try this:

void Book::preorder(TreeNode *ptr, Person temp[], int x)
{

 if(ptr!=NULL)
 {
  temp[x].name=ptr->item.name;
  x++;
  preorder(ptr->left, temp, x);
  preorder(ptr->right, temp, x);
 }

}
寒江雪… 2024-09-15 08:00:10
void Book::preorder(TreeNode *ptr, Person temp[])
{
    if(ptr!=NULL)
    {
        temp[globX].name=ptr->item.name;
        temp[globX].phoneNumber=ptr->item.phoneNumber;
        globX++;
        preorder(ptr->left, temp);
        preorder(ptr->right, temp);
    }

}

是我的最终代码。而且我很确定它可以工作......以前的代码有某种逻辑错误。使用全局变量(我知道不推荐)我想通了。

void Book::preorder(TreeNode *ptr, Person temp[])
{
    if(ptr!=NULL)
    {
        temp[globX].name=ptr->item.name;
        temp[globX].phoneNumber=ptr->item.phoneNumber;
        globX++;
        preorder(ptr->left, temp);
        preorder(ptr->right, temp);
    }

}

is my final code. And i'm pretty sure that it works... Previous code has some kind of logic error. Using global variable (i know it's not recommended) I figured it out.

╰つ倒转 2024-09-15 08:00:10

这也许为时已晚,但我认为在这里指出是很好的。上述答案在这里所建议的仅与序列化二叉(搜索)树有关。想象一下,您希望稍后根据序列化版本重建树。您必须标记叶子,以便当您尝试重新构建树时,可以清楚哪个节点是另一个节点的子节点。要实现此目的,只需在遇到叶节点时向数组(或列表)添加 NULL 引用即可。然而,当向上一级时,请向数组添加另一个 NULL 引用。换句话说,在每次递归返回之前,只需将 NULL 添加到已传入该方法的数组中即可。这样,任何向上移动都会产生向数组插入 NULL 的结果。重建列表时,从左到右从数组读取元素时,将元素添加到堆栈中。一旦命中 NULL 引用,您就会从堆栈中 pop() 最顶层的对象,并留下堆栈的下一个 peek() 以将其作为其子对象之一指向。继续处理数组中的下一个元素并重复相同的过程。也许有更好的方法来进一步优化这种方法,但这是我最想提到的。希望有帮助。

This is perhaps too late, however I believe, is good to point out here. What the aforementioned answers suggest over here solely relate to serializing a binary (search) tree. Imagine you wish to reconstruct the tree later given its serialized version. You have to mark up the leaves so that when you attempt to re-build the tree, it is clear which node is a child of another one. To achieve this, simply add NULL references to the array (or list) when you encounter a leaf node. Yet, when going one level up, add yet another NULL reference to the array. In other words, before returning from each recursion, simply add a NULL to the array that has been passed in to the method. This way, any move one level up will yield insertion of a NULL to the array. When reconstructing the list, add the elements to a stack as you read them from the array left-to-right. Once hitting a NULL reference, you pop() the topmost object from the stack and left the next peek() of the stack to point to it as one of its children. Continue to the next element within the array and repeat the same procedure. There are perhaps better ways to further optimize this approach but this is what was on top of my head to mention. Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文