您的位置:首页 > 其它

二叉树、二叉链表

2016-05-28 11:23 253 查看
</pre><pre name="code" class="cpp">#include <iostream>
using namespace std;
typedef char elemtype;
int n=0;
typedef struct BiNode
{
elemtype data;
BiNode *lchild, *rchild;
}BiNode;
class BiTree
{
public:
BiTree(){root=Creat();}
~BiTree(){Release(root);}
BiNode * Getroot();
void PreOrder(BiNode *root); //前序遍历递归算法
void PreOrder2(BiNode *root);  //前序遍历非递归算法
void InOrder(BiNode *root);   //中序遍历递归算法
void InOrder2(BiNode *root); //中序遍历非递归算法
void PostOrder(BiNode *root); //后序遍历递归算法
void PostOrder2(BiNode *root); //后序遍历非递归算法
void LeverOrder(BiNode *root); //层序遍历
int  Depth(BiNode *root)  ; //求深度
void Count(BiNode *root); //求二叉树的结点个数
private:
BiNode *root;
BiNode* Creat();
void Release(BiNode *root);
};

BiNode* BiTree::Getroot( )
{
return root;
}
void   BiTree::PreOrder(BiNode *root)
{
if (root ==NULL)  return;    //递归调用的结束条件
else {
cout<<root->data;         //访问根结点bt的数据域
PreOrder(root->lchild);    //前序递归遍历bt的左子树
PreOrder(root->rchild);    //前序递归遍历bt的右子树
}
}
void BiTree::PreOrder2(BiNode *root)
{
int top= -1;      //采用顺序栈,并假定不会发生上溢
BiNode * s[100];
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)//
{
if (root==NULL) return;
else {
InOrder(root->lchild);
cout<<root->data;
InOrder(root->rchild);
}
}
void BiTree::InOrder2(BiNode *root)
{
int top=-1;  //采用顺序栈,并假定不会发生上溢
BiNode* s[100];
while (root !=NULL||top!=-1)
{
while(root!=NULL)
{
s[++top]=root;  //将根指针root入栈
root=root->lchild;
}
if (top!=-1)          //栈非空
{
root=s[top--];
cout<<root->data;
root=root->rchild;
}
}
}
void BiTree::PostOrder(BiNode *root)
{
if (root==NULL) return;
else {
PostOrder(root->lchild);
PostOrder(root->rchild);
cout<<root->data;
}
}

void BiTree::LeverOrder(BiNode *root)
{
int front,rear;
BiNode * Q[100];
BiNode *q;
front=rear=-1;    //采用顺序队列,并假定不会产生上溢
if(root==NULL) return ;
Q[++rear]=root;
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;
}
}

/*
*前置条件:空二叉树
*输    入:数据ch;
*功    能:初始化一棵二叉树,构造函数调用
*输    出:无
*后置条件:产生一棵二叉树
*/
BiNode* BiTree::Creat()
{
BiNode* root;
elemtype ch;
cout<<"请输入创建一棵二叉树的结点数据"<<endl;
cin>>ch;
if (ch=='#') root = NULL;
else{
root = new BiNode;       //生成一个结点
root->data=ch;
root->lchild = Creat();    //递归建立左子树
root->rchild = Creat();    //递归建立右子树
}
return root;
}

void BiTree::Count(BiNode *root)  //n为全局量并已初始化为0
{
if (root) {
Count(root->lchild);
n++;
Count(root->rchild);
}
}
int  BiTree::Depth(BiNode *root)  //求二叉树深度
{
int hl ,hr;
if (root==NULL) return 0;
else {
hl= Depth(root->lchild);
hr= Depth(root ->rchild);
return max(hl, hr)+1;
}
}
void BiTree::Release(BiNode *root)
{
if (root!=NULL)
{
Release(root->lchild); //释放左子树
Release(root->rchild); //释放右子树
delete root;    //释放根结点
}
}

void main()
{
BiTree bt;
//	cout<<"请输入二叉树序列,如:AB#D##C##"<<endl;
BiNode *root=bt.Getroot();
cout<<"树的前序遍历序列为:"<<endl;
bt.PreOrder(root);
cout<<endl;
cout<<"------中序遍历------ "<<endl;
bt.InOrder(root);
cout<<endl;
cout<<"------后序遍历------ "<<endl;
bt.PostOrder(root);
cout<<endl;
cout<<"------层序遍历------ "<<endl;
bt.LeverOrder(root);
cout<<endl;
cout<<bt.Depth(root)<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: