1.二叉树的销毁
2.二叉树的层序遍历
3.判断二叉树是否为完全二叉树
4.二叉树的性质
1.二叉树的销毁
以后序的方式遍历销毁左右子数,因为前序和中序销毁的话根会被销毁而找不到左右子树的位置,后序的根访问在最后,可以找到左右的子树位置。
void TreeDestory(BTNode* root)
{if (root == NULL)return;TreeDestory(root->left);TreeDestory(root->right);free(root);
}
2.二叉树的层序遍历(BFS广度优先遍历)
注意点:因为实现二叉树的层序遍历需要用到队列的代码,如果是想把之前写的队列代码拷贝过来,则需要把其文件复制到当前的工程目录中,才可以用include去找到对应的代码。
(前序是深度优先遍历DFS)
实现的思想:
把树的结点地址放进队列中,先把树的根结点放进队列,然后再出队列,出队列的同时把根结点的左右子结点放进队列,每次出队列时,都把出的结点的左右子结点放进队列,上一层带下一层,出队列的同时打印其的值,就是一层一层的出来。
void TreeLevelOrder(BTNode* root)
{Queue q;QueueInit(&q);if (root)QueuePush(&q, root);while (!QueueEmpty(&q)){BTNode* front = QueueFront(&q);QueuePop(&q);printf("%d ", front->data);if (front->left)QueuePush(&q, front->left);if (front->right)QueuePush(&q, front->right);}QueueDestroy(&q);
}
typedef struct BinaryTreeNode* QDataType;
typedef struct QueueNode
{struct QueueNode* next;QDataType val;
}QNode
代码分析:
(注意QDataType的类型是BTNode*,并不是int,val存储的是树结点地址)
首先创建一个队列,把树的根结点地址放进去 ,然后使用循环while,以队列是否为NULL作为条件,使用一个指针front去接收队列出的指针,然后再出队列的数据,需要注意的是QueuePop中free的是队列的空间,并不是树的空间,所以下面能用front去访问其他的结点与值,不为NULL就进队列,然后下一个循环又出,达到层序遍历的目的。
3.判断二叉树是否为完全二叉树
实现思想:
用层序遍历来解决,创建一个队列,把树的结点以地址的形式放进队列中,空的结点也放进去(待会会以这个为判断),每取出队列的数据就判断值是否为NULL,如果为NULL就跳出循环,这时候队列还有数据为出队列,接下来就是进队列去遍历队列,如果有非空的值,就说明这个数不是完全二叉树,否则为完全二叉树。这样判断是因为如果不是完全二叉树,最后一层的结点是不连续的,有空结点插在非空的结点之中,依照层序来看的话,就是队列中空和非空都放在一起,所以以第一个取出值为NULL时,就跳出循环,因为此时非空的结点已经出队列了,有一个非空还在队列中(假设不是完全二叉树),就可以以此来判断了。
void TreeComplete(BTNode* root)
{Queue q;QueueInit(&q);if (root)QueuePush(&q, root);while (!QueueEmpty(&q)){BTNode* front = QueueFront(&q);QueuePop(&q);if (front == NULL)break;QueuePush(&q, front->left);QueuePush(&q, front->right);}while (!QueueEmpty(&q)){BTNode* a = QueueFront(&q);QueuePop(&q);if (a){printf("no");return;}}printf("yes");QueueDestroy(&q);
}
4.二叉树的性质
性质3:N0=N2+1,(一般性感兴趣可以搜索)可以先用特殊的来验证,满二叉树来看,最后一层都是度为0的,其余的都是度为2的,最后一层有2^(h-1)个,出去最后一层共有2^(h-1)-1个结点,则相减就为1.
题目一:
题目二:
题目三:
把下面还原成树的形状
前序:1 2 3 4 5 7 6
中序3 2 1 5 7 4 6
解析:
全代码
test文件:
#include<stdio.h>
#include<stdlib.h>
#include"Queue.h"typedef int BTDataType;
typedef struct BinaryTreeNode
{BTDataType data;BinaryTreeNode* left;BinaryTreeNode* right;
}BTNode;BTNode* BuyNode(int x)
{BTNode* a = (BTNode*)malloc(sizeof(BTNode));if (a == NULL){perror("malloc fail:");return 0;}a->data = x;a->left = NULL;a->right = NULL;return a;
}BTNode* CreatNode()
{BTNode* node1 = BuyNode(1);BTNode* node2 = BuyNode(2);BTNode* node3 = BuyNode(3);BTNode* node4 = BuyNode(4);BTNode* node5 = BuyNode(5);BTNode* node6 = BuyNode(6);BTNode* node7 = BuyNode(7);node1->left = node2;node1->right = node4;node2->left = node3;node2->right = node7;node4->left = node5;node4->right = node6;return node1;
}
void PrevOver(BTNode* root)
{if (root == NULL){printf("N ");return;}printf("%d ", root->data);PrevOver(root->left);PrevOver(root->right);
}int TreeNode(BTNode* root)
{return root == NULL ? 0 : TreeNode(root->left) + TreeNode(root->right) + 1;
}int TreeHeight(BTNode* root)
{if (root == NULL){return 0;}int heightleft = TreeHeight(root->left);int heightright = TreeHeight(root->right);return heightleft > heightright ? heightleft +1 : heightright +1;
}
int LeafNode(BTNode* root)
{if (root == NULL)return 0;if (root->left == NULL && root->right == NULL)return 1;return LeafNode(root->left) + LeafNode(root->right);
}
int TreeLevelKSize(BTNode* root, int k)
{if (root == NULL){return 0;}if (k == 1){return 1;}return TreeLevelKSize(root->left, k - 1) + TreeLevelKSize(root->right, k - 1);
}
BTNode* TreeFind(BTNode* root, int x)
{if (root == NULL){return NULL;}if (root->data == x){return root;}BTNode* a = TreeFind(root->left, x);if (a){return a;}BTNode* b = TreeFind(root->right, x);if (b){return b;}return NULL;
}
void TreeDestory(BTNode* root)
{if (root == NULL)return;TreeDestory(root->left);TreeDestory(root->right);free(root);
}void TreeLevelOrder(BTNode* root)
{Queue q;QueueInit(&q);if (root)QueuePush(&q, root);while (!QueueEmpty(&q)){BTNode* front = QueueFront(&q);QueuePop(&q);printf("%d ", front->data);if (front->left)QueuePush(&q, front->left);if (front->right)QueuePush(&q, front->right);}QueueDestroy(&q);
}
void TreeComplete(BTNode* root)
{Queue q;QueueInit(&q);if (root)QueuePush(&q, root);while (!QueueEmpty(&q)){BTNode* front = QueueFront(&q);QueuePop(&q);if (front == NULL)break;QueuePush(&q, front->left);QueuePush(&q, front->right);}while (!QueueEmpty(&q)){BTNode* a = QueueFront(&q);QueuePop(&q);if (a){printf("no");return;}}printf("yes");QueueDestroy(&q);
}
int main()
{BTNode* root = CreatNode();//PrevOver(root);//int ret = TreeNode(root);//int ret1 = TreeHeight(root);//int ret1 = LeafNode(root);//int k = 2;//int ret1=TreeLevelKSize(root, k);//printf("%d", ret1);//int x = 2;//BTNode* a=TreeFind(root, x);//TreeLevelOrder(root);TreeComplete(root);//printf("%d", (*a).data);
}
Queue.h文件:
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
#include<stdbool.h>typedef struct BinaryTreeNode* QDataType;
typedef struct QueueNode
{struct QueueNode* next;QDataType val;
}QNode;typedef struct Queue
{QNode* phead;QNode* ptail;int size;
}Queue;void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);// 队尾插入
void QueuePush(Queue* pq, QDataType x);
// 队头删除
void QueuePop(Queue* pq);// 取队头和队尾的数据
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);int QueueSize(Queue* pq);
bool QueueEmpty(Queu
Queue.c文件:
#include"Queue.h"void QueueInit(Queue* pq)
{assert(pq);pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->phead;while (cur){QNode* next = cur->next;free(cur);cur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}// 队尾插入
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");return;}newnode->next = NULL;newnode->val = x;if (pq->ptail == NULL){pq->phead = pq->ptail = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}// 队头删除
void QueuePop(Queue* pq)
{assert(pq);assert(pq->size != 0);/*QNode* next = pq->phead->next;free(pq->phead);pq->phead = next;if (pq->phead == NULL)pq->ptail = NULL;*/// 一个节点if (pq->phead->next == NULL){free(pq->phead);pq->phead = pq->ptail = NULL;}else // 多个节点{QNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}QDataType QueueFront(Queue* pq)
{assert(pq);assert(pq->phead);return pq->phead->val;
}QDataType QueueBack(Queue* pq)
{assert(pq);assert(pq->ptail);return pq->ptail->val;
}int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}bool QueueEmpty(Queue* pq)
{assert(pq);return pq->size == 0;
}