二叉树的遍历
前序遍历:根---左子树---右子树
中序遍历:左子树---根---右子树
后序遍历:左子树---右子树---根
层序遍历:每一层依次遍历
二叉树的实现
BinaryTree.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include "Queue.h"
typedef int BTDataType;
typedef struct BinaryTreeNode {
BTDataType val;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
}BTNode;
BTNode* createBinaryTree();
void PreOrder(BTNode* root);
void InOrder(BTNode* root);
void PostOrder(BTNode* root);
BTNode* CreateBinaryTree(char* str, int* pi);
void BinaryTreeDestory(BTNode** root);
int BinaryTreeSize(BTNode* root);
int BinaryTreeLeafSize(BTNode* root);
int BinaryTreeDepth(BTNode* root);
int BinaryTreeLevelKSize(BTNode* root,int k);
BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
void LevelOrder(BTNode* root);
int BinaryTreeComplete(BTNode* root);
BinaryTree.c
#include "BinaryTree.h"
BTNode* BuyNode(BTDataType val) {
BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
if (newnode == NULL) {
printf("fail!\n");
exit(-1);
}
newnode->left = NULL;
newnode->right = NULL;
newnode->val = val;
return newnode;
}
//暴力创建
BTNode* createBinaryTree() {
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 = NULL;
node3->left = NULL;
node3->right = NULL;
node4->left = node5;
node4->right = node6;
node5->left = NULL;
node5->right = NULL;
node6->left = node7;
node6->right = NULL;
node7->left = NULL;
node7->right = NULL;
return node1;
}
//前序遍历
void PreOrder(BTNode* root) {
if (root == NULL) {
printf("NULL ");
return;
}
printf("%d ", root->val);
PreOrder(root->left);
PreOrder(root->right);
}
//传入字符串创建
BTNode* CreateBinaryTree(char* str, int* pi) {
if (str[*pi] == '#') {
(*pi)++;
return NULL;
}
BTNode* root = BuyNode(str[(*pi)++] - '0');
root->left=CreateBinaryTree(str, pi);
root->right=CreateBinaryTree(str, pi);
return root;
}
//销毁二叉树
void BinaryTreeDestory(BTNode** root) {
if ((*root )== NULL) {
return;
}
BinaryTreeDestory(&(*root)->left);
BinaryTreeDestory(&(*root)->right);
free(*root);
*root = NULL;
}
//中序遍历
void InOrder(BTNode* root) {
if (root == NULL) {
printf("NULL ");
return;
}
InOrder(root->left);
printf("%d ", root->val);
InOrder(root->right);
}
//后序遍历
void PostOrder(BTNode* root) {
if (root == NULL) {
printf("NULL ");
return;
}
PostOrder(root->left);
PostOrder(root->right);
printf("%d ", root->val);
}
//计算二叉树结点数量
int BinaryTreeSize(BTNode* root) {
if (root == NULL) return 0;
return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}
//计算二叉树叶子节点数量
int BinaryTreeLeafSize(BTNode* root) {
if (root == NULL)return 0;
if (root->left == NULL && root->right == NULL)return 1;
return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}
//计算二叉树深度
int BinaryTreeDepth(BTNode* root) {
if (root == NULL)return 0;
int leftDepth = BinaryTreeDepth(root->left);
int rightDepth = BinaryTreeDepth(root->right);
return 1 + (leftDepth > rightDepth ? leftDepth : rightDepth);
}
//计算第k层的结点个数
int BinaryTreeLevelKSize(BTNode* root,int k) {
if (root == NULL)return 0;
if (k == 1) return 1;
return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);
}
//查找val=x的结点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x) {
if (root == NULL)return NULL;
if (root->val == x)return root;
BTNode* nodeleft = BinaryTreeFind(root->left, x);
if (nodeleft != NULL)return nodeleft;
BTNode* noderight = BinaryTreeFind(root->right, x);
if (noderight != NULL)return noderight;
return NULL;
}
//层序遍历
void LevelOrder(BTNode* root) {
if (root == NULL) {
printf("NULL ");
return;
}
Queue q;
QueueInite(&q);
QueuePush(&q,root);
while (!QueueEmpty(&q)) {
BTNode* front = QueueFront(&q);
if (front == NULL) {
printf("NULL ");
}
else {
printf("%d ", front->val);
}
QueuePop(&q);
if (front != NULL) {
QueuePush(&q, front->left);
QueuePush(&q, front->right);
}
}
}
//判断二叉树是否事完全二叉树
int BinaryTreeComplete(BTNode* root) {
if (root == NULL)return 1;
Queue q;
QueueInite(&q);
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)) {
if (QueueFront(&q) != NULL) {
return 0;
}
QueuePop(&q);
}
return 1;
}
Queue.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct BinaryTreeNode;
typedef struct BinaryTreeNode* QDataType;
typedef struct QueueNode QueueNode;
typedef struct Queue Queue;
struct QueueNode {
QDataType data;
struct QueueNode* next;
};
struct Queue {
QueueNode* head;
QueueNode* tail;
};
void QueueInite(Queue* pq);
void QueuePush(Queue* pq, QDataType val);
int QueueEmpty(Queue* pq);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
void QueueDestory(Queue* pq);
int QueueSize(Queue* pq);
//void QueuePrint(Queue* pq);
Queue.c
#include "Queue.h"
void QueueInite(Queue* pq) {
pq->head = NULL;
pq->tail = NULL;
}
void QueuePush(Queue* pq, QDataType val) {
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
if (newnode == NULL) {
printf("fail\n");
exit(-1);
}
newnode->next = NULL;
newnode->data = val;
if (pq->tail == NULL) {
pq->head = newnode;
pq->tail = newnode;
return;
}
pq->tail->next = newnode;
pq->tail = pq->tail->next;
}
void QueuePop(Queue* pq) {
assert(!QueueEmpty(pq));
if (pq->head->next == NULL) {
free(pq->head);
pq->head = pq->tail = NULL;
return;
}
QueueNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
int QueueEmpty(Queue* pq) {
return pq->head == NULL && pq->tail == NULL;
}
QDataType QueueFront(Queue* pq) {
assert(!QueueEmpty(pq));
return pq->head->data;
}
QDataType QueueBack(Queue* pq) {
assert(!QueueEmpty(pq));
return pq->tail->data;
}
void QueueDestory(Queue* pq) {
QueueNode* cur = pq->head;
while (cur!=NULL) {
QueueNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
//void QueuePrint(Queue* pq) {
// /*QueueNode* cur = pq->head;
// while (cur != NULL) {
// printf("%d ", cur->data);
// cur = cur->next;
// }
// printf("\n");*/
// while (!QueueEmpty(pq)) {
// printf("%d ", QueueFront(pq));
// QueuePop(pq);
// }
// printf("\n");
//}
int QueueSize(Queue* pq) {
int size = 0;
QueueNode* cur = pq->head;
while (cur != NULL) {
size++;
cur = cur->next;
}
return size;
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include "BinaryTree.h"
int main() {
BTNode* root = createBinaryTree();
PreOrder(root);
printf("\n");
InOrder(root);
printf("\n");
PostOrder(root);
printf("\n");
BinaryTreeDestory(&root);
char str[100] = "123###45##67###";
int i=0;
root= CreateBinaryTree(str, &i);
printf("\t前序遍历:");
PreOrder(root);
printf("\n");
printf("\t中序遍历:");
InOrder(root);
printf("\n");
printf("\t后序遍历:");
PostOrder(root);
printf("\n");
int size = BinaryTreeSize(root);
printf("TreeSize=%d\n", size);
int leafsize = BinaryTreeLeafSize(root);
printf("leafsize=%d\n", leafsize);
int depth = BinaryTreeDepth(root);
printf("TreeDepth=%d\n", depth);
int kLevelSize = BinaryTreeLevelKSize(root,3 );
printf("TreeLevelKSize=%d\n", kLevelSize);
BTNode* node = BinaryTreeFind(root, 6);
printf("node->val=%d\n", node->val);
printf("\t层序遍历:");
LevelOrder(root);
int ret = BinaryTreeComplete(root);
if (ret == 0)printf("NOBinaryTreeComplete!");
return 0;
}