目录
详细实现链接:
<二叉树>《数据结构(C语言版)》
<二叉树(链式)>《数据结构(C语言版)》
1.分析实现功能,感受二叉树的结构:
结构设计注意问题:
功能函数:
层序遍历复用的查找问题:
2.完整源码:
BinaryTree:
#include <stdlib.h> #include <stdio.h> #include"Queue.h" typedef char BTDataType; typedef struct BinaryTreeNode { struct BinaryTreeNode* left; struct BinaryTreeNode* right; BTDataType data; }BTNode; //前序遍历 void PrevOrder(BTNode* root) { if (root == NULL) { printf("NULL "); return; } printf("%c ", root->data); PrevOrder(root->left); PrevOrder(root->right); } //中序遍历 void InOrder(BTNode* root) { if (root == NULL) { printf("NULL "); return; } InOrder(root->left); printf("%c ", root->data); InOrder(root->right); } //后序遍历 void PostOrder(BTNode* root) { if (root == NULL) { printf("NULL "); return; } PostOrder(root->left); PostOrder(root->right); printf("%c ", root->data); } //int size = 0; //void TreeSize(BTNode* root) //{ // if (root == NULL) // { // return; // } // else // { // ++size; // } // // TreeSize(root->left); // TreeSize(root->right); //} //void TreeSize(BTNode* root, int* psize) //{ // if (root == NULL) // { // return; // } // else // { // ++(*psize); // } // // TreeSize(root->left, psize); // TreeSize(root->right, psize); //} //采用分治思想 int TreeSize(BTNode* root) { return root == NULL ? 0 : TreeSize(root->left) + TreeSize(root->right) + 1; } // 叶子节点的个数 int TreeLeafSize(BTNode* root) { //1.0个 //2.1个 //3.一个以上 if (root == NULL) return 0; if (root->left == NULL && root->right == NULL) return 1; return TreeLeafSize(root->left) + TreeLeafSize(root->right); } //层序遍历 void LevelOrder(BTNode* root) { // 核心思路:上一层出的时候带下一层节点进 Queue q; QueueInit(&q); if (root) QueuePush(&q, root); while (!QueueEmpty(&q)) { BTNode* front = QueueFront(&q); QueuePop(&q); printf("%c ", front->data); if (front->left) { QueuePush(&q, front->left); } if (front->right) { QueuePush(&q, front->right); } } printf("\n"); QueueDestory(&q); } int main() { BTNode* A = (BTNode*)malloc(sizeof(BTNode)); A->data = 'A'; A->left = NULL; A->right = NULL; BTNode* B = (BTNode*)malloc(sizeof(BTNode)); B->data = 'B'; B->left = NULL; B->right = NULL; BTNode* C = (BTNode*)malloc(sizeof(BTNode)); C->data = 'C'; C->left = NULL; C->right = NULL; BTNode* D = (BTNode*)malloc(sizeof(BTNode)); D->data = 'D'; D->left = NULL; D->right = NULL; BTNode* E = (BTNode*)malloc(sizeof(BTNode)); E->data = 'E'; E->left = NULL; E->right = NULL; A->left = B; A->right = C; B->left = D; B->right = E; PrevOrder(A); printf("\n"); InOrder(A); printf("\n"); PostOrder(A); printf("\n"); /*TreeSize(A); printf("TreeSize:%d\n", size); size = 0; TreeSize(B); printf("TreeSize:%d\n", size);*/ /*int Asize = 0; TreeSize(A, &Asize); printf("TreeSize:%d\n", Asize); int Bsize = 0; TreeSize(B, &Bsize); printf("TreeSize:%d\n", Bsize);*/ printf("TreeSize:%d\n", TreeSize(A)); printf("TreeSize:%d\n", TreeSize(B)); LevelOrder(A); return 0; }
复用的Queue.h:
#pragma once #include <stdio.h> #include <stdbool.h> #include <assert.h> #include <stdlib.h> //前置声明 struct BinaryTreeNode; //在Queue.c展开时,可以在这里指定,防止找不到 //typedef int QDataType; typedef struct BinaryTreeNode* QDataType; //头文件展开时向上找 typedef struct QueueNode { struct QueueNode* next; QDataType data; }QNode; typedef struct Queue { QNode* head; QNode* tail; }Queue; //初始化 void QueueInit(Queue* pq); //销毁 void QueueDestory(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(Queue* pq);
复用的Queue.c:
#include"Queue.h" //初始化 void QueueInit(Queue* pq) { assert(pq); pq->head = pq->tail = NULL; } //销毁 void QueueDestory(Queue* pq) { assert(pq); QNode* cur = pq->head; while (cur) { QNode* next = cur->next; free(cur); cur = next; } pq->head = pq->tail = NULL; } // 队尾入 void QueuePush(Queue* pq, QDataType x) { assert(pq); QNode* newnode = (QNode*)malloc(sizeof(QNode)); if (newnode == NULL) { printf("malloc fail!\n"); exit(-1); } newnode->data = x; newnode->next = NULL; if (pq->tail == NULL) { pq->head = pq->tail = newnode; } else { pq->tail->next = newnode; pq->tail = newnode; } } // 队头出 void QueuePop(Queue* pq) { assert(pq); assert(pq->head); // 1、一个 // 2、多个 if (pq->head->next == NULL) { free(pq->head); pq->head = pq->tail = NULL; } else { QNode* next = pq->head->next; free(pq->head); pq->head = next; } } //取队头元素 QDataType QueueFront(Queue* pq) { assert(pq); assert(pq->head); return pq->head->data; } //取队尾元素 QDataType QueueBack(Queue* pq) { assert(pq); assert(pq->head); return pq->tail->data; } //队列大小 int QueueSize(Queue* pq) { assert(pq); int size = 0; QNode* cur = pq->head; while (cur) { ++size; cur = cur->next; } return size; } //队列判空 bool QueueEmpty(Queue* pq) { assert(pq); return pq->head == NULL; }