BST的所有节点的父母
All parents of a node in BST?
使用递归函数(预购)打印二进制搜索树(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(零)值结尾。
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编写,为简单起见,您可以根据需要进行修改,希望它有所帮助!
相关文章:
- 反向给定链表中的K节点
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- Boost Graph Library,修复节点大小
- C++A*算法并不总是在路径中具有目标节点
- 如何找到2个单链表的公共节点
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 为什么我的删除节点函数实际上没有删除节点?
- 我们可以删除链表中静态内存中的节点吗
- 如何在pugixml中获取节点的内部XML
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 节点 *temp; 和节点 *tmp = 新节点之间的差异
- 引擎节点:未定义的符号:_ZTV6Config
- 为什么"delete"关键字不删除节点?
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 如何按数字顺序插入链表节点?
- 为什么我的双向链表删除函数会删除多个节点?
- BST的所有节点的父母