二叉树创建和遍历

#include <cstdio>
#include <cstdlib>
#include <queue>
#include <string>

using namespace std;

/**
*二叉树结构体,储存一个char类型的数据和左孩子和右孩子的地址
*/

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

/**
*给定数据创建一个树的节点,左孩子和右孩子都设置为空指针
*/

BTNodePtr constructBTNode(char paraChar) {
	BTNodePtr resultPtr = new BTNode;
	resultPtr->element = paraChar;
	resultPtr->left = NULL;
	resultPtr->right = NULL;
	return resultPtr;
}
/**
*给定一颗树的层次遍历创建一颗二叉树,返回该树的根节点。
*/

BTNodePtr stringToBTree(char* paraString) {
	int i;
	char ch;

	queue <BTNodePtr> tempQueuePtr;

	BTNodePtr resultHeader;
	BTNodePtr tempParent, tempLeftchild, tempRightchild;
	i = 0;
	ch = paraString[i];
	resultHeader = constructBTNode(ch);
	tempQueuePtr.push(resultHeader);
	printf("enqueue %c ends.\n", resultHeader->element);

	while (!tempQueuePtr.empty()) {
		tempParent = tempQueuePtr.front();
		printf("dequeue %c ends.\n", tempParent->element);
		tempQueuePtr.pop();

		i++;
		ch = paraString[i];
		if (ch == '#') {
			tempParent->left = NULL;
		}
		else {
			tempLeftchild = constructBTNode(ch);
			tempQueuePtr.push(tempLeftchild);
			printf("enqueue %c ends.\n", tempLeftchild->element);
			tempParent->left = tempLeftchild;
		}
		i++;
		ch = paraString[i];
		if (ch == '#') {
			tempParent->right = NULL;
		}
		else {
			tempRightchild = constructBTNode(ch);
			tempQueuePtr.push(tempRightchild);
			printf("enqueue %c ends.\n", tempRightchild->element);
			tempParent->right = tempRightchild;
		}
	}

	return resultHeader;
}

/**
*给定一棵树,以广度优先搜索的方式打印出该树
*/

void levelwise(BTNodePtr paraTreePtr) {
	char tempString[100];
	int i = 0;
	queue <BTNodePtr> tempQueuePtr;
	BTNodePtr tempNodePtr;
	tempQueuePtr.push(paraTreePtr);
	printf("enqueue %c ends.\n", paraTreePtr->element);

	while (!tempQueuePtr.empty()) {
		tempNodePtr = tempQueuePtr.front();
		printf("dequeue %c ends.\n", tempNodePtr->element);
		tempQueuePtr.pop();

		tempString[i] = tempNodePtr->element;
		i++;

		if (tempNodePtr->left != NULL) {
			tempQueuePtr.push(tempNodePtr->left);
			printf("enqueue %c ends.\n", tempNodePtr->left->element);
		}
		if(tempNodePtr->right != NULL){
			tempQueuePtr.push(tempNodePtr->right);
			printf("enqueue %c ends.\n", tempNodePtr->right->element);
		}
	}
	
	tempString[i] = '\0';

	printf("levelwise: %s\n", tempString);
}

/**
*二叉树前序遍历
*/

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

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

//中序遍历

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

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

//后序遍历

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

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

int main() {
	BTNodePtr tempHeader;
	tempHeader = constructBTNode('c');
	printf("There is only one node. Preorder visit: ");
	preorder(tempHeader);
	printf("\n");

	char* tempString = (char*)"acde#bf######";

	tempHeader = stringToBTree(tempString);
	printf("Preorder: ");
	preorder(tempHeader);
	printf("\n");
	printf("Inorder: ");
	inorder(tempHeader);
	printf("\n");
	printf("Postorder: ");
	postorder(tempHeader);
	printf("\n");
	printf("Levelwise: ");
	levelwise(tempHeader);
	printf("\n");

	return 0;
}

二叉树是最简单的一种树,一个父节点最多只有两个子节点。二叉树的创建在这里使用的是层序遍历,即利用广度优先搜索的思想和队列的结构对每一层进行构建。先将根节点加入队列,对根节点的左右子树进行检索,若存在就加入队列,若不存在就为空指针。再重复这个流程,直到队列里所有元素都访问完。

二叉树的遍历在这里使用的是前序,中序,后序三种遍历方式。前序遍历就是先打印根节点,再访问左子树并打印,访问到没有左子树的节点时判断是否有右子树,若无,返回上层指令判断有无右子树,若有则重复访问左子树的类似过程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值