二叉搜索树中一个节点的所有父节点是什么?

3

在使用递归函数(前序遍历)打印二叉搜索树(BST)时,我需要打印当前节点的所有父节点(从根开始的路径)。
可以使用辅助数据结构(例如我的代码中的path),但我不想使用node->path来存储路径。

      4                           
     / \  
    /   \  
   2     6
  / \   / \
 1   3  5  7  
假设我正在使用先序遍历按行打印节点:
NODE    PATH  
4       4  
2       4,2  
1       4,2,1
3       4,2,3       
6       4,6
5       4,6,5
7       4,6,7  

我按照以下步骤进行:工作正常!
在此代码中,路径以0(零)值结尾。而且在BST中没有节点值为0。
void printpath(int* mypath){
   while(*mypath)  
      printf("%d ", *mypath++);  
}

void preorder(struct tree *p, int* path){
    int *mypath = calloc(sizeof(path)/sizeof(int) + 1 , sizeof(int*));
    int* myp=mypath;

    if(p!=NULL){  
       while( *myp++ = *path++ );  
       --myp;
       *myp=p->data;
       *(myp+1)=0;

        printf("%d PATH ",p->data);
        printpath(mypath);
        printf("\n");
        preorder(p->left, mypath);
        preorder(p->right, mypath);
    }
    free(mypath);
}

但是我不想保留路径数组,因为BST中有很多节点。有人能给我建议其他的数据结构或方法吗?一个建议就足够了,但应该高效。


2
据我所见,您需要保持某种结构来跟踪路径,而您的路径数组实际上似乎是最简单的想法。另一种选择是让每个节点保留指向其父节点的指针,但这当然会“浪费”这个空间,不仅在打印时如此。但是,您可以通过这种方式避免分配并使用恒定的空间进行打印。 - Yefim Dinitz
@YefimDinitz:感谢您的建议 :) ... 实际的数据结构(DS)比BST复杂得多。我使用BST这个术语来保持问题陈述简单,所以我想避免父指针...也许任何动态DS都会有用,但很难维护 :( 。 - Grijesh Chauhan
我尝试的方法是传递一个包含路径中当前节点之前所有节点和要查看的节点的字符串。您将连接传递节点的信息到字符串,然后打印出节点的数据和新字符串。然后对其子节点执行相同的函数,传递新字符串。它运行良好,但可能不是您想要实现的方式,因为我必须使用stringstream将整数连接到字符串中。 - Chad Campbell
2个回答

6

这里有一个古老的技巧,至今仍然有效:在调用栈中保留后向指针。

    struct stacked_list{
      struct stacked_list* prev;
      struct tree* tree; 
    };

   void printpath_helper(int data, struct stacked_list* path) {
      if (!path->prev)
        printf("%d PATH ", data);
      else
        printpath_helper(data, path->prev);
      printf("%d ", path->tree->data);
    }

    void printpath(struct stacked_list* path) {
      printpath_helper(path->tree->data, path);
      putchar('\n');
    }

    void preorder_helper(struct stacked_list* path) {
      if (path->tree) {
        printpath(path);
        struct stacked_list child = {path, path->tree->left};
        preorder_helper(&child);
        child.tree = path->tree->right;
        preorder_helper(&child);
      }
    }

    void preorder(struct tree* tree) {
      struct stacked_list root = {NULL, tree};
      preorder_helper(&root);
    }

每次递归 preorder_helper 都会创建一个参数结构体并将其地址传递给下一个递归,从而有效地创建了一个参数的链表,printpath_helper 可以遍历这个链表来打印路径。由于你想要从上到下打印路径,printpath_helper 还需要反转链表,因此函数的递归深度会加倍;如果你可以从下到上打印,printpath_helper 可以是一个简单的循环(或尾递归)。

1

您可以使用一个单一的数组来保存当前节点的父节点,并在递归时将其传递下去,而不是创建一个新的数组,只需记得在递归完成后恢复该数组即可。我认为这样可以节省很多内存。代码如下:

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct node
{
    int data;
    struct node *left, *right;
};

// A utility function to create a node
struct node* newNode( int data )
{
    struct node* temp = (struct node *) malloc( sizeof(struct node) );

    temp->data = data;
    temp->left = temp->right = NULL;

    return temp;
}



void print_preorder_path(struct node *root,vector<int>& parents)
{
     if(root!=NULL)
     {
              cout<<root->data<<"\t";
              for(size_t i=0;i<parents.size();++i)cout<<parents[i]<<",";
              cout<<root->data<<endl;

              parents.push_back(root->data);
              print_preorder_path(root->left,parents);
              print_preorder_path(root->right,parents);
              parents.pop_back();

     }
}

int main()
{
     // Let us construct the tree given in the above diagram
    struct node *root         = newNode(4);
    root->left                = newNode(2);
    root->left->left          = newNode(1);
    root->left->right         = newNode(3);
    root->right               = newNode(6);
    root->right->left         = newNode(5);
    root->right->right        = newNode(7);

    vector<int> parents;

    cout<<"NODE\tPATH"<<endl;
    print_preorder_path(root,parents);

    getchar();
    return 0;
}

代码使用STL编写,以简化操作,您可以根据需要进行修改,希望能对您有所帮助!

不错啊,这正是我想要实现的方式。谢谢! - Grijesh Chauhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接