论文网
English Papers
万事OK网
发表论文
 
 首页 > IT文章 > 程序设计 >
平衡二叉树源码

[科技论文网] http://www.scipapers.com    2007-12-01  

    平衡二叉树源码

    #include <stdio.h>

    typedef struct bitreetype
     {
      int item;
      int bdegree;/*平衡因子,左子树深度-右子树深度*/
      struct bitreetype *lchild;
      struct bitreetype *rchild;
     }bitree;

    typedef struct treequeuetype
     {
      int head;
      int tail;
      bitree *items[1000];
     }treequeue;/*定义一个队列,后面的平衡调整要用层序遍历,于是要用这个队列*/

    void resetqueue(treequeue *queue)
     {
      queue->head=-1;
      queue->tail=-1;
      return;
     }/*把队列清空*/
    void inqueue(treequeue *queue,bitree *element)
     {
      queue->tail++;
      queue->items[queue->tail]=element;
     }/*入队列*/
    bitree *outqueue(treequeue *queue)
     {
      queue->head++;
      return queue->items[queue->head];
     }/*出队列*/
    int isqueueempty(treequeue *queue)
     {
      if(queue->head==queue->tail)
      return 1;
      else
      return 0;
     }/*判断队列是否为空*/
    void fillmemory(char *source,int len,char content)
     {
      while(len)
      {
       source=source+len;
       *source=content;
       source=source-len;
       len--;
      }
      *source=0;
     }/*用CONTENT的内容去FILL以SOURCE为首,LEN长度的一块空间,初始化内存方便*/

    int getnums(int *dst)/*输入字符串并把字符串转化为一串数存入DST指向的内存中去,我们用它采集原始数据*/
     {
      char *temp,*num,*p,t;
      int len=0;
      temp=(char *)malloc(1000*sizeof(char));
      num=(char *)malloc(20*sizeof(char));
      p=num;
      fillmemory(temp,1000,0);
      fillmemory(num,20,0);
      scanf("%s",temp);
      t=*temp;
      temp++;
      while(t)
      {
       if(t!=',')
       {
        *num=t;
        num++;
        t=*temp;
        temp++;
       }/*抽出一个数放入NUM临时空间中*/
       else
       {
        num=p;
        *dst=atoi(num);
        len++;
        fillmemory(num,20,0);
        dst++;
        t=*temp;
        temp++;
       }/*将NUM中的数字转化出来存入DST中*/
      }
      num=p;
      *dst=atoi(num);
      len++;
      fillmemory(num,20,0);
      dst++;
      t=*temp;
      temp++;
      return len;
     }/*处理最后一个数字*/

    /*****唉,写上面的函数时都两个月没写过C了,所以可能上面的函数条理相当差的说*****/
    void insertbitree(bitree *head,int source)/*向以HEAD为头结点的排序二叉树中插入一个以SOURCE为内容的点*/
     {
      if(source<=head->item)
      {
       if(head->lchild==NULL)
       {
        head->lchild=(bitree *)malloc(sizeof(bitree));
        head->lchild->item=source;
        head->lchild->lchild=NULL;
        head->lchild->rchild=NULL;
        head->lchild->bdegree=0;
       }
       else
       insertbitree(head->lchild,source);
      }
      else
      {
       if(head->rchild==NULL)
       {
        head->rchild=(bitree *)malloc(sizeof(bitree));
        head->rchild->item=source;
        head->rchild->lchild=NULL;
        head->rchild->rchild=NULL;
        head->rchild->bdegree=0;
       }
       else
       insertbitree(head->rchild,source);
      }
     }/*递归插入的思想:如果SOURCE小于头结点,那么插入头结点的左孩子,否则插入右孩子,递归向下,直到找到空位置为止*/
    bitree *createbitree(int *source,int len)/*用SOURCE为首地址,LEN为长度的一段空间中的内容建立一棵二叉数*/
     {
      int temp;
      bitree *head=NULL;
      head=(bitree *)malloc(sizeof(bitree));
      head->lchild=NULL;
      head->rchild=NULL;
      head->item=*source;
      head->bdegree=0;
      source++;
      len--;
      while(len>0)
      {
       insertbitree(head,*source);/*这个函数很强大,用心体会吧,哈哈哈*/
        source++;
        len--;
      }
      return head;
     }
    int getdepth(bitree *head)/*求排序二叉树的深度*/
     {
      int ltemp,rtemp;
      if(head==NULL)return 0;
      if(head->lchild==NULL && head->rchild==NULL)return 1;
      ltemp=1+getdepth(head->lchild);
      rtemp=1+getdepth(head->rchild);
      if(ltemp>=rtemp)return ltemp;
      else return rtemp;
     }/*递归求深的思想:首先规定好0,1两个递归出口,然后分别求左右子树的深度并返回大者*/
    void addbdegree(bitree *head)/*为排序二叉树追加平衡因子*/
     {
      if(head==NULL)return;
      else
      {
       head->bdegree=getdepth(head->lchild)-getdepth(head->rchild);
       addbdegree(head->lchild);
       addbdegree(head->rchild);
      }
     }
    bitree *leveldetect(bitree *head)/*以层序遍历为基本框架,检查"特殊"点*/
     {
      treequeue *tqueue;
      bitree *temp;
      tqueue=(treequeue *)malloc(sizeof(treequeue));
      resetqueue(tqueue);
      if(head!=NULL)inqueue(tqueue,head);
      while(!isqueueempty(tqueue))
      {
       temp=outqueue(tqueue);
       if(temp->bdegree<=-2 || temp->bdegree>=2)
       {
        if(temp->bdegree==2 && temp->lchild!=NULL && temp->lchild->bdegree==1)
         return temp;
        if(temp->bdegree==2 && temp->lchild!=NULL && temp->lchild->bdegree==-1)
        return temp;
        if(temp->bdegree==-2 && temp->rchild!=NULL && temp->rchild->bdegree==-1)
        return temp; 
        if(temp->bdegree==-2 && temp->rchild!=NULL && temp->rchild->bdegree==1)
        return temp;
       }
       if(temp->lchild!=NULL)inqueue(tqueue,temp->lchild);
       if(temp->rchild!=NULL)inqueue(tqueue,temp->rchild);
      }
      return NULL;
     }/*(2,1),(2,-1),(-2,-1),(-2,1)完美的卡诺图啊!!*/
    bitree *getmother(bitree *head,bitree *child)
     {
      bitree *temp;
      if(head==child)return NULL;
      if(head->lchild==child || head->rchild==child)return head;
      if(head->lchild==NULL || head->rchild==NULL)return NULL;
      if(head->lchild!=NULL)
      {
       temp=getmother(head->lchild,child);
       if(temp!=NULL)return temp;
      }
      return getmother(head->rchild,child);
     }/*递归查找一个节点的妈妈*/
    bitree *createsuperbitree(int *source,int len)/*不消说了,建立平衡二叉树*/
     {
      int itemp;
      bitree *head=NULL;
      bitree *temp=NULL;
      bitree *tmother=NULL;
      bitree *p=NULL;
      bitree *q=NULL;
      head=(bitree *)malloc(sizeof(bitree));
      head->lchild=NULL;
      head->rchild=NULL;
      head->item=*source;
      head->bdegree=0;
      source++;
      len--;
      while(len>0)
      {
       insertbitree(head,*source);
       addbdegree(head);
       temp=leveldetect(head);
       if(temp!=NULL)
       {
        tmother=getmother(head,temp);
        if(temp->bdegree==2 && temp->lchild!=NULL && temp->lchild->bdegree==1)
        {
         p=temp->lchild;
         temp->lchild=p->rchild;
         p->rchild=temp;
         if(tmother!=NULL)
               {
         if(tmother->lchild==temp)tmother->lchild=p;
         else tmother->rchild=p;
               }
         else
         head=p;
        }/*LL*/
        if(temp->bdegree==2 && temp->lchild!=NULL && temp->lchild->bdegree==-1)
        {
         p=temp->lchild->rchild;
         q=temp->lchild;
         q->rchild=p->lchild;
         temp->lchild=p->rchild;
         p->lchild=q;
         p->rchild=temp;
         if(tmother!=NULL)
         {
          if(tmother->lchild==temp)tmother->lchild=p;
          else tmother->rchild=p;
         }
         else
         head=p;
        }/*LR*/
        if(temp->bdegree==-2 && temp->rchild!=NULL && temp->rchild->bdegree==-1)
        {
         p=temp->rchild;
         temp->rchild=p->lchild;
         p->lchild=temp;
         if(tmother!=NULL)
         {
          if(tmother->lchild==temp)tmother->lchild=p;
          else tmother->rchild=p;
         }
         else
         head=p;
        }/*RR*/
        if(temp->bdegree==-2 && temp->rchild!=NULL && temp->rchild->bdegree==1)
        {
         p=temp->rchild->lchild;
         q=temp->rchild;
         temp->rchild=p->lchild;
         q->lchild=p->rchild;
         p->lchild=temp;
         p->rchild=q;
         if(tmother!=NULL)
         {
          if(tmother->lchild==temp)tmother->lchild=p;
          else tmother->rchild=p;
         }
         else
         head=p;
        }/*RL*/
        addbdegree(head);
       }
       source++;
        len--;
      }
      return head;
     }
    main()/*演示*/
     {
      int binums[100],i,len;
      bitree *head,*temp;
      for(i=0;i<=99;i++)binums[i]=0;
      len=getnums(binums);
      head=createsuperbitree(binums,len);
      temp=getmother(head,head->rchild->rchild->rchild);
     } 

        来源:

声明:本文由网友推荐或作者提交,版权归原作者所有,刊登此文仅为传播知识,展示研究成果,提高文章引用率。未经原作者授权,禁止用于任何形式的商业行为。科技论文网倡导尊重知识、尊重劳动、保护原创、知识共享。由于部分论文文章来于网络,文章作者不祥,请相关的原创作者与我们联系,以便加上您的署名。

  
平衡二叉树源码
下面没有链接了     随机算法求圆周率演示程序源代码
最新论文
·[程序设计]平衡二叉树源码
·[程序设计]随机算法求圆周率演示程序源代码
·[程序设计]分治法解决棋盘覆盖问题
·[程序设计]找零钱问题的贪心算法
·[程序设计]C编程实现大数求和程序
·[程序设计]循环冗余校验 CRC的算法分析和程序实现
·[程序设计]AVR单片机CRC校验码的查表与直接生成
·[程序设计]CRC算法原理及C语言实现
·[程序设计]欧几里德算法及其实现
·[程序设计]蚁群算法Python实现
 
 

搜索论文

Google
论文分类

论文网 论文发表网 论文 免费论文网 找论文网 毕业论文 中国论文网 英语论文 百度论文 聘教网 易搜
 免费发布论文    中国论文网 2008版权所有  业务联系:pinjiao@126.com