C++ BST中节点的所有父节点?
在打印二进制搜索树(BST)时使用递归函数(预排序)。我需要打印当前节点的所有父节点(路径形式根)。C++ BST中节点的所有父节点?,c++,c,algorithm,data-structures,advanced-search,C++,C,Algorithm,Data Structures,Advanced Search,在打印二进制搜索树(BST)时使用递归函数(预排序)。我需要打印当前节点的所有父节点(路径形式根)。 可以使用辅助数据结构(例如,代码中的路径),但我不想将节点->路径保留为存储路径 假设我正在使用预排序遍历打印行中的节点: 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 我做了如下:工作很好 在此代码中,路
可以使用辅助数据结构(例如,代码中的路径),但我不想将节点->路径保留为存储路径 假设我正在使用预排序遍历打印行中的节点:
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中有很多节点。有人能给我推荐其他数据结构或方法吗?一个建议就足够了,但应该是有效的 这里有一个老把戏,它仍然有效:
将后指针保留在调用堆栈中。
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\u helper
的每个递归都会创建一个参数结构,并将其地址传递给下一个递归,从而有效地创建一个参数链接列表,printpath\u helper
可以向上遍历以实际打印路径。由于要从上到下打印路径,printpath\u helper
还需要反转链表,因此最终将函数的递归深度加倍;如果您可以从下到上打印,printpath\u 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<<"NODE\tPATH"<<endl;
print_preorder_path(root,parents);
getchar();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
结构节点
{
int数据;
结构节点*左、*右;
};
//用于创建节点的实用程序函数
结构节点*新节点(整型数据)
{
结构节点*temp=(结构节点*)malloc(sizeof(结构节点));
温度->数据=数据;
临时->左=临时->右=空;
返回温度;
}
无效打印\u预订单\u路径(结构节点*根、向量和父节点)
{
if(root!=NULL)
{
coutleft=newNode(5);
根->右->右=新节点(7);
媒介亲本;
coutas据我所知,您需要保持某种结构来跟踪路径,而您的路径数组实际上似乎是最简单的想法。另一种方法是让每个节点保持一个指向其父节点的指针,但这当然是“浪费”始终使用此空间,而不仅仅是在打印时。您可以不进行分配,而是使用常量空间进行打印。@YefimDinitz:谢谢您的建议:)…实际的数据结构(DS)比BST复杂得多。我使用术语BST使问题陈述保持简单,因此我喜欢避免父指针…可能任何动态DS都有用,但很难维护:(.我尝试的是传递一个字符串,该字符串包含路径和要查看的节点中当前节点之前的所有节点。您可以将传递的节点的信息连接到该字符串,然后打印节点的数据和新字符串。然后对传递新字符串的子节点执行相同的函数。它工作正常,但可能不正常您想要实现它的方式,因为我必须使用stringstream将整数连接到字符串。很好的一个..这是我想实现的方式..谢谢!
#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;
}