2012-02-12 60 views
-1

任何人都可以解释为什么我在这里得到两次输出?为什么在这个树程序中我得到了两次输出?

# include<iostream> 
# include<conio.h> 
# include <stdio.h> 

using namespace std; 

struct tree 
{ 
    int data; 
    struct tree * left; 
    struct tree * right; 
}; 

struct tree * insert(struct tree * root,int value) 
{ 
    if (root==NULL) 
    { 
     struct tree * node= (struct tree *)malloc(sizeof(struct tree)); 
     node->data=value; 
     node->right=NULL; 
     node->left=NULL; 
     return node; 
    } 
    else if(root->data > value) 
     root->left=insert(root->left,value); 
    else 
     root->right=insert(root->right,value); 
} 

int same_tree(struct tree * root1, struct tree* root2) 
{ 
    if((root1==NULL && root2!=NULL) || (root1!=NULL && root2==NULL)) 
    { 
     cout << " tree are not equal \n"; 
     return -1; 
    } 
    if(root1 && root2) 
    { 
     same_tree(root1->left,root2->left); 
     if(root1->data!=root2->data) 
     { 
     cout << "tree not equal \n"; 
     return -1;       
     } 
     same_tree(root1->right,root2->right); 
    } 
} 

int main() 
{ 
    struct tree * root=NULL; 
    root= insert(root,8); 
    insert(root,6); 
    insert(root,7); 
    insert(root,5); 
    insert(root,1); 
    insert(root,20); 
    insert(root,15); 
    struct tree * root2=NULL; 
    root2= insert(root2,8); 
    insert(root2,6); 
    insert(root2,7); 
    insert(root2,5); 
    insert(root2,1); 
    insert(root2,20); 
    insert(root2,18); 
    int j= same_tree(root,root2); 
    if(j==-1) 
     cout << " tree not eqqual \n"; 
    else 
     cout << "tree are equal\n"; 
    getch(); 
    return 0; 
} 

编写该程序是为了比较两棵树是否相同(在它们包含的同一层次结构和数据中)。我在这里比较的两棵树是从main(root和root2)传入的。 如果有相同的树,我得到一次“树相等”的O/O。但如果树不相等,我得到一个o/p“tre不相等”,并在下一行中作为“树相等”。我无法解释为什么?我编写了整个程序,以便任何人都可以复制粘贴并在系统上运行它。我想问题在于same_tree的递归调用的地方,但位置和原因是什么我没有得到

+0

这是为什么标签的Java? – 2012-02-12 18:42:49

+1

这是什么*语言* FrankenC++? – 2012-02-12 18:44:38

+0

@Luchian对不起mea culpa ... untagged它 – Invictus 2012-02-12 18:45:10

回答

1

比较树时,如果左节点不同,则只返回-1(错误)。你只是忽略了正确的节点。另外,如果你的树只在右边不同,你的same_tree根本不会返回值。我真的很惊讶这个编译。

如果你看看你的same_tree观察,你会发现它检查是否只放过一个/右为空,然后如果不为空。但是,如果它们都是无效的,那么就只能通过了。您也忽略了许多点的返回值。

说你有两个根,每个价值5,其中一人已进入same_tree你会比较这将左派面前说是不同的,当离开7,而其他已经离开8.现在。然而,返回值被忽略,你会继续比较根值(都是5),因为它们是相同的,你不会返回错误代码,你的主要方法会认为每件事情都很好,并且打印出来,他们是平等的。

+0

是的,这是真的,可能是我所做的不是一个好的编程习惯。但是我确定它在不相等的情况下返回-1。并在此基础上进行主要检查。但我仍然不确定为什么我得到两个O/P。 – Invictus 2012-02-12 19:04:43

+1

当它们不相等时,您应该得到两个输出,在same_tree和main()中都有输出。 – 2012-02-12 19:14:31

+0

非常感谢。如您在评论中提到的那样投票并接受了您找到错误的答案。 – Invictus 2012-02-12 19:22:18

0

你在你的插入方法缺少return语句(惊喜呢编译):

struct tree * insert(struct tree * root,int value) 
{ 
    if (root==NULL) 
    { 
     struct tree * node= (struct tree *)malloc(sizeof(struct tree)); 
     node->data=value; 
     node->right=NULL; 
     node->left=NULL; 
     return node; 
    } 
    else if(root->data > value) 
     root->left=insert(root->left,value); 
    else 
     root->right=insert(root->right,value); 

    return root; 
} 
+0

那肯定不是问题。要插入的节点将始终从if(root == NULL)返回,尽管我没有捕获它们,除了我想作为root的第一个值。如果你用你的return语句编辑我的程序,它不会在O/P中产生任何影响,我已经提到了 – Invictus 2012-02-12 18:51:22

+0

而且还遗漏了same_tree中的返回值。 – alexander 2012-02-12 18:53:47

+0

通过为我加了,你是在摧毁有超过3个节点的任何树,作为原始根节点的左/右指针将被设置为垃圾(或NULL,如果你的编译器是好的),而不会返回'root'第四次插入。这就是为什么你的树评估是平等的,因为当你到达测试代码中的'same_tree'方法时,你已经销毁了你的子节点,并且只留下了根节点,这在测试代码中是相同的。 – kamprath 2012-02-12 19:02:25

1

隐而不宣”它会打扰你,你是否缺少same_tree中的return语句以获得相等的树?编译器应该警告你。如果您使用的是g ++,则应始终使用

-W -Wall (-pedantic is good as well) 
+0

是啊这就是真实的,可我做了什么不是一个良好的编程习惯。但是我确定它在不相等的情况下返回-1。并在此基础上进行主要检查。但我仍然不确定为什么我得到两个O/P。 Newaz +1为你的建议 – Invictus 2012-02-12 18:58:22

+0

因为你的函数(same_tree)将始终达到它返回-1(在某种递归级别)的条件。它会打印出“树不相等”。然后执行会上升到堆栈,在顶部没有返回语句,所以函数返回一些随机值。 (0在你的情况下) – Kylo 2012-02-12 19:10:17

相关问题