帮我看一下我的二叉树的实现有什么问题,为什么遍历的时候不对啊!
日期:2006-08-27 荐:
帮我看一下我的二叉树的实现有什么问题,为什么遍历的时候不对啊!复习数据结构中.....我参考 严蔚敏的《数据结构》 来实现 二叉树, 是参照先序遍历递归创建的, 但是 二叉树建好了以后, 中序遍历和后序遍历 结果都不对啊, 有谁能帮帮我啊!代码如下#include <stdio.h>#include <stdlib.h>typedef char elemType;typedef struct btree{elemType data;struct btree *leftnode, *rightnode;} btree;int createnode(btree **tree){char ch=getchar();if(ch == '#')*tree = NULL;else{if(!((*tree)=(btree *)malloc(sizeof(btree)))){printf(":malloc error!");exit(-1);}(*tree)->data = ch;createnode(&((*tree)->leftnode));createnode(&((*tree)->rightnode));}return 0;}int preorder(btree *tree, int (*visit)(elemType)){if(tree == NULL){return 0;} else{if(!visit(tree->data)){if (!preorder(tree->leftnode, visit)){if(!preorder(tree->rightnode, visit)){return 0;}exit(-1);}exit(-1);}exit(-1);}}int inorder(btree *tree, int (*visit)(elemType)){if(tree == NULL){return 0;} else {if(!inorder(tree->leftnode, visit)){if(!visit(tree->data)){if(!inorder(tree->rightnode, visit)){return 0;}exit(-1);}exit(-1);}exit(-1);}}int postorder(btree *tree, int (*visit)(elemType)){if(tree == NULL)return 0;else {if(!postorder(tree->leftnode, visit)){if(!postorder(tree->rightnode, visit)){if(!visit(tree->data)){return 0;}exit(-1);}exit(-1);}exit(-1);}}int visit(elemType elem){printf("\t%c", elem);return 0;}int main(void){btree *tree=NULL;createnode(&tree);printf("Pre order:");preorder(tree, visit);printf("In order:");inorder(tree, visit);printf("Post order:");postorder(tree, visit);return 0;}这个程序我也试了,不能通过的!你用的什么编译器啊?vc2003的啊,gcc 也可以编译通过的啊,语法肯定没问题。typedef char elemType;typedef struct btree{elemType data;struct btree *leftnode, *rightnode;} btree;btree *root=NULL,*currnode=NULL,*prenode=NULL,*newnode;void createtree(void){ if(root==NULL) { root=(btree*)malloc(sizeof(btree)); root->leftnode=NULL; root->rightnode=NULL; } else { prenode=NULL currnode=root; newnode=(btree*)malloc(sizeof(btree)); newnode->data=ch; newnode->leftnode=NULL; newnode->rightnode=NULL; while(currnode!=NULL) { if(newnode->data>currnode->data) { prenode=currnode; currnode=currnode->leftnode; } else { prenode=currnode; currnode=currnode->rightnode; } } if(newnode->data>=renode->data) { prenode->rightnode=newnode; } else { prenode->leftnode=newnode; } }} int inorder(btree *tree){ if(tree==NULL) return 0; inorder(tree->lefnode); visit(tree); inorder(tree->rightnode);}int postord(btree *tree){ if(tree==NULL) return 0; inorder(tree->lefnode); inorder(tree->rightnode); visit(tree);}随手写的可能有错,但是大概是这样的,如果你是初学这,不要看严蔚敏的《数据结构》,递归有时候不能理解数据结构的本质,我以为只有国腾还在用这个教材。exit(-1);是做什么的。改成break或者continue.To ywchen2000(灌水大帝:努力奋斗) 我的想法是 实现一般的二叉树, 输入的数据是完全二叉树的节点排列形式的, 不是那种二叉排序树的形式。To thomaslw(void): 这是递归算法, 里面又没有 for 、while 的循环, 好像没必要用 break, continue 吧?对哈。不过exit(-1);有必要吗完全二叉树:也是这样的写法的,只不过输入的数据能让二叉树变成完全二叉树
这里的都是递归算法啊 试试非递归看看就是 建二叉树的时候没有其他的办法吗递归创建 二叉树 好像算是一个基本的算法吧,先搞定递归的,然后在看非递归的呵呵,我认为应该先非递归才递归,这样你才能理解从单链表到二叉到跳跃表的进化呀.数据结构都是随便用的怎么用爽就怎么用没有必要拘泥于形式管他是什么结构呢我喜欢怎么用就怎么用高效的解决问题才是硬道理。非递归的要么是算法上的非递归要么就是栈模拟递归只不过表现形式不同递归的是系统在用栈模拟非递归的是自己用栈在模拟int preorder(btree *tree, int (*visit)(elemType)){if(tree == NULL)return 0;visit(tree->data);preorder(tree->leftnode, visit);preorder(tree->rightnode, visit)}#include<iostream.h>#include<stdlib.h>struct btnode{ char data; struct btnode *lchild, *rchild;};void del(btnode *bt){ if(bt) { del(bt->lchild); del(bt->rchild); delete bt; }}btnode *creat(void){ char ch; btnode *t = NULL; cout << "input 0 to end the binary tree" << endl; cin >> ch; if(ch == '0') return 0; t = new btnode; t->data = ch; t->lchild = creat(); t->rchild = creat(); return (t);}void Pre(btnode * p){ if(!p) cout << "null tree!" << endl; else { cout << p->data << endl << "lchild " << endl; Pre(p->lchild); cout << "rchild" << endl; Pre(p->rchild); }}void in(btnode * p){ if(!p) cout << "null tree!" << endl; else { in(p->lchild); cout << p->data << endl ; in(p->rchild); }}void post(btnode * p){ if(!p) cout << "null tree!" << endl; else { post(p->lchild); post(p->rchild); cout << p->data << endl << endl; }}main(){ btnode *tree, *tmp; tree = creat(); tmp=tree; cout<<"the pre order!"<<endl<<endl; Pre(tree); cout<<endl; cout<<"the in order !"<<endl<<endl; in(tree); cout<<endl; cout<<"the post order !"<<endl<<endl; post(tree); tree=tmp; del(tree); system("PAUSE");}多用些测试数据 用vs单步跟踪嘛。讨论些这些没的价值嘛。谢谢大家。问题搞定,我要结贴了。
标签: