二叉树的介绍
(摘自网络)
二叉树
是一种由节点构成的树形数据结构,其中每个节点最多有两个子节点。其中一个被称为左子节点
,另一个被称为右子节点
。在二叉树中,每个节点可以有零个、一个或两个子节点。根据子节点的数量,可以将二叉树分类为满二叉树
、完全二叉树
和普通二叉树
。二叉树通常用于搜索和排序算法,并且在编程中被广泛应用。
二叉树的构建
是指将一些节点按照某种规则组织成一个二叉树的过程。二叉树遍历是指按照预先定义好的方式,对构建好的二叉树进行遍历访问每个节点的过程。
具体来说,二叉树遍历一般分为前序遍历、中序遍历和后序遍历
三种方式。
其中前序遍历
是指先访问根节点,然后再访问左子树和右子树;中序遍历
是指先访问左子树,然后再访问根节点和右子树;后序遍历
是指先访问左子树和右子树,最后再访问根节点。
代码实现
#include <stdio.h>
#include <malloc.h>
#define QUEUE_SIZE 5
/**
* Binary tree node.
*/
typedef struct BTNode
{
char element;
BTNode* left;
BTNode* right;
}BTNode, * BTNodePtr;
/**
* A queue with a number of pointers.
*/
typedef struct BTNodePtrQueue
{
BTNodePtr* nodePtrs;
int front;
int rear;
}BTNodePtrQueue, * QueuePtr;
/**
* Initialize the queue.
*/
QueuePtr initQueue()
{
QueuePtr resultQueuePtr = (QueuePtr)malloc(sizeof(struct BTNodePtrQueue));
resultQueuePtr->nodePtrs = (BTNodePtr*)malloc(QUEUE_SIZE * sizeof(BTNodePtr));
resultQueuePtr->front = 0;
resultQueuePtr->rear = 1;
return resultQueuePtr;
}//Of initQueue
/**
* Is the queue empty?
*/
bool isQueueEmpty(QueuePtr paraQueuePtr) {
if ((paraQueuePtr->front + 1) % QUEUE_SIZE == paraQueuePtr->rear) {
return true;
}//Of if
return false;
}//Of isQueueEmpty
/**
* Add a pointer to the queue.
*/
void enqueue(QueuePtr paraQueuePtr, BTNodePtr paraBTNodePtr) {
printf("front = %d, rear = %d.\r\n", paraQueuePtr->front, paraQueuePtr->rear);
if ((paraQueuePtr->rear + 1) % QUEUE_SIZE == paraQueuePtr->front % QUEUE_SIZE) {
printf("Error, trying to enqueue %c. queue full.\r\n", paraBTNodePtr->element);
return;
}//Of if
paraQueuePtr->nodePtrs[paraQueuePtr->rear] = paraBTNodePtr;
paraQueuePtr->rear = (paraQueuePtr->rear + 1) % QUEUE_SIZE;
printf("enqueue %c ends.\r\n", paraBTNodePtr->element);
}//Of enqueue
/**
* Remove an element from the queue and return.
*/
BTNodePtr dequeue(QueuePtr paraQueuePtr) {
if (isQueueEmpty(paraQueuePtr)) {
printf("Error, empty queue\r\n");
return NULL;
}//Of if
paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;
//BTNodePtr tempPtr = paraQueuePtr->nodePtrs[paraQueuePtr->front + 1];
printf("dequeue %c ends.\r\n", paraQueuePtr->nodePtrs[paraQueuePtr->front]->element);
return paraQueuePtr->nodePtrs[paraQueuePtr->front];
}//Of dequeue
/**
* Construct a BTNode using the given char.
*/
BTNodePtr constructBTNode(char paraChar) {
BTNodePtr resultPtr = (BTNodePtr)malloc(sizeof(BTNode));
resultPtr->element = paraChar;
resultPtr->left = NULL;
resultPtr->right = NULL;
return resultPtr;
}//Of constructBTNode
/**
* Construct a binary tree using the given string.
*/
BTNodePtr stringToBTree(char* paraString) {
int i;
char ch;
//Use a queue to manage the pointers
QueuePtr tempQueuePtr = initQueue();
BTNodePtr resultHeader;
BTNodePtr tempParent, tempLeftChild, tempRightChild;
i = 0;
ch = paraString[i];
resultHeader = constructBTNode(ch);
enqueue(tempQueuePtr, resultHeader);
while (!isQueueEmpty(tempQueuePtr)) {
tempParent = dequeue(tempQueuePtr);
//The left child
i++;
ch = paraString[i];
if (ch == '#') {
tempParent->left = NULL;
}
else {
tempLeftChild = constructBTNode(ch);
enqueue(tempQueuePtr, tempLeftChild);
tempParent->left = tempLeftChild;
}//Of if
//The right child
i++;
ch = paraString[i];
if (ch == '#') {
tempParent->right = NULL;
}
else {
tempRightChild = constructBTNode(ch);
enqueue(tempQueuePtr, tempRightChild);
tempParent->right = tempRightChild;
}//Of if
}//Of while
return resultHeader;
}//Of stringToBTree
/**
* Levelwise.
*/
void levelwise(BTNodePtr paraTreePtr) {
//Use a queue to manage the pointers
char tempString[100];
int i = 0;
QueuePtr tempQueuePtr = initQueue();
BTNodePtr tempNodePtr;
enqueue(tempQueuePtr, paraTreePtr);
while (!isQueueEmpty(tempQueuePtr)) {
tempNodePtr = dequeue(tempQueuePtr);
//For output.
tempString[i] = tempNodePtr->element;
i++;
if (tempNodePtr->left != NULL) {
enqueue(tempQueuePtr, tempNodePtr->left);
}//Of if
if (tempNodePtr->right != NULL) {
enqueue(tempQueuePtr, tempNodePtr->right);
}//Of if
}//Of while
tempString[i] = '\0';
printf("Levelwise: %s\r\n", tempString);
}//Of levelwise
/**
* Preorder.
*/
void preorder(BTNodePtr tempPtr) {
if (tempPtr == NULL) {
return;
}//Of if
printf("%c", tempPtr->element);
preorder(tempPtr->left);
preorder(tempPtr->right);
}//Of preorder
/**
* Inorder.
*/
void inorder(BTNodePtr tempPtr) {
if (tempPtr == NULL) {
return;
}//Of if
inorder(tempPtr->left);
printf("%c", tempPtr->element);
inorder(tempPtr->right);
}//Of inorder
/**
* Post order.
*/
void postorder(BTNodePtr tempPtr) {
if (tempPtr == NULL) {
return;
}//Of if
postorder(tempPtr->left);
postorder(tempPtr->right);
printf("%c", tempPtr->element);
}//Of postorder
/**
* The entrance.
*/
int main() {
BTNodePtr tempHeader;
tempHeader = constructBTNode('c');
printf("There is only one node. Preorder visit: ");
preorder(tempHeader);
printf("\r\n");
char* tempString = "acde#bf######";
tempHeader = stringToBTree(tempString);
printf("Preorder: ");
preorder(tempHeader);
printf("\r\n");
printf("Inorder: ");
inorder(tempHeader);
printf("\r\n");
printf("Postorder: ");
postorder(tempHeader);
printf("\r\n");
printf("Levelwise: ");
levelwise(tempHeader);
printf("\r\n");
return 1;
}//Of main
代码分析
1. 定义二叉树节点和队
/**
* Binary tree node.
*/
typedef struct BTNode {
char element;
BTNode* left;
BTNode* right;
}BTNode, * BTNodePtr;
/**
* A queue with a number of pointers.
*/
typedef struct BTNodePtrQueue {
BTNodePtr* nodePtrs;
int front;
int rear;
}BTNodePtrQueue, * QueuePtr;
其中BTNode二叉树的节点,包了节点所存储的元素,以及左子节点和右子节点。
其中BTNodePtrQueue 的front
和rear
分别指向队列队头和队尾
2.初始化
QueuePtr initQueue()
{
QueuePtr resultQueuePtr = (QueuePtr)malloc(sizeof(struct BTNodePtrQueue));
resultQueuePtr->nodePtrs = (BTNodePtr*)malloc(QUEUE_SIZE * sizeof(BTNodePtr));
resultQueuePtr->front = 0;
resultQueuePtr->rear = 1;
return resultQueuePtr;
}//Of initQueue
- 首先使用
malloc()
函数动态分配了一个队列指针resultQueuePtr
所指向的结构体的内存空间。然后,为nodePtrs
数组动态分配了QUEUE_SIZE 个 BTNodePtr 类型
的内存空间。最后,将队首 front 设为 0,队尾 rear 设为 1,并返回该结构体指针。 - 能够保证队列的可拓展性,同时还避免了内存浪费的情况。
3. 判断队列是否为空
/**
* Is the queue empty?
*/
bool isQueueEmpty(QueuePtr paraQueuePtr) {
if ((paraQueuePtr->front + 1) % QUEUE_SIZE == paraQueuePtr->rear) {
return true;
}//Of if
return false;
}//Of isQueueEmpty
检查队头的下一个位置是否等于队尾的位置
4. 添加
/**
* Add a pointer to the queue.
*/
void enqueue(QueuePtr paraQueuePtr, BTNodePtr paraBTNodePtr) {
printf("front = %d, rear = %d.\r\n", paraQueuePtr->front, paraQueuePtr->rear);
if ((paraQueuePtr->rear + 1) % QUEUE_SIZE == paraQueuePtr->front % QUEUE_SIZE) {
printf("Error, trying to enqueue %c. queue full.\r\n", paraBTNodePtr->element);
return;
}//Of if
paraQueuePtr->nodePtrs[paraQueuePtr->rear] = paraBTNodePtr;
paraQueuePtr->rear = (paraQueuePtr->rear + 1) % QUEUE_SIZE;
printf("enqueue %c ends.\r\n", paraBTNodePtr->element);
}//Of enqueue
- 添加指针到队列,通过传入一个队列指针和一个二叉树节点指针,将节点指针添加到队列中。
- 首先,函数会打印出当前队列的头和尾。然后,判断队列是否已满,如果已经满了则打印错误信息并返回;
- 如果队列没有满,则将节点指针添加到队列中,并更新队列尾的位置。
5. 移出
/**
* Remove an element from the queue and return.
*/
BTNodePtr dequeue(QueuePtr paraQueuePtr) {
if (isQueueEmpty(paraQueuePtr)) {
printf("Error, empty queue\r\n");
return NULL;
}//Of if
paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;
//BTNodePtr tempPtr = paraQueuePtr->nodePtrs[paraQueuePtr->front + 1];
printf("dequeue %c ends.\r\n", paraQueuePtr->nodePtrs[paraQueuePtr->front]->element);
return paraQueuePtr->nodePtrs[paraQueuePtr->front];
}//Of dequeue
- 若队列为空则输出错误信息并返回空指针。
- 对于非空队列,更新队首指针,并返回队首指向的元素。
6.构建二叉树结点
/**
* Construct a BTNode using the given char.
*/
BTNodePtr constructBTNode(char paraChar) {
BTNodePtr resultPtr = (BTNodePtr)malloc(sizeof(BTNode));
resultPtr->element = paraChar;
resultPtr->left = NULL;
resultPtr->right = NULL;
return resultPtr;
}//Of constructBTNode
- 首先通过
malloc函数
分配了一个BTNodePtr类型
的空间并将其地址赋值给了resultPtr
指针。然后将传入的paraChar
赋值给了新建节点的element,左右子树指针均初始化为NULL
。 - 最终返回resultPtr指针,即新建节点的地址。
7.将给定字符串转化为二叉树
/**
* Construct a binary tree using the given string.
*/
BTNodePtr stringToBTree(char* paraString) {
int i;
char ch;
//Use a queue to manage the pointers
QueuePtr tempQueuePtr = initQueue();
BTNodePtr resultHeader;
BTNodePtr tempParent, tempLeftChild, tempRightChild;
i = 0;
ch = paraString[i];
resultHeader = constructBTNode(ch);
enqueue(tempQueuePtr, resultHeader);
while (!isQueueEmpty(tempQueuePtr)) {
tempParent = dequeue(tempQueuePtr);
//The left child
i++;
ch = paraString[i];
if (ch == '#') {
tempParent->left = NULL;
}
else {
tempLeftChild = constructBTNode(ch);
enqueue(tempQueuePtr, tempLeftChild);
tempParent->left = tempLeftChild;
}//Of if
//The right child
i++;
ch = paraString[i];
if (ch == '#') {
tempParent->right = NULL;
}
else {
tempRightChild = constructBTNode(ch);
enqueue(tempQueuePtr, tempRightChild);
tempParent->right = tempRightChild;
}//Of if
}//Of while
return resultHeader;
}//Of stringToBTree
首先初始化一个空的队列,然后按照前序遍历
的方式逐个处理字符,如果该字符是“#”,则表示该节点没有子节点,否则构建一个新的节点,并将其插入到队列尾部
,同时将该节点作为父节点的左子节点或右子节点
。最后返回二叉树的头节点。
8. 遍历
levelwise、preorder、inorder、postorder
levelwise函数用于按层遍历
二叉树,它使用了一个队列来管理节点指针。首先将根节点入队,然后进入循环。在循环中,取出队首的节点指针,将其值存入临时字符串中,并将其左右子节点入队,直到队列为空。最后输出存储在字符串中的值。
preorder函数用于先序遍历
二叉树,它首先判断当前节点是否为空,如果为空则直接返回。否则先输出当前节点的值,然后递归调用左子节点和右子节点。
inorder函数用于中序遍历
二叉树,它也首先判断当前节点是否为空,如果为空则直接返回。否则先递归调用左子节点,然后输出当前节点的值,最后递归调用右子节点。
postorder函数用于后序遍历
二叉树,它也首先判断当前节点是否为空,如果为空则直接返回。否则先递归调用左子节点和右子节点,最后输出当前节点的值。