欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
平衡二叉樹(shù)源碼|平衡二叉樹(shù),源碼


#include <stdio.h>

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

typedef struct treequeuetype
 {
  int head;
  int tail;
  bitree *items[1000];
 }treequeue;/*定義一個(gè)隊列,后面的平衡調整要用層序遍歷,于是要用這個(gè)隊列*/

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長(cháng)度的一塊空間,初始化內存方便*/

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++;
   }/*抽出一個(gè)數放入NUM臨時(shí)空間中*/
   else
   {
    num=p;
    *dst=atoi(num);
    len++;
    fillmemory(num,20,0);
    dst++;
    t=*temp;
    temp++;
   }/*將NUM中的數字轉化出來(lái)存入DST中*/
  }
  num=p;
  *dst=atoi(num);
  len++;
  fillmemory(num,20,0);
  dst++;
  t=*temp;
  temp++;
  return len;
 }/*處理最后一個(gè)數字*/

/*****唉,寫(xiě)上面的函數時(shí)都兩個(gè)月沒(méi)寫(xiě)過(guò)C了,所以可能上面的函數條理相當差的說(shuō)*****/
void insertbitree(bitree *head,int source)/*向以HEAD為頭結點(diǎn)的排序二叉樹(shù)中插入一個(gè)以SOURCE為內容的點(diǎn)*/
 {
  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小于頭結點(diǎn),那么插入頭結點(diǎn)的左孩子,否則插入右孩子,遞歸向下,直到找到空位置為止*/
bitree *createbitree(int *source,int len)/*用SOURCE為首地址,LEN為長(cháng)度的一段空間中的內容建立一棵二叉數*/
 {
  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);/*這個(gè)函數很強大,用心體會(huì )吧,哈哈哈*/
    source++;
    len--;
  }
  return head;
 }
int getdepth(bitree *head)/*求排序二叉樹(shù)的深度*/
 {
  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兩個(gè)遞歸出口,然后分別求左右子樹(shù)的深度并返回大者*/
void addbdegree(bitree *head)/*為排序二叉樹(shù)追加平衡因子*/
 {
  if(head==NULL)return;
  else
  {
   head->bdegree=getdepth(head->lchild)-getdepth(head->rchild);
   addbdegree(head->lchild);
   addbdegree(head->rchild);
  }
 }
bitree *leveldetect(bitree *head)/*以層序遍歷為基本框架,檢查"特殊"點(diǎn)*/
 {
  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);
 }/*遞歸查找一個(gè)節點(diǎn)的媽媽*/
bitree *createsuperbitree(int *source,int len)/*不消說(shuō)了,建立平衡二叉樹(shù)*/
 {
  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);
 } 

 


本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
二叉樹(shù)的基礎問(wèn)題
編寫(xiě)算法,按樹(shù)狀打印二叉樹(shù)的算法
二叉樹(shù)的先序遍歷
二叉樹(shù)各種操作的C語(yǔ)言實(shí)現
二叉樹(shù)的創(chuàng )建及遍歷(遞歸與非遞歸)
二叉樹(shù)遍歷非遞歸算法
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久