二叉树的相关操作(一)

1.查找二叉树中值为x的结点

void Search(BiTree T,BiTree *&q,int x){
   if(T==NULL){
      return false;
    }else{
       Search(T->lchild,q,x);
       Search(T->rchild,q,x);
    }
}

2.计算二叉树的节点总数

//法一  全局变量记录节点数
int n=0;
void Count(BiTree L){
   if(T!=NULL){
       n++;
       Count(T->lchild);
      Count (T->rchild);
    }
}
//法二  局部变量 
void Count(BiTree L){
    int n1,n2;
    if(T==NULL){
      return false;
    }else {
      n1=Count(T->lchild);
      n2=Count(T->rchild);
      return n1+n2+1;//左子树节点数+右子树节点数+根节点
    }
}

 3.计算二叉树叶子节点的个数

//法一
int n;
void Countleaf(BiTree L){
   if(L->lchild==NULL&&L->rchild==NULL){
       n++;
     }else{
        Countleaf(L->lchild);
        Countleaf(L->rchild);
     }
}
//法二
void Countleaf(BiTree L){
   int n1,n2;
    if(L==NULL){
        return false;
     }else{
     if(L->lchild==NULL&&L->rchild==NULL){
         return 1;
      }else{
         n1=Countleaf(L->lchild);
         n2=Countleaf(L->rchild);
      }
   }
}

4.计算二叉树双分支节点的个数

//需要判断根节点是否为双分支节点
void Count(BiTree L){
     int n1=0,n2=0;
     if(L==NULL){             //根节点为空
       return 0;
     }else
     if(L->lchild&&L->rchild){  //根节点为双分支
        n1=Count(L->lchild);
        n2=Count(L->rchild);
        return n1+n2+1;
     }else{                     //根节点为单分支节点
        n1=Count(L->lchild);
        n2=Count(L->rchild);
        return n1+n2;
    }
  }
}

5.计算二叉树单分支节点个数

//需要判断根节点是否为单分支节点
void Count(BiTree L){
     int n1=0,n2=0;
     if(L==NULL){             //根节点为空
       return 0;
     }else
     if(L->lchild&&L->rchild==NULL)||(L->rchild&&L->lchild==NULL){  //根节点为单分支
        n1=Count(L->lchild);
        n2=Count(L->rchild);
        return n1+n2+1;
     }else{                     //根节点为双分支节点
        n1=Count(L->lchild);
        n2=Count(L->rchild);
        return n1+n2;
    }
  }
}
//计算树的深度
void TreeDepth(BiTree L){
    int n1=0,n2=0;
    if(L==NULL){
       return 0;
  }else{
     n1=TreeDepth(L->lchild);
     n2=TreeDepth(L->rchild);
     return n1>n2?n1+1:n2+1;  //Max(左子树,右子树)的深度+根节点=深度
   }
}

6.交换二叉树的所有左右节点

//交换左右指正域中的内容
void Swap(BiTree &L){
    if(L!=NULL){
      BiTNode *temp=L->lchild;
      L->lchild=L->rchild;
      L->rchild=temp;
      Swap(L->lchild);
      Swap(L->rchild);
   }
}

7. 求二叉树中值为x的节点的层次号

void NodeDepth(BiTree L,ElemType x,int level){
    if(L!=NULL){
      if(L->data==x){
           printf("x的层数为:%d",level);
           NodeDepth(L->lchild,x,level+1);
           NodeDepth(L->rchild,x,level+1);
        }
    }
}
void Func(BiTree L,ElemType x){
    NodeDepth(L,x,1);             //level初始为1 根节点
}

8.二叉树层次遍历(队列)

void LevelOrder(BiTree T){
    Queue Q;             //定义一个队列
    InitQueue(Q);        //初始化队列
    BiTree *p=T;         //定义一个指针,指向二叉树根节点
    EnQueue(Q,p);
    while(!IsEmpty(Q)){
      DeQueue(Q,p);    //出队,p接收出队元素
      printf("%d",p->data);
      if(p->lchild){
         EnQueue(Q,p->lchild);
       }else 
      if(p->rchild){
           EnQueue(Q,p->rchild);
     }
   }
}

9.反转层次遍历序列(栈)

Void ReverseLevelOrder(BiTree T){
     Queue(Q);
     Stack(s);//      定义一个栈
     InitStack(s);//   初始化栈
     InitQueue(Q);
     BiTree *p=T;
     EnQueue(Q,p);

     while(!IsEmpty(Q)){  //队列不为空
         DeQueue(Q,p);  //出队
         push(s,p);    //入栈
         if(p->lchild){ 
         Queue(Q,p->lchild);
         }else 
         i(p->rchild){
         Queue(p->rchild);
       }
     }
      while(!IsEmpty(s)){ //栈不为空
        pop(s,p);          //出栈
        printf("%d",p);    //打印出栈序列
     }
} 
  • 13
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lucky77.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值