二叉树的遍历 不知道哪里有问题 求指点
2013-10-04 16:19
204 查看
#include<iostream>
using namespace std;
struct BiNode
{
char data;
BiNode *lchild,*rchild;
};
struct element
{
BiNode *ptr;
int flag;
};
class BiTree
{
public:
BiTree(){root=NULL;}
BiTree(BiNode *root);
// ~BiTree();
void preorder(BiNode *root);
void inorder(BiNode *root);
void postorder(BiNode *root);
void levelorder(BiNode *root);
//void release(BiNode *root);
private:
BiNode *root;
void creat(BiNode *root);
};
void BiTree::preorder(BiNode *root)
{
int top=-1;
BiNode *s=new BiNode;
while(root!=NULL||top!=-1)
{
while(root!=NULL)
{
cout<<root->data;
s[++top]=*root;
root=root->lchild;
}
if(top!=-1)
{
*root=s[top--];
root=root->rchild;
}
}
}
void BiTree::inorder(BiNode *root)
{
int top=-1;
BiNode *s=new BiNode;
while(root!=NULL||top!=-1)
{
while(root!=NULL)
{
s[++top]=*root;
root=root->lchild;
}
if(top!=-1)
{
*root=s[top--];
cout<<root->data;
root=root->rchild;
}
}
}
void BiTree::postorder(BiNode *root)
{
int top=-1;
element *s=new element;
while(root!=NULL||top!=-1)
{
while(root!=NULL)
{
top++;
s[top].ptr=root;
s[top].flag=1;
root=root->lchild;
}
while(top!=-1&&s[top].flag==2)
{
root=s[top--].ptr;
cout<<root->data;
}
if(top!=-1)
{
s[top].flag=2;
root=s[top].ptr->rchild;
}
}
}
/*void BiTree::levelorder(BiTree *root)
{
int front=rear=0;
if(root==NULL)return ;
BiNode *Q=new BiNode;
Q[++rear]=root;
BiNode *q;
while(front!=rear)
{
q=Q[++front];
cout<<q->data;
if(q->lchild!=NULL)
Q[++rear]=q->lchild;
if(q->rchild!=NULL)
Q[++rear]=q->rchild;
}
}
*/
void BiTree::creat(BiNode *root)
{
char ch;
cin>>ch;
if(ch=='#')
root=NULL;
else
{
root=new BiNode;
root->data=ch;
creat(root->lchild);
creat(root->rchild);
}
}
BiTree::BiTree(BiNode *root)
{
creat(root);
}
/*void BiTree::release(BiNode *root)
{
if(root!=NULL)
{
release(root->lchild);
release(root->rchild);
delete root;
}
}
/*BiTree::~BiTree(BiNode *root)
{
release(root);
}*/
int main()
{
BiNode *q=new BiNode;
BiTree s(q);
s.inorder(q);
return 0;
}
using namespace std;
struct BiNode
{
char data;
BiNode *lchild,*rchild;
};
struct element
{
BiNode *ptr;
int flag;
};
class BiTree
{
public:
BiTree(){root=NULL;}
BiTree(BiNode *root);
// ~BiTree();
void preorder(BiNode *root);
void inorder(BiNode *root);
void postorder(BiNode *root);
void levelorder(BiNode *root);
//void release(BiNode *root);
private:
BiNode *root;
void creat(BiNode *root);
};
void BiTree::preorder(BiNode *root)
{
int top=-1;
BiNode *s=new BiNode;
while(root!=NULL||top!=-1)
{
while(root!=NULL)
{
cout<<root->data;
s[++top]=*root;
root=root->lchild;
}
if(top!=-1)
{
*root=s[top--];
root=root->rchild;
}
}
}
void BiTree::inorder(BiNode *root)
{
int top=-1;
BiNode *s=new BiNode;
while(root!=NULL||top!=-1)
{
while(root!=NULL)
{
s[++top]=*root;
root=root->lchild;
}
if(top!=-1)
{
*root=s[top--];
cout<<root->data;
root=root->rchild;
}
}
}
void BiTree::postorder(BiNode *root)
{
int top=-1;
element *s=new element;
while(root!=NULL||top!=-1)
{
while(root!=NULL)
{
top++;
s[top].ptr=root;
s[top].flag=1;
root=root->lchild;
}
while(top!=-1&&s[top].flag==2)
{
root=s[top--].ptr;
cout<<root->data;
}
if(top!=-1)
{
s[top].flag=2;
root=s[top].ptr->rchild;
}
}
}
/*void BiTree::levelorder(BiTree *root)
{
int front=rear=0;
if(root==NULL)return ;
BiNode *Q=new BiNode;
Q[++rear]=root;
BiNode *q;
while(front!=rear)
{
q=Q[++front];
cout<<q->data;
if(q->lchild!=NULL)
Q[++rear]=q->lchild;
if(q->rchild!=NULL)
Q[++rear]=q->rchild;
}
}
*/
void BiTree::creat(BiNode *root)
{
char ch;
cin>>ch;
if(ch=='#')
root=NULL;
else
{
root=new BiNode;
root->data=ch;
creat(root->lchild);
creat(root->rchild);
}
}
BiTree::BiTree(BiNode *root)
{
creat(root);
}
/*void BiTree::release(BiNode *root)
{
if(root!=NULL)
{
release(root->lchild);
release(root->rchild);
delete root;
}
}
/*BiTree::~BiTree(BiNode *root)
{
release(root);
}*/
int main()
{
BiNode *q=new BiNode;
BiTree s(q);
s.inorder(q);
return 0;
}
相关文章推荐
- 二叉树的先序、中序、后序遍历(删除树有问题)
- 做了个超级简单的网页,不过不知道哪里有问题
- 代码有警告不知道哪里有问题!
- 在web页面上放了一些服务器按钮,在各个按钮的单击事件中都有代码!可不知道哪里出问题了,怎么操作都不能触发这些事件,好象代码一点都不执行!根本没办法调试!请高手指点一二!谢谢!
- 读取文件中的数据出错,不知道哪里有问题
- 今天对于图又有了更深的理解,可是还是有问题,那个非递归的深度优先不知道怎么的,好像不能够完全遍历。有点郁闷呀。
- ~二叉树的非递归遍历操作~
- 二叉树的建立和遍历算法 - 数据结构和算法47
- 二叉树非递归遍历
- 二叉树递归遍历和非递归遍历
- 二叉树的递归与非递归遍历实现
- 2013-04-06 二叉树的创建,遍历,统计度为2的节点的个数。
- 二叉树的后序遍历
- 二叉树的前序,中序,后序遍历Java实现
- 二叉树的遍历有几种方式?
- 二叉树的遍历
- [树]二叉树的建立以及三种遍历
- java实现泛型二叉树及其各种遍历
- 二叉树实现(构造,遍历)-java
- 数据结构 c语言实现 二叉树的层次遍历(linux下实现)