数据结构之二叉树的构建与遍历(C语言)

一、二叉树的概念

二叉树是树形结构的一个重要类型。许多实际问题抽象出来的数据结构往往是二叉树形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显得特别重要。二叉树特点是每个节点最多只能有两棵子树,且有左右之分。

二叉树有四种遍历方式,分别为前序遍历、中序遍历、后序遍历和层次遍历 ,遍历的实现方式有两种, 递归实现和非递归实现 。

 

二、代码步骤

1、创建二叉树

2、创建链队列

3、链队列的初始化

4、判断队列是否为空

5、入队

6、出队

7、构造节点

8、将字符串转化为二叉树

9、逐层遍历

10、前序遍历

11、中序遍历

12、后序遍历

13、程序入口

14、运行结果


三、代码功能

1、创建二叉树

typedef struct BTNode
{
	char element;
	BTNode* left;
	BTNode* right;
}BTNode, *BTNodePtr;

2、创建链队列

typedef struct BTNodePtrQueue
{
	BTNodePtr* nodePtrs;
	int front;
	int rear;
}BTNodePtrQueue, *QueuePtr;

3、链队列的初始化

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;
}

4、判断队列是否为空

bool isQueueEmpty(QueuePtr paraQueuePtr)
{
	if ((paraQueuePtr->front + 1) % QUEUE_SIZE == paraQueuePtr->rear)
	{
		return true;
	}//Of if

	return false;
}

5、入队

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);
}

6、出队

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];
}

7、构造节点

BTNodePtr constructBTNode(char paraChar)
{
	BTNodePtr resultPtr = (BTNodePtr)malloc(sizeof(BTNode));
	resultPtr->element = paraChar;
	resultPtr->left = NULL;
	resultPtr->right = NULL;
	return resultPtr;
}

8、将字符串转化为二叉树

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;
}

9、逐层遍历

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);
}

10、前序遍历

void preorder(BTNodePtr tempPtr)
{
	if (tempPtr == NULL)
	{
		return;
	}//Of if

	printf("%c", tempPtr->element);
	preorder(tempPtr->left);
	preorder(tempPtr->right);
}

11、中序遍历

void inorder(BTNodePtr tempPtr)
{
	if (tempPtr == NULL)
	{
		return;
	}//Of if

	inorder(tempPtr->left);
	printf("%c", tempPtr->element);
	inorder(tempPtr->right);
}

12、后序遍历

void postorder(BTNodePtr tempPtr)
{
	if (tempPtr == NULL) {
		return;
	}//Of if

	postorder(tempPtr->left);
	postorder(tempPtr->right);
	printf("%c", tempPtr->element);
}

13、程序入口

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;
}

14、运行结果

There is only one node. Preorder visit: c
front = 0, rear = 1.
enqueue a ends.
dequeue a ends.
front = 1, rear = 2.
enqueue c ends.
front = 1, rear = 3.
enqueue d ends.
dequeue c ends.
front = 2, rear = 4.
enqueue e ends.
dequeue d ends.
front = 3, rear = 0.
enqueue b ends.
front = 3, rear = 1.
enqueue f ends.
dequeue e ends.
dequeue b ends.
dequeue f ends.
Preorder: acedbf
Inorder: ecabdf
Postorder: ecbfda
Levelwise: front = 0, rear = 1.
enqueue a ends.
dequeue a ends.
front = 1, rear = 2.
enqueue c ends.
front = 1, rear = 3.
enqueue d ends.
dequeue c ends.
front = 2, rear = 4.
enqueue e ends.
dequeue d ends.
front = 3, rear = 0.
enqueue b ends.
front = 3, rear = 1.
enqueue f ends.
dequeue e ends.
dequeue b ends.
dequeue f ends.
Levelwise: acdebf

四、整体代码

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值