二叉树、二叉链表

来源:互联网 发布:剑灵女生捏脸数据 编辑:程序博客网 时间:2024/06/05 06:30
</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;}


0 0