目录
二叉树实现
定义节点
typedef struct BinaryTreeNode {
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
int val;
}BTNode;
我们首先定义二叉树的节点,用val来保存值,通过两个指针实现节点与节点之间的联系。
创建节点
//创建节点
BTNode* BuyNode(int x) {
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
if (node == NULL) {
perror("malloc fail");
exit(-1);
}
node->val = x;
node->left = NULL;
node->right = NULL;
return node;
}
手动连接
int main() {
//创建节点
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);
BTNode* node8 = BuyNode(8);
//手动连接
node1->left = node2;
node1->right = node4;
node2->left = node3;
node2->right = node7;
node4->left = node5;
node4->right = node6;
node3->left = node8;
return 0;
}
前序
//前序-根左右
void PrevOrder(BTNode* root) {
if (root == NULL) {
printf("NULL ");
return;
}
printf("%d ",root->val);
PrevOrder(root->left);
PrevOrder(root->right);
}
中序
//中序-左根右
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 TreeSize(BTNode* root) {
return root == NULL ? 0 : TreeSize(root->left) + TreeSize(root->right) + 1;
}
分治思想:整体的个数=左子树个数+右子树个数+1
叶子节点个数
//叶子节点个数
int TreeLeafSize(BTNode* root) {
//尽头
if (root == NULL) {
return 0;
}
//判断是否为叶子节点
if ((root->left == NULL) && (root->right == NULL)) {
return 1;
}
int left = TreeLeafSize(root->left);
int right = TreeLeafSize(root->right);
return left + right;
}
第k层节点个数
//求第k层节点个数
int TreekLevel(BTNode* root, int k) {
assert(k > 0);
if (root == NULL) {
return 0;
}
if (k == 1) {
return 1;
}
return TreekLevel(root->left, k - 1) + TreekLevel(root->right,k-1);
}
查找值为x的节点
//二叉树查找值为x的节点
BTNode* TreeFind(BTNode* root, int x) {
if (root == NULL) {
return NULL;
}
if (root->val == x) {
return root;
}
BTNode*left=TreeFind(root->left,x);
if (left != NULL) {
return left;
}
BTNode* right = TreeFind(root->right, x);
if (right != NULL) {
return right;
}
return NULL;
}
层序
//层序---一层一层走
void LevelOrder(BTNode* root) {
Que q;
//初始化
QueueInit(&q);
if (root != NULL) {
//入队列
QueuePush(&q, root);
}
//判断队顶元素是否为空,为空不入,不为空进入
while (!QueueEmpty(&q)) {
//先复制队顶元素
BTNode* front = QueueFront(&q);
//打印队顶元素
printf("%d ",front->val);
//如果队顶左孩子不为空,入队列
if (front->left != NULL) {
QueuePush(&q, front->left);
}
//如果队顶右孩子不为空,入队列
if (front->right != NULL) {
QueuePush(&q, front->right);
}
//删除
QueuePop(&q);
}
printf("\n");
//销毁
QueueDestroy(&q);
}
层序的实现需要借助队列,我们需要在队列里存储节点的地址。队列内容可以参考以往文章。
判断二叉树是否为完全二叉树
//判断二叉树是否是完全二叉树---完全二叉树返回1 非完全二叉树返回0
int TreeComplete(BTNode* root) {
Que q;
//初始化
QueueInit(&q);
if (root != NULL) {
//入队列
QueuePush(&q, root);
}
//判断队顶元素是否为空,为空不入,不为空进入
while (!QueueEmpty(&q)){
//先复制队顶元素
BTNode* front = QueueFront(&q);
if (front == NULL) {
break;
}
QueuePush(&q, front->left);
QueuePush(&q, front->right);
//删除
QueuePop(&q);
}
while (!QueueEmpty(&q)) {
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front != NULL) {
//销毁
QueueDestroy(&q);
return false;
}
}
//销毁
QueueDestroy(&q);
return true;
}
二叉树高度
//二叉树高度
int TreeHeight(BTNode* root) {
if (root == NULL) {
return 0;
}
return fmax(TreeHeight(root->left), TreeHeight(root->right)) + 1;
}
销毁
//销毁
void TreeDestroy(BTNode* root) {
//先销毁左 再销毁右 再销毁根---后序思想
if (root == NULL) {
return;
}
TreeDestroy(root->left);
TreeDestroy(root->right);
free(root);
}
通过前序遍历的数组构建二叉树(通过递归链接)
// 通过前序遍历的数组构建二叉树
BTNode* BinaryTreeCreate(BTDataType* a, int* pi) {
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
if (node == NULL) {
perror("malloc fail");
exit(-1);
}
node->val = a[(*pi)++];
if (node->val == '#') {
return NULL;
}
node->left = BinaryTreeCreate(a,pi);
node->right = BinaryTreeCreate(a,pi);
return node;
}
int main(){
int i=0;
char a[]="ABC###G##";//#表示NULL
BTNode*pf= BinaryTreeCreate(a,&i);
}
如有不足,请各位大佬多多指正!!!