ADT-二叉树

来源:互联网 发布:大麦户 2016 系统源码 编辑:程序博客网 时间:2024/06/08 02:42
#include<stdlib.h>#include<stdio.h>#include<malloc.h>//函数状态码定义#define TRUE       1#define FALSE      0#define OK         1#define ERROR      0#define OVERFLOW   -1#define INFEASIBLE -2#define NULL  0typedef int Status;//二叉链表存储结构定义typedef int TElemType;typedef struct BiTNode{    TElemType data;    struct BiTNode  *lchild, *rchild; } BiTNode, *BiTree;//构造空树Status InitBiTree(BiTree &T)  {      T=NULL;      return OK;  }  void DestroyBiTree(BiTree &T)  {      /*初始条件:二叉树T存在。操作结果:销毁二叉树T*/      if(T)/*非空树*/      {          if(T->lchild)/*有左孩子*/              DestroyBiTree(T->lchild);/*销毁左孩子子树*/          if(T->rchild)/*有右孩子*/              DestroyBiTree(T->rchild);/*销毁右孩子子树*/          free(T);/*释放根结点*/          T=NULL;/*空指针赋0*/      }  }  Status DeleteChild(BiTree p,int LR){    if(p)    {        if(LR==0)            DestroyBiTree(p->lchild);        else            DestroyBiTree(p->rchild);        return OK;    }    return ERROR;}Status CreateBiTree(BiTree &T){    //按先序次序输入二叉树中结点的值(一个字符),    //空格字符表示空树,构造二叉链表表示的二叉树T。    char ch;    ch = getchar();//scanf("%c",&ch);    if(ch == ' ')        T = NULL;    else    {        if(!(T = (BiTNode*)malloc(sizeof(BiTNode))))            exit(OVERFLOW);        T->data = ch;                //生成根结点        CreateBiTree(T->lchild);     //构造左子树        CreateBiTree(T->rchild);     //构造右子树    }    return OK;}  /*                  按层次顺序建立一棵二叉树 :队列         */Status LevelCreateBiTree(BiTree &T){    BiTree p,s;//p指向父亲结点,s指向孩子结点    Queue BiNodeQueue;    char ch;    ch=getchar();    if(ch==' ')    {        return NULL;    }    T=(BiTNode*)malloc(sizeof(BiTNode)); //生成根结点    T->data=ch;    EnQueue(BiNodeQueue,T); //用队列实现层次遍历    while(!BiNodeQueue.Empty())    {        DeQueue(BiNodeQueue,p);        ch=getchar(); //为了简化操作,分别对左右子结点进行赋值。        if(ch!=' ')//子树不空则进队列进行扩充。下同        {            s=(BiTNode*)malloc(sizeof(BiTNode));            s->data=ch;            p->lchild=s;            EnQueue(BiNodeQueue,s);        }        else        {            p->lchild=NULL;        }        ch=getchar();        if(ch!=' ')        {            s=(BiTNode*)malloc(sizeof(BiTNode));            s->data=ch;            p->rchild=s;            EnQueue(BiNodeQueue,s);        }        else        {            p->rchild=NULL;        }    }    return OK;} Status Root(SqBiTree T,TElemType *e){    /* 初始条件: 二叉树T存在 */    /* 操作结果:  当T不空,用e返回T的根,返回OK;否则返回ERROR,e无定义 */    if(BiTreeEmpty(T))        return NULL;    else        return T->data;}Status BiTreeEmpty(BiTree T) /*初始条件:二叉树T存在*/  {      /*操作结果:若T为空二叉树,则返回TRUE,否则FALSE*/      if(T!=NULL)   return OK;      else    return TRUE;  }  Status TreeDepth(BiTree T){//递归法求树的深度if(T==NULL)d=0;else{   d1=TreeDepth(T->lchild1);d2=TreeDepth(T->rchild);      if(d1>d2)d=d1+1;      else d=d2+1;}return d;}Status Value(BiTree p)  {      /*初始条件:二叉树T存在,p指向T中某个结点*//*操作结果:返回p所指结点的值*/      return p->data;  }  void Assign(BiTree p,TElemType value)  {      /*给p所指结点赋值为value*/p->data=value;  } BiTree Point(BiTree T,TElemType s)//返回二叉树T中指向元素值为S的结点指针{    LinkQueue q;    QElemType a;    if(T)    {        InitQueue(q);//初始化队列        EnQueue(q,T);//根指针入队        while(!QueueEmpty(q))//队不空        {            DeQueue(q,a);//出队,队列元素赋给e            if(a->data==s)//a所指结点为的值为s                return a;            if(a->lchild)//有左孩子                EnQueue(q,a->lchild);//入队左孩子            if(a->rchild)//有右孩子                EnQueue(q,a->rchild);//入队右孩子        }    }    return NULL;}TElemType LeftChild(BiTree T,TElemType e){    //返回e的左孩子    BiTree a;    if(T)    {        a=Point(T,e);//a是指向结点e的指针        if(a&&a->lchild)            return a->lchild->data;    }    return NULL;}TElemType RightChild(BiTree T,TElemType e){    BiTree a;    if(T)    {        a=Point(T,e);//a是指向结点e的指针        if(a&&a->rchild)//T中存在结点e并且e存在右孩子            return a->rchild->data;    }    return NULL;}TElemType Parent(BiTree T,TElemType e){    //返回双亲    LinkQueue q;    QElemType a;    if(T)    {        InitQueue(q);        EnQueue(q,T);//树根入队列        while(!QueueEmpty(q))//队不空        {            DeQueue(q,a);//出队,队列元素赋给a            if(a->lchild&&a->lchild->data==e||a->rchild&&a->rchild->data==e)//找到e                return a->data;            else            {                if(a->lchild)                    EnQueue(q,a->lchild);//入队列左孩子                if(a->rchild)                    EnQueue(q,a->rchild);//入队列右孩子            }        }    }    return NULL;}TElemType LeftSibling(BiTree T,TElemType e){    //返回左兄弟    TElemType a;    BiTree p;    if(T)    {        a=Parent(T,e);//a为e的双亲        if(a!=NULL)        {            p=Point(T,a);//p指向结点a的指针            if(p->lchild&&p->rchild&&p->rchild->data==e)//p存在左右孩子而且右孩子是e                return p->lchild->data;        }    }    return NULL;}TElemType RightSibling(BiTree T,TElemType e){    //返回右孩子    TElemType a;    BiTree p;    if(T)    {        a=Parent(T,e);//a为e的双亲        if(a!=NULL)        {            p=Point(T,a);//p为指向结点的a的指针            if(p->lchild&&p->rchild&&p->lchild->data==e)                return p->lchild->data;        }    }    return NULL;}Status InsertChild(BiTree p,int LR,BiTree c){    //根据LR为0或1,插入C为T中P所指结点的左或右子树,P所结点的原有左或右子树则成为C的右子树    if(p)    {        if(LR==0)//把二叉树C插入P所指结点的子树        {            c->rchild=p->lchild;//p所结点的原有左子树成为C的右子树            p->lchild=c;//二叉树成为P的左子树        }        else        {            c->rchild=p->rchild;//p指结点的原有右子树成为C的右子树            p->rchild=c;        }        return OK;    }    return ERROR;}void PreOrderTraverse(BiTree T,Status(*visit)(TElemTyp e)){      /*初始条件:二叉树T存在,Visit是对结点操作的应用函数。算法6.1,有改动*//*操作结果:先序递归遍历T,对每个结点调用函数Visit一次且仅一次*/      if(T)/*T不空*/      {          visit(T->data);/*先访问根结点*/          PreOrderTraverse(T->lchild,visit);/*再先序遍历左子树*/          PreOrderTraverse(T->rchild,visit);/*最后先序遍历右子树*/      }  }  Status PreOrderTraverse(BiTree T){    stack S;    InitStack(S);     BiTree p=T;  //p指向当前访问的结点         while(p||!StackEmpty(S))    {        if(p)        {            printf("%c",p->data);            Push(S,p);            p=p->lchild;        }        else        {            Pop(S,p);            p=p->rchild;        }    }    return OK;}  Status InOrderTraverse(BiTree T,Status(*visit)(TElemType))  {      /*初始条件:二叉树T存在,Visit是对结点操作的应用函数*/      /*操作结果:中序递归遍历T,对每个结点调用函数Visit一次且仅一次*/      if(T)      {          InOrderTraverse(T->lchild,visit);/*先中序遍历左子树*/          visit(T->data);/*再访问根结点*/          InOrderTraverse(T->rchild,visit);/*最后中序遍历右子树*/          return OK;      }  }  Status InOrderTraverse(BiTree T) {      stack S;    BiTree p;    InitStack(S);  Push(S, T);      while (!StackEmpty(S))     {        while (GetTop(S, p) && p)             Push(S, p->lchild);   // 向左走到尽头        Pop(S, p);                // 空指针退栈(叶子的左孩子)        if (!StackEmpty(S))         {   // 访问结点,向右一步            Pop(S, p);              printf("%d",p->data);   //当前根结点            Push(S, p->rchild);        }    }    return OK;} Status PostOrderTraverse(BiTree T,Status(*visit)(TElemType))  {      /*初始条件:二叉树T存在,Visit是对结点操作的应用函数*         /*操作结果:后序递归遍历T,对每个结点调用函数Visit一次且仅一次*/      if(T)/*T不空*/      {          PostOrderTraverse(T->lchild,visit);/*先后序遍历左子树*/          PostOrderTraverse(T->rchild,visit);/*再后序遍历右子树*/          visit(T->data);/*最后访问根结点*/          return OK;      }  }Status PostOrderTraverse(BiTree T) {     stack S;    InitStack(S);    BiTree p=T,pre=NULL;    while ( p || !StackEmpty(S))     {         if (p)         {             Push(S,p);             p = p->left;         }         else        {             Pop(S,p);             if (p->right!=NULL && pre!=p->right)            { //pre指向上次访问的右结点,避免再次访问                p=p->right;             }             else            {                 printf("%d",p->data);                pre=p;                p=NULL;             }         }     } }void LevelOrderTraverse(BiTree T){//二叉树层次遍历    Queue BiNodeQueue;    BiTree p=T;    EnQueue(BiNodeQueue,p);    while(!BiNodeQueue.Empty())    {        DeQueue(BiNodeQueue,p);        if(p)        {            printf("%c",p->data);            EnQueue(BiNodeQueue,p->lchild);            EnQueue(BiNodeQueue,p->rchild);        }    }}  //交换左右子树void Exchange(BiTree T){    BiTree temp;    if(T)    {        Exchange1(T->lchild);        Exchange1(T->rchild);        temp=T->lchild;        T->lchild=T->rchild;        T->rchild=temp;    }}//二叉树的复制void CopyBiTree(BiTree &P,BiTree Q){    BiTree lp,rp;    if(!Q)        P=NULL;    else    {        CopyBiTree(lp,Q->lchild);        CopyBiTree(rp,Q->rchild);        P=(BiTree)malloc(sizeof(BiNode));        P->data=Q->data;        P->lchild=lp;        P->rchild=rp;    }}//二叉树中叶子结点个数int LeavesNum(BiTree T){    if(T)    {        if(T->lchild==NULL&&T->rchild==NULL)        {            return 1;        }        return LeavesNum(T->lchild)+LeavesNum(T->rchild);    }    return 0;}//非递归求法求叶子数int LeavesNum(BiTree T){    int count=0;    stack S;    InitStack(S);    BiTree p=T;    while(p||!StackEmpty(S))    {        if(p)        {            Push(S,p);            if(p->lchild==NULL&&p->rchild==NULL)            {                count++;            }            p=p->lchild;        }        else        {            Pop(S,p);            p=p->rchild;        }    }    return count;} //结点个数int  Num_Of_Node(BiTree t){    if(t==NULL)        return 0 ;    else        return Num_Of_Node(t->lchild)+Num_Of_Node(t->rchild)+1;}void destroyBiTree(BiTree & T) //删除树  {      if (T)      {          destroyBiTree(T->lchild);          destroyBiTree(T->rchild);          free(T);          T = NULL;      }  }