二叉树层序、判断是否为完全二叉树

二叉树层序和判断是否为完全二叉树需要借助队列的先进先出性质。

结构体如下:

typedef int BTDatatype;
typedef struct BinaryTreeNode
{
    struct BinaryTreeNode *left;
    struct BinaryTreeNode *right;
    BTDatatype data;
}BTNode;
typedef BTNode*  QeDatatype;
typedef struct QueueNode
{
    QeDatatype data;
    struct QueueNode *next;
}QueueNode;
typedef struct Queue
{
    QueueNode *head;
    QueueNode *tail;
}Queue;

**队列的一些基本操作:
代码如下:**
int IsQueueEmpty(Queue *q)//队列是否为空
{
if (q->head == NULL)
return 0;
return 1;
}
int QueueSize(Queue *q)//队列个数
{
int count = 0;
if (q->head == NULL)
return 0;
QueueNode *cur = q->head;
while (cur)
{
count++;
cur = cur->next;
}
return count;
}
QeDatatype QueueBack(Queue *q)//队列最后一个元素
{
assert(q&&q->tail);
return q->tail->data;
}
QeDatatype QueueFront(Queue *q)//队列第一个元素
{
assert(q&&q->head);
return q->head->data;
}
void QueuePop(Queue *q)//出队列
{
assert(q);
if (q->head == NULL)
{
printf(“没有元素\n”);
return;
}
if (q->head == q->tail)//说明只有一个元素
{
free(q->head);
q->head = NULL;
q->tail = NULL;
}
else
{
QueueNode *cur = q->head->next;
free(q->head);
q->head = cur;
}
}

void QueuePush(Queue *q, QeDatatype x)//进队列
{
QueueNode new = (QueueNode )malloc(sizeof(QueueNode));
new->data = x;
new->next = NULL;
if (q->head == NULL)
{
q->head = q->tail = new;
}
else
{
q->tail->next = new;
q->tail = new;
}
}
void QueueInit(Queue *q)//初始化队列
{
q->head = NULL;
q->tail = NULL;
}
二叉树层序:
这里写图片描述
代码如下:

void LevelOrder(BTNode *root) //层序遍历
{
    if (root==NULL)
        return;
    Queue q;
    QueueInit(&q); //初始化队列
    if (root)
        QueuePush(&q, root);
    while (IsQueueEmpty(&q))//如果队列不为空
    {
        BTNode *front= QueueFront(&q);
        printf("%d ", front->data);
        QueuePop(&q); //可以pop,因为从队列里pop的是结点的指针
        if(front->left )
            QueuePush(&q, front->left );
        if(front->right )
            QueuePush(&q, front->right);
    }
    printf("\n");
}

判断是否为完全二叉树:
这里写图片描述
代码如下:
是完全二叉树返回1,否返回0。

int BTreeIscomplete(BTNode *root)//判断完全二叉树
{
    if (root == NULL)
        return 1;
    Queue q;
    QueueInit(&q); //初始化队列
    QueuePush(&q, root);
    while (IsQueueEmpty(&q))
    {
        BTNode *front = QueueFront(&q);
        if (front == NULL)
            break;
        QueuePop(&q);
        QueuePush(&q, front->left);
        QueuePush(&q, front->right);
    }
    while (IsQueueEmpty(&q))
    {
        if (QueueFront(&q) != NULL)
            return 0;
        QueuePop(&q);
    }
    return 1;
}
**第二种方法判断是否为完全二叉树:**
利用flag,基于完全二叉树结点是连续的:如果一个结点的左右孩子有空,则将flag设为1,flag=1后,如果一个结点左右子树有一个非空,那么该树则不是完全二叉树。
代码如下:

int BTreeIscompleteFlag(BTNode *root)//判断完全二叉树
{
if (root == NULL)
return 1;
Queue q;
int flag = 0;
QueueInit(&q); //初始化队列
QueuePush(&q, root);
while (IsQueueEmpty(&q))
{
BTNode *front = QueueFront(&q);
if (front == NULL)
break;
if ((front->left || front->right) && flag == 1)
return 0;
if (front->left == NULL|| front->right == NULL)
flag = 1;
QueuePop(&q);
QueuePush(&q, front->left);
QueuePush(&q, front->right);
}
return 1;
}


测试函数:

void TestBinaryTree()
{
BTNode *root = NULL;
BTDatatype a[] = { 1, 2, 3, ‘#’, ‘#’,4,’#’, ‘#’, 5, 6,9 ,’#’,’#’,’#’, ‘#’ };
int pIndex = 0;
int sz = sizeof(a) / sizeof(a[0]);
root = CreateBTTree(a, sz, &pIndex, ‘#’);
BTtreePrevOreder(root);//前序
printf(“\n”);
LevelOrder(root); //层序遍历
printf(“%d\n”, BTreeIscomplete(root)); //判断完全二叉树
printf(“%d\n”,BTreeIscompleteFlag(root)); //判断完全二叉树}
结果如下:
这里写图片描述
该树不是完全二叉树。

猜你喜欢

转载自blog.csdn.net/sophia__yu/article/details/80032492