编写算法判别给定二叉树是否为完全二叉树_二叉树 ,你是完整的吗?

eaf06931e0591c83a7e4a9832407bb19.png记得点击蓝字关注叉酱哦! de62f042cc8be4bd0d7ddec71b85a2ef.pngTREE 编写函数,判断给定的二叉树是否是完全二叉树 什么是完全二叉树? 一棵深度为k的有n个结点的二叉树 ,对树中的结点按从上至下、从左到右的顺序进行编号,如果编号为i(1≤i≤n)的结点与满二叉树 中编号为i的结点在二叉树中的位置相同,则这棵二叉树称为完全二叉树。 dfd54ba2d59661ea4acdca65a3f8f181.png eb5bad0b5fc1b20160edc5d4e07e73de.png 19269406fc5465f6369ae10ad571a3c8.gif一些二叉树 5024a25027db40fed66c5cbb3f73e4b4.png算法思想:

1、使用队列

2、遍历到任何一个节点,有右孩子没有左孩子直接返回false.

3、不违反2的情况下,当前节点左右两个孩子不全,标记为flag=1,从当前节点开始所有的节点均为叶子节点,即不能有孩子。若有返回false。

b4e14040b2ac9ac0172eb168fc83006d.png

图片说明

样例调试

1.输入

ABD###CE##F##

输出

层次遍历为:

A第1层

BC第2层

DEF第3层

不是完全二叉树

2.输入

ABD##E##C#F##

输出

层次遍历为:

A第1层

BC第2层

DEF第3层

不是完全二叉树

3.输入

ABD##E##CF##G##

输出

层次遍历为:

A第1层

BC第2层

DEFG第3层

是完全二叉树

4.输入

ABCE####D##

输出

ABCE####D##

层次遍历为:

A第1层

BD第2层

C第3层

E第4层

不是完全二叉树

源代码
#include#include#includetypedef char TElemType;#define STACKINITSIZE 256//初次分配空间大小#define STACKINCREMENT 128//空间分配增量大小#define INITQUEUE 20//队列空间大小using namespace std;typedef struct BiTNode{  TElemType         data;//数据元素  struct BiTNode     *lchild,*rchild;//左、右孩子指针 }BiTNode,*BiTree;void CreateBiTree(BiTree &T,TElemType definition){  cin>>definition;   if(definition == '#')  {    T=NULL;  }  else  {    T=(BiTNode*)malloc(sizeof(BiTNode));    T->data=definition;    CreateBiTree(T->lchild,definition);    CreateBiTree(T->rchild,definition);  }}void visit(BiTNode *T){  cout<data<<"";} //队列用于层次遍历typedef struct Queue{  BiTNode *front;//队列头指针  BiTNode *tail;//队列尾指针  int size;//队列空间大小}Queue;//创建队列int InitQueue(Queue &Q){  Q.front = (BiTNode*)malloc(INITQUEUE * sizeof(BiTNode));  if(!Q.front)  {    return 0;  }  Q.tail = Q.front;  Q.size = INITQUEUE;  return 1;}//判断队列是否为空bool EmptyQueue(Queue Q){  if(Q.tail == Q.front)  {    return true;  }  else  {    return false;  }}//入队列int EnQueue(Queue &Q,BiTNode e){  if((Q.tail - Q.front + INITQUEUE) % INITQUEUE == INITQUEUE - 1)  {    return 0;  }  *Q.tail = e;  Q.tail++;  return 1;}//出队列int DeQueue(Queue &Q,BiTNode &e)//用e返回元素{  if(Q.front == Q.tail)  {    return 0;  }  e = *Q.front;  Q.front++;  return 1;}//判断是否为完全二叉树bool IfCompBinTree(BiTree T)//1是完全二叉树,0不是完全二叉树{    if(T==NULL)    {        return true;    }    Queue Q1;    InitQueue(Q1);    BiTNode *ch;    ch = new BiTNode;    EnQueue(Q1,*T);    int flag=0; //标记  没有孩子或右孩子为空    while(!EmptyQueue(Q1))    {        DeQueue(Q1,*ch);        if(ch->lchild==NULL&&ch->rchild!=NULL)//左孩子空        {            return false;        }        if(flag==1&&(ch->lchild!=NULL||ch->rchild!=NULL))//前面的兄弟是叶子或没有右孩子,自己有孩子        {            return false;        }        if(ch->lchild==NULL||ch->rchild==NULL)//叶子或右孩子为空,后面的兄弟不能有孩子        {            flag=1;        }        if(ch->lchild!=NULL)//把孩子拉进来        {            EnQueue(Q1,*ch->lchild);        }        if(ch->rchild!=NULL)        {            EnQueue(Q1,*ch->rchild);        }    }    return true;}//层次遍历 void LevelOrderTraverse(BiTree T){  if(T==NULL)  {    cout<<"LevelOrderTraverse NULL"<<endl;  }  else  {    BiTNode *e;    e = new BiTNode;    Queue Q;    InitQueue(Q);    EnQueue(Q,*T);    int dep=0;//计数    int curcount=1;//本层未访问节点数量    int nextcount=0;//下一层节点数量    while(!EmptyQueue(Q))//队列不为空    {            DeQueue(Q,*e);      visit(e);      curcount--;      if(e->lchild!=NULL)      {        EnQueue(Q,*e->lchild);        nextcount++;      }      if(e->rchild!=NULL)      {        EnQueue(Q,*e->rchild);        nextcount++;      }      if(curcount==0)      {        dep++;        cout<<"第"<"层"<<        curcount=nextcount;//下一层        nextcount=0;      }    }  }  }int main(){    BiTree T=NULL;    TElemType d,c;    cout<<"请输入树的元素:"<<endl;    CreateBiTree(T,d);  cout<<"层次遍历为:"<<endl;//输出检查  LevelOrderTraverse(T);  cout<<endl;    if(IfCompBinTree(T))    {        cout<<"是完全二叉树"<<endl;    }    else    {        cout<<"不是完全二叉树"<<endl;    }    system("pause");}
结果分析

时间复杂度为O(n)

d6668e8404258e511a0763641a793fc8.pngEND a9a1cfd4662ec2c05f39426beddac14d.png a43cc6ec0e1f2a241f7ce805e739ab42.png扫码关注叉酱二叉树和叉酱呀!!! ccbc16bf4d40a2ea3ee5958ce35f6ba4.png
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值