数据结构初阶之二叉树(5)

目录

二叉树的创建与遍历

二叉树的销毁

二叉树的层序遍历

判断一棵树是否是完全二叉树


二叉树的创建与遍历

之前学习过二叉树的遍历,其实遍历的思路还可以用作二叉树的创建,假设我们有以下的二叉树前序遍历的结果"abd##eh##i##cf##g##",把这个字符串还原为一个二叉树。

二叉树前序遍历的结果,自然用二叉树前序遍历的思路进行创建最合适。

//创建二叉树节点
typedef char BTDataType;
typedef struct BTNode
{
	BTDataType val;
	struct BTNode* left;
	struct BTNode* right;
}BTNode;
//递归创建二叉树
BTNode * BTcreat(char * s, int *i)
{
	if (s[*i] == '#')
	{
		(*i)++;
		return NULL;
	}
	BTNode * root = (BTNode *)malloc(sizeof(BTNode));
	assert(root);
	root->val = s[*i];
	(*i)++;
	root->left = BTcreat(s, i);
	root->right = BTcreat(s, i);
	return root;
}

二叉树的销毁

二叉树的销毁需要依次销毁节点,所以根节点应当是最后销毁的,所以最符合的是后续遍历的顺序(左节点--->右节点--->根节点)。

二叉树销毁代码

void BTreeDestory(BTNode * root)
{
	if (root == NULL)
	{
		return;
	}
	BTreeDestory(root->left);
	BTreeDestory(root->right);
	free(root);
}

二叉树的层序遍历

层序遍历是按照二叉树每一层进行遍历,进行层序遍历需要使用队列,利用队列先进先出的特性。

 层序遍历代码

order.h

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>


//二叉树节点
typedef struct BTNode
{
	char val;
	struct BTNode *left;
	struct BTNode *right;
}BTNode;

//队列节点
typedef struct QueueNode
{
	BTNode * root;
	struct QueueNode * next;
}QueueNode;

//队列
typedef struct QL
{
	QueueNode * head;
	QueueNode * tail;
}QL;

//前序创建二叉树
BTNode* BTClear(char *ch, int * flag);
//前序打印二叉树
void BTpreoprint(BTNode *root);
//队列初始化
void QueueInit(QL * Queue);
//入队
void QueuePush(QL * Queue, BTNode * root);
//判空
bool QueueEmpty(QL * Queue);
//出队
void QueuePop(QL * Queue);
//层序打印
void leverprint(QL * Queue);
//弹出元素
BTNode* QueueFront(QL * Queue);

order.c

#include"leverorder.h"

//前序遍历创建二叉树
BTNode* BTClear( char *ch, int * flag)
{
	if (ch[*flag] == '#')
	{
		(*flag)++;
		return NULL;
	}
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
	if (newnode == NULL)
	{
		perror("malloc BTNode is fall");
		return;
	}
	newnode->val = ch[*flag];
	(*flag)++;
	newnode->left = BTClear(ch, flag);
	newnode->right = BTClear(ch, flag);
	return newnode;
}

//前序打印二叉树
void BTpreoprint(BTNode *root)
{
	if (root == NULL)
	{
		printf("# ");
		return;
	}
	printf("%c ", root->val);
	BTpreoprint(root->left);
	BTpreoprint(root->right);
}



//队列初始化
void QueueInit(QL * Queue)
{
	assert(Queue);
	Queue->head = Queue->tail = NULL;
}

//入队
void QueuePush(QL * Queue, BTNode * root)
{
	assert(Queue);
	QueueNode * newnode = (QueueNode *)malloc(sizeof(sizeof(QueueNode)));
	assert(newnode);
	newnode->root = root;
	newnode->next = NULL;
	if (Queue->head == NULL)
	{
		Queue->head = Queue->tail = newnode;
	}
	else
	{
		Queue->tail->next = newnode;
		Queue->tail = Queue->tail->next;
	}
}

//判空
bool QueueEmpty(QL * Queue)
{
	assert(Queue);
	return Queue->head == NULL;
}

//出队
void QueuePop(QL * Queue)
{
	assert(Queue);
	assert(!QueueEmpty(Queue));
	if (Queue->head == Queue->tail)
	{
		Queue->tail = NULL;
	}
	QueueNode * del = Queue->head;
	Queue->head = Queue->head->next;
	del=NULL;
}
//弹出元素
BTNode* QueueFront(QL * Queue)
{
	assert(Queue);
	assert(!QueueEmpty(Queue));
	return Queue->head->root;
}


//层序打印
void leverprint(QL * Queue)
{
	assert(Queue);
	assert(!QueueEmpty(Queue));
	if (Queue->head->root == NULL)
	{
		printf("# ");
	}
	else
	{
		printf("%c ", Queue->head->root->val);
	}

}

test.c

#include"leverorder.h"

void test()
{
	BTNode *root;
	char ch[] = "abd##e##cf##g##";
	int flag = 0;
	root = BTClear(ch, &flag);
	BTpreoprint(root);
	printf("\n");
	QL Queue;
	QueueInit(&Queue);
	QueuePush(&Queue, root);
	while (!QueueEmpty(&Queue))
	{
		BTNode * front = QueueFront(&Queue);
		leverprint(&Queue);
		QueuePop(&Queue);
		if (front != NULL)
		{
			QueuePush(&Queue, front->left);
			QueuePush(&Queue, front->right);
		}
	}
}

int main()
{
	test();
	system("pause");
	return 0;
}

判断一棵树是否是完全二叉树

完全二叉树只有最后一层才会出现空,且空后不会出现数据,所以判断完全二叉树,最好的方法是使用层序遍历,判断空后是否出现元素。

整个判断分为两部:

一、判断整个二叉树直到空出现。

二、空出现以后判断其后是否有其他元素出现。

代码

头文件部分

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>


//二叉树节点
typedef struct BTNode
{
	char val;
	struct BTNode *left;
	struct BTNode *right;
}BTNode;

//队列节点
typedef struct QueueNode
{
	BTNode * root;
	struct QueueNode * next;
}QueueNode;

//队列
typedef struct QL
{
	QueueNode * head;
	QueueNode * tail;
}QL;

//前序创建二叉树
BTNode* BTClear(char *ch, int * flag);
//前序打印二叉树
void BTpreoprint(BTNode *root);
//队列初始化
void QueueInit(QL * Queue);
//入队
void QueuePush(QL * Queue, BTNode * root);
//判空
bool QueueEmpty(QL * Queue);
//出队
void QueuePop(QL * Queue);
//层序打印
void leverprint(QL * Queue);
//弹出元素
BTNode* QueueFront(QL * Queue);
函数部分
#include"leverorder.h"

//前序遍历创建二叉树
BTNode* BTClear( char *ch, int * flag)
{
	if (ch[*flag] == '#')
	{
		(*flag)++;
		return NULL;
	}
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
	if (newnode == NULL)
	{
		perror("malloc BTNode is fall");
		return;
	}
	newnode->val = ch[*flag];
	(*flag)++;
	newnode->left = BTClear(ch, flag);
	newnode->right = BTClear(ch, flag);
	return newnode;
}

//前序打印二叉树
void BTpreoprint(BTNode *root)
{
	if (root == NULL)
	{
		printf("# ");
		return;
	}
	printf("%c ", root->val);
	BTpreoprint(root->left);
	BTpreoprint(root->right);
}



//队列初始化
void QueueInit(QL * Queue)
{
	assert(Queue);
	Queue->head = Queue->tail = NULL;
}

//入队
void QueuePush(QL * Queue, BTNode * root)
{
	assert(Queue);
	QueueNode * newnode = (QueueNode *)malloc(sizeof(sizeof(QueueNode)));
	assert(newnode);
	newnode->root = root;
	newnode->next = NULL;
	if (Queue->head == NULL)
	{
		Queue->head = Queue->tail = newnode;
	}
	else
	{
		Queue->tail->next = newnode;
		Queue->tail = Queue->tail->next;
	}
}

//判空
bool QueueEmpty(QL * Queue)
{
	assert(Queue);
	return Queue->head == NULL;
}

//出队
void QueuePop(QL * Queue)
{
	assert(Queue);
	assert(!QueueEmpty(Queue));
	if (Queue->head == Queue->tail)
	{
		Queue->tail = NULL;
	}
	QueueNode * del = Queue->head;
	Queue->head = Queue->head->next;
	del=NULL;
}
//弹出元素
BTNode* QueueFront(QL * Queue)
{
	assert(Queue);
	assert(!QueueEmpty(Queue));
	return Queue->head->root;
}


//层序打印
void leverprint(QL * Queue)
{
	assert(Queue);
	assert(!QueueEmpty(Queue));
	if (Queue->head->root == NULL)
	{
		printf("# ");
	}
	else
	{
		printf("%c ", Queue->head->root->val);
	}

}
测试部分
#include"leverorder.h"

bool test()
{
	BTNode *root;
	char ch[] = "abd###c#g##";
	int flag = 0;
	root = BTClear(ch, &flag);
	BTpreoprint(root);
	printf("\n");
	QL Queue;
	QueueInit(&Queue);
	QueuePush(&Queue, root);
	while (!QueueEmpty(&Queue))
	{
		BTNode * front = QueueFront(&Queue);
		leverprint(&Queue);
		QueuePop(&Queue);
		if (front != NULL)
		{
			QueuePush(&Queue, front->left);
			QueuePush(&Queue, front->right);
		}
		if (front == NULL)
		{
			break;
		}
	}
	while (!QueueEmpty(&Queue))
	{
		BTNode * front = QueueFront(&Queue);
		leverprint(&Queue);
		QueuePop(&Queue);
		if (front != NULL)
		{
			QueuePush(&Queue, front->left);
			QueuePush(&Queue, front->right);
		}
		if (front != NULL)
		{
			return 0;
		}
		
	}
	return 1;
}

int main()
{
	bool flag =test();
	if (flag)
	{
		printf("是完全二叉树\n");
	}
	else
	{
		printf("不是完全二叉树\n");
	}
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值