n元树的最低共同祖先
lowest common ancestor of n-ary tree
我想找出二叉树的最低共同祖先。这是我在c++中尝试过的,但程序停止工作(运行时错误)。有人能建议我如何改进吗?
另外,我知道这个程序将输出给定节点的最右祖先,但我无法找到找到正确LCA的方法?
#include<cstdio>
#include<iostream>
using namespace std;
#define MARKER ')'
#define N 5
using namespace std;
// A node of N-ary tree
struct Node {
char key;
Node *child[N]; // An array of pointers for N children
};
Node *newNode(char key)
{
Node *temp = new Node;
temp->key = key;
for (int i = 0; i < N; i++)
temp->child[i] = NULL;
return temp;
}
int height(struct Node *root)
{
if(root==NULL)
return 0;
int hg[N];
int maxx=-9999;
for(int i=0;i<N;i++)
{
hg[i]=height(root->child[i])+1;
if(hg[i]>maxx)
maxx=hg[i];
}
return maxx;
}
int size(struct Node*root)
{
int sz=1;
if(root==NULL)
return 0;
else
{
for(int i=0;i<N;i++) sz=sz+size(root->child[i]);
}
return sz;
}
struct Node *LCA(struct Node *a,struct Node *b, struct Node *root)
{
cout<<a->key<<" "<<b->key<<endl;
if(a==root || b==root)
return root;
struct Node *temp=NULL;
for(int i=0;i<N;i++)
{
struct Node *res=LCA(a,b,root->child[i]);
if(res!=NULL)
{
temp=res;
}
}
return temp;
}
Node *createDummyTree()
{
Node *root = newNode('A');
root->child[0] = newNode('B');
root->child[1] = newNode('C');
root->child[2] = newNode('D');
root->child[0]->child[0] = newNode('E');
root->child[0]->child[1] = newNode('F');
root->child[2]->child[0] = newNode('G');
root->child[2]->child[1] = newNode('H');
root->child[2]->child[2] = newNode('I');
root->child[2]->child[3] = newNode('J');
root->child[0]->child[1]->child[0] = newNode('K');
return root;
}
void traverse(Node *root)
{
if (root)
{
printf("%c ", root->key);
for (int i = 0; i < N; i++)
traverse(root->child[i]);
}
}
int main()
{
Node *root = createDummyTree();
cout<<height(root)<<endl;
cout<<size(root)<<endl;
cout<<LCA(root->child[2]->child[0],root->child[2]->child[1],root)->key<<endl;
return 0;
}
解决办法很简单,朋友。首先,我们为每个节点包含一个父指针和level字段。
struct Node {
char key;
Node *child[N];
Node *parent;
int level; // An array of pointers for N children
};
现在我们将利用上面的结构。
关键是首先将两个指针放在同一层,如果这样做,它们相等,则完成,如果它们不相等,则只需将两个指针向上移动1层,直到它们相等。那它。
更重要的一点是,您不需要将根指针传递给LCA,因此您的主函数是这样的:
int main()
{
Node *root = createDummyTree();
cout<<LCA(root->child[2]->child[0],root->child[2]->child[1])->key<<endl;
return 0;
}
你的LCA函数将是这样的。
struct Node *LCA(struct Node *a,struct Node *b)
{
struct Node *larger,*smaller;
if(a->level>b->level)
{larger=a;smaller=b;}
else {larger=b;smaller=a;}
while(larger->level!=smaller->level)
larger=larger->parent;
while(larger!=smaller)
{
larger=larger->parent;
smaller=smaller->parent;
}
return larger;//you can also return smaller here.
}
在你的createDummyTree中,你需要做的唯一额外的事情就是设置每个节点的父节点和级别,它将是这样的。
Node *createDummyTree()
{
Node *root = newNode('A');
root->level=0;
root->child[0] = newNode('B');
root->child[0]->parent=root;
root->child[0] ->level=1;
root->child[1] = newNode('C');
root->child[1]->parent=root;
root->child[1] ->level=1;
root->child[2] = newNode('D');
root->child[2]->parent=root;
root->child[2] ->level=1;
root->child[0]->child[0] = newNode('E');
root->child[0]->child[0]->parent=root->child[0];
root->child[0]->child[0]->level=2;
root->child[0]->child[1] = newNode('F');
root->child[0]->child[1]->parent=root->child[0];
root->child[0]->child[1]->level=2;
root->child[2]->child[0] = newNode('G');
root->child[2]->child[0]->parent=root->child[2];
root->child[2]->child[0]->level=2;
root->child[2]->child[1] = newNode('H');
root->child[2]->child[1]->parent=root->child[2];
root->child[2]->child[1]->level=2;
root->child[2]->child[2] = newNode('I');
root->child[2]->child[2]->parent=root->child[2];
root->child[2]->child[2]->level=2;
root->child[2]->child[3] = newNode('J');
root->child[2]->child[3]->parent=root->child[2];
root->child[2]->child[3]->level=2;
root->child[0]->child[1]->child[0] = newNode('K');
root->child[0]->child[1]->child[0]->parent=root->child[0]->child[1];
root->child[0]->child[1]->child[0]->level=3;
return root;
}
即使在最坏的情况下,上面的代码也会给你在O(height)中的答案。
如果你在一棵只有向下指针的n元树中寻找lca,你会想要向下搜索树中保存a和b都可以到达的最低节点试着想想这个角度
我建议创建一个方法来查找a是否是b的后代。然后我将创建一个方法来接收一个节点、祖先节点和另外两个节点a和b,并问:a和b是否可以从祖先节点到达?然后我将有一个函数做以下事情:对于每个子元素,a和b都可以从我的子元素到达,返回这个递归函数用子元素调用的结果。如果没有子节点满足这个要求,如果a和b可以从父节点到达,我将返回父节点。然后,我将调用第三个方法,根作为父,与a和b。希望这有助于
我找到了许多不同的方法来解决这个问题,例如:
- 在node结构(link)中跟踪每个节点的父节点
- 在将原始节点映射到欧拉数(link)或跟踪每个节点的深度(link)时使用欧拉漫游 但下面的小代码是最简单的一个在我看来:
假设:
- 它假设两个给定节点都存在于给定的n元树中。为了解决这个问题,我们可以运行另一种方法来验证它们是否都存在于树中。
如何工作:
-
要找到给定节点的最低共同祖先,我们需要考虑两个选项
-
Option1:给定的两个节点都在同一子树中,其中一个是最低的共同祖先,这是给定算法返回temp
的情况 -
Option2:给定的一个节点属于访问节点的一个子树,另一个节点属于另一个子树,此时我们需要在(if count==2)
条件检查下返回本算法实现的访问节点。Node LCA(Node a, Node b, Node root) { if(a == root || b == root) return root; int count = 0; Node temp = null; for(Node iter : root.children) { Node res = LCA(a, b, iter); if(res != null) { count++; temp = res; } } if(count == 2) return root; return temp;
}
- 用于查找最低共同祖先的代码不适用于某些测试用例
- 在模板函数中提示祖先类型
- 投射到非基类,但具有共同的祖先
- C 语言设计:孩子称呼所有虚拟祖先构造函数
- 在 n 元树 c++ 中查找给定节点的祖先
- 多重继承,如何只打印一次共同祖先的一部分
- 为什么在派生最多的类的初始值设定项列表中显式调用虚拟基类构造函数的规则,而较老的祖先已经拥有它?
- 如何强制转换为正确的祖先类,以便定义模板专用化
- 在编译时确定最小公共祖先
- 使绑定知道祖先绑定
- 应该在所有继承级别还是仅在祖先级别声明虚拟函数
- 二叉搜索树的最低公共祖先解释代码
- 我的C++代码出了什么问题?最不常见的祖先
- 前向声明从嵌套模板中隐藏祖先模板参数
- 递归树,打印祖先节点
- 查找树中两个节点的最小共同祖先
- 在具有父 ID 的一般树中查找最低共同祖先
- 如何初始化指向模板化祖先数据的指针
- 如何找到对象的祖先类型?
- 当 A 和 B 没有共同祖先时,通过 dynamic_cast 从 A* 转换为 B* 是否有效?