Binary tree level order, judge whether it is a complete binary tree

The order of the binary tree and the judgment of whether it is a complete binary tree need to rely on the first-in-first-out property of the queue.

The structure is as follows:

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;

**Some basic operations of the queue: the
code is as follows:**
int IsQueueEmpty(Queue *q)//Whether the queue is empty
{
if (q->head == NULL)
return 0;
return 1;
}
int QueueSize(Queue *q )//Number of queues
{
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)//The last element of the queue
{
assert(q&&q->tail);
return q->tail->data;
}
QeDatatype QueueFront(Queue *q)//The first element of the queue
{
assert(q&&q->head);
return q->head->data;
}
void QueuePop(Queue *q)//Out of the queue
{
assert(q);
if (q->head == NULL)
{
printf("No element\n");
return;
}
if (q->head == q->tail)//Indicates that there is only one element
{
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)//Into the queue
{
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)//initialize the queue
{
q->head = NULL;
q->tail = NULL;
}
Binary tree hierarchy: The
write picture description here
code is as follows:

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");
}

Determine whether it is a complete binary tree: the
write picture description here
code is as follows: if it
is a complete binary tree, it returns 1, and if it is not, it returns 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);//Preorder
printf("\n");
LevelOrder(root); //Level order traversal
printf("%d\n", BTreeIscomplete(root)); //Judging complete binary tree
printf(" %d\n",BTreeIscompleteFlag(root)); //Judging complete binary tree}
The result is as follows:
write picture description here
The tree is not a complete binary tree.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324599797&siteId=291194637