BST的所有节点的父母

All parents of a node in BST?

本文关键字:父母 节点 BST      更新时间:2023-10-16

使用递归函数(预购)打印二进制搜索树(BST)时。我需要打印当前节点的所有父母(路径形式)。
可以使用辅助数据结构(例如,我的代码中的路径),但我不想保留 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(零)值结尾。

中没有节点值为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中有很多节点。有人可以建议我其他数据结构/或方法吗?建议足够,但应该有效。

这是一个旧技巧,仍然有效: keep the back pointers in the call stack.

    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可能是一个简单的循环(或尾部递归)。

您可以使用单个数组来保持当前节点的父母并在执行递归时将其传递,而不是创建新数组,请记住在完成递归完成时恢复数组。我认为您可以节省很多回忆。代码如下:

#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<<"NODEtPATH"<<endl;
    print_preorder_path(root,parents);
    getchar();
    return 0;
}

代码以STL编写,为简单起见,您可以根据需要进行修改,希望它有所帮助!