如何检查两个二进制树是否包含相同的节点?
我正在尝试实施一个函数,该函数检查两个二进制搜索树是否相等,节点的顺序无关紧要。但是我的实施不起作用。
我不允许将树弄平成阵列。
这是我到目前为止的:
int isIdentical(struct Node* root1, struct Node* root2)
{
if (root1 == NULL && root2 == NULL)
return 1;
else if (root1 == NULL || root2 == NULL)
return 0;
else {
if (root1->data == root2->data && isIdentical(root1->left, root2->left)
&& isIdentical(root1->right, root2->right))
return 1;
else
return 0;
}
}
当用包含节点的树提供时>树B = 2 5 4 6 ,输出应为:
1
,这意味着它们是相等的,但是我得到了0
。我不确定我出错了哪里。
这就是插入节点的方式:
struct Node {
int data;
struct Node* left;
struct Node* right;
};
I am trying the implement a function which checks whether two binary search trees are equal, order of the nodes not matter. But my implementation does not work.
I am not allowed to flatten the trees into arrays.
this is what I have so far:
int isIdentical(struct Node* root1, struct Node* root2)
{
if (root1 == NULL && root2 == NULL)
return 1;
else if (root1 == NULL || root2 == NULL)
return 0;
else {
if (root1->data == root2->data && isIdentical(root1->left, root2->left)
&& isIdentical(root1->right, root2->right))
return 1;
else
return 0;
}
}
when supplied with trees containing the nodes tree A = 2 4 5 6
and Tree B = 2 5 4 6
, the output should be:
1
, meaning they are equal, but instead I am getting 0
. I am not sure where I am going wrong.
This is how Node is implemeted:
struct Node {
int data;
struct Node* left;
struct Node* right;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
制作递归
treea
的递归功能,并检查treeb
中的每个项目是否存在。失败时,它放弃了搜索并返回0
失败。 如果成功,可以是您的功能,请再次调用
treea
和treeb
反向的参数。 “检查现在”操作可能是迭代和内联的,因为它不需要回溯。示例未经尝试的代码,以给出这个想法。
然后像
treea
的每个项目一样呼叫,可以在treeb
中找到,并且可以在treeb
的每个项目中找到treea
代码>然后它们包含相同的数据。只要钥匙是唯一的。Make a recursive function that traverses
treeA
and checks that every item is present intreeB
. On failure it abandons the search and returns0
for failure. It can be your functionIf success, call the function again with the arguments for
treeA
andtreeB
reversed. The 'check if present' operation can be iterative and inline, because it does not need to backtrack.Example untried code, to give the idea.
Then call like
If every item of
treeA
can be found intreeB
, and every item oftreeB
can be found intreeA
then they contain the same data. Provided the keys are unique.您为什么认为它们是平等的?他们不是。
树A
表示为2 4 5 6
,我猜您通过某种预订或级别的遍历获得了。如果您的树B
(2,5,4,6)相等,则使用相同的遍历,您将获得相同的顺序。如果遍历相同,它们不等。节点顺序无关紧要:
如果节点的顺序无关紧要。您可以做的一件事是对两种树木进行遍历遍历,然后从两者中得到一个分类的阵列。然后按元素比较两个数组元素,并声明是否相等。
Why do you think they are equal? They are not.
tree A
is represented as2 4 5 6
which I guess you obtained by some sort of pre-order or level-order traversal. If yourtree B
(2, 5, 4, 6) is equal then with the same sort of traversal you'd obtain same order. They are not equal if the traversal is the same.Order of nodes doesn't matter:
If the order of the nodes doesn't matter. One thing you could do is do an inorder traversal for both trees and you get a sorted array from both. Then compare both arrays element by element and declare equal or not.
您的功能只会将其与具有完全相同结构的2棵树相提并论。如果树的平衡方式不同,那么比较将返回
0
,即使值相同。进行此比较是无关紧要的,因为如果树不平衡,树可以具有任意的深度。
您可以深入填充数组,然后以深度的一阶行走第一树,然后检查值与数组中的值相同。
这是一个简单的实现:
如果不允许您将树转换为数组,则可以:
这是后者的简单实现:
Your function will only compare as equal 2 trees that have exactly the same structure. If the trees are balanced differently, the comparison will return
0
even if the values are identical.Performing this comparison is non trivial as the trees can have an arbitrary depth if they are not balanced.
You can walk the first tree in depth first order to populate an array and then walk the second tree in depth first order, checking that the values are identical to those in the array.
Here is a simple implementation:
If you are not allowed to convert the trees to arrays, you could:
Here is a simple implementation of the latter: