现在的位置: 首页 > 综合 > 正文

二叉数操作

2019年04月25日 ⁄ 综合 ⁄ 共 3086字 ⁄ 字号 评论关闭

void insertbitree(bitree *head,intsource)/*向以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小于头结点,那么插入头结点的左孩子,否则插入右孩子,递
归向下,直到找到空位置为止*/
  

 

/*用SOURCE为首地址,LEN为长度的一段空间中的内容建立一棵二叉数*/
bitree *createbitree(int*source,intlen)
{
  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);
}/*递归查找一个节点的妈妈*/

抱歉!评论已关闭.