二叉树的基本操作

本次写了一些二叉树的基础操作代码如下:

头文件如下:

#pragma once
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <stdlib.h>
//struct TreeNode
//{
//	// data;
//	TNDataType data;
//	//struct TreeNode* childs[N];
//	SeqList childs; //->struct TreeNode*
//};
typedef char BTDataType;

typedef struct BTNode
{
	BTDataType _data;
	struct BTNode* _left;
	struct BTNode* _right;
}BTNode;


// a是一个前序遍历的数组
BTNode* BTCreate(BTDataType* a, int n, int* pi);
BTNode* BTBuyNode(BTDataType x);
void BTDestory(BTNode** root);

int BTSize(BTNode* root);
int BTLeafSize(BTNode* root);
int BTLevelKSize(BTNode* root, int k);
int BTHeight(BTNode* root);
BTNode* BTFind(BTNode* root, BTDataType x);

// 遍历  递归&非递归
void BTPrevOrder(BTNode* root);
void BTInOrder(BTNode* root);
void BTPostOrder(BTNode* root);
void BTLevelOrder(BTNode* root);
int BTComplete(BTNode* root);

void BTPrevOrderNonR(BTNode* root);
void BTInOrderNonR(BTNode* root);
void BTPostOrderNonR(BTNode* root);

void TestBT();

函数如下:

#include "BinaryTree.h"
#include "Queue.h"
#include "Stack.h"



BTNode* BTCreate(BTDataType* a, int n, int* pi)
{
	if(a[*pi] != '#')
	{
		BTNode* root = BTBuyNode(a[*pi]);
		++(*pi);
		root->_left = BTCreate(a,n,pi);
		++(*pi);
		root->_right = BTCreate(a,n,pi);
		return root;
	}
	else
	{
		return NULL;
	}
}



BTNode* BTBuyNode(BTDataType x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	node->_left = NULL;
	node->_right = NULL;
	node->_data = x;

	return node;
}


void BTDestory(BTNode** root)
{
	BTNode* tree = *root;
	if(tree == NULL)	
	{
		return ;
	}
	BTDestory(&tree->_left);
	BTDestory(&tree->_right);
	free(tree);
	*root = NULL;
}

int BTSize(BTNode* root)
{
	if(root == NULL)
		return 0;
	else
		return BTSize(root->_left) + BTSize(root->_right) + 1;
}
int BTLeafSize(BTNode* root)
{
	if(root == NULL)
		return 0;
	else if(root->_left == NULL && root->_right == NULL)
		return 1;
	else
		return BTLeafSize(root->_left) + BTLeafSize(root->_right);
}
int BTLevelKSize(BTNode* root, int k)
{
	if(root == NULL)
		return 0;
	else if(k == 1)
		return 1;
	else
		return BTLevelKSize(root->_left,k-1) + BTLevelKSize(root->_right,k-1);
}


int BTHeight(BTNode* root)
{
	int leftheight;
	int rightheight;
	if(root == NULL)
	{
		return 0;
	}
	leftheight = BTHeight(root->_left);
	rightheight = BTHeight(root->_right);
	return leftheight > rightheight ? leftheight+1 : rightheight+1;
}


BTNode* BTFind(BTNode* root, BTDataType x)
{
	BTNode* ret = NULL;
	if(root == NULL || root->_data == x)
	{
		return root;
	}
	ret = BTFind(root->_left,x);
	if(ret)
		return ret;
	ret = BTFind(root->_right,x);
	if(ret)
	{
		return ret;
	}
		return NULL;
}

// 遍历  递归&非递归
void BTPrevOrder(BTNode* root)
{
	if(root == NULL)
		return ;
	printf("%c ",root->_data);
	BTPrevOrder(root->_left);
	BTPrevOrder(root->_right);
}



void BTInOrder(BTNode* root)
{
	if(root == NULL)
		return ;
	BTInOrder(root->_left);
	printf("%c ",root->_data);
	BTInOrder(root->_right);
}
void BTPostOrder(BTNode* root)
{
	if(root == NULL)
		return ;
	BTPostOrder(root->_left);
	BTPostOrder(root->_right);
	printf("%c ",root->_data);
}
void BTLevelOrder(BTNode* root)
{
	Queue queue;
	QUDataType front;
	QueueInit(&queue);
	QueuePush(&queue,root);
	while(QueueEmpty(&queue) != 0)
	{
		front = QueueFront(&queue);
		printf("%c ",front->_data);
		QueuePop(&queue);
		if(front->_left != NULL)
		{
			QueuePush(&queue,front->_left);
		}
		if(front->_right != NULL)
		{
			QueuePush(&queue,front->_right);
		}
	}
	printf("\n");
		QueueDestory(&queue);
}

void BTPrevOrderNonR(BTNode* root)
{
	Stack s;
	BTNode* cur;
	BTNode* top;
	StackInit(&s);
	cur = root;
	while(cur || StackEmpty(&s) != 0)//当cur不为空&&栈不为空时
	{
		while(cur)//当cur不为空时
		{
			printf("%c ",cur->_data);
			StackPush(&s,cur);//入栈
			cur = cur->_left;//将cur左子树付给cur
		}	
		top = StackTop(&s);//取栈顶指针
		StackPop(&s);
		cur = top->_right;
	}
	printf("\n");
	StackDestory(&s);
}



void BTInOrderNonR(BTNode* root)
{
	Stack s;
	BTNode* top;
	BTNode* cur;
	StackInit(&s);
	cur = root;
	while(cur || StackEmpty(&s) != 0)
	{
		while(cur)
		{
			StackPush(&s,cur);
			cur = cur->_left;
		}
		top = StackTop(&s);
		StackPop(&s);
		printf("%c ",top->_data);
		cur = top->_right;
	}
	printf("\n");
	StackDestory(&s);
}
void BTPostOrderNonR(BTNode* root)
{
	Stack s;
	BTNode* cur = NULL;
	BTNode* top = NULL;
	BTNode* sign = NULL;
	StackInit(&s);
	cur = root;
	while(cur || StackEmpty(&s) != 0)
	{
		while(cur)
		{
			StackPush(&s,cur);
			cur = cur->_left;
		}
		top = StackTop(&s);
		if(top->_right == NULL || top->_right == sign)//如果右子树为空 || 等于标志
		{
			printf("%c ",top->_data);
			sign = top;//设置一个标志
			StackPop(&s);
		}
		else
		{
			cur = top->_right;
		}
	}
	printf("\n");
	StackDestory(&s);
}




int BTComplete(BTNode* root)
{
	Queue q;
	BTNode* front;
	QueueInit(&q);
	if(root)
	{
		QueuePush(&q,root);
	}
	while(QueueEmpty(&q) != 0)
	{
		front = QueueFront(&q);
		QueuePop(&q);
		if(front)
		{
			QueuePush(&q,front->_left);
			QueuePush(&q,front->_right);
		}
		else
		{
			break;//此时找到第一个为空的子树
		}
	}
	while(QueueEmpty(&q) != 0)//判断之后的结点后有无不为空的结点
	{
		front = QueueFront(&q);
		if(front)
		{	
			QueueDestory(&q);
			return -1;
		}
		else
		{
			QueuePop(&q);
		}
	}
	return 0;
}





void TestBinaryTree();







void TestBinaryTree()
{
	char array[] = {'A', 'B', 'D', '#', '#','G','#', '#', 'C',
		'E', '#', '#', 'F', '#', '#'};
	int k = 0;
	size_t i = 0;
	int ret = 0;
	BTNode* tree = BTCreate(array, sizeof(array)/sizeof(BTDataType), &i);
	printf("树的大小为:%d\n",BTSize(tree));
	printf("叶结点的大小为: %d\n",BTLeafSize(tree));
	/*printf("请输入要查询结点数的行数:");
	scanf_s("%d",&k);
	printf("第k行的结点为: %d\n",BTLevelKSize(tree,k));*/
	BTPrevOrder(tree);
	printf("\n");
	BTPrevOrderNonR(tree);
	BTInOrder(tree);
	printf("\n");
	BTInOrderNonR(tree);
	BTPostOrder(tree);
	printf("\n");
	BTPostOrderNonR(tree);
	BTLevelOrder(tree);
	ret = BTComplete(tree);
	if(ret == 0)
	{
		printf("是完全二叉树\n");
	}
	BTDestory(&tree);
}



void main()
{	
	TestBinaryTree();
}

下面附上所需的栈和队列代码:

//////////////////////////////////////////////////////////////////
//栈.h

#pragma once

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

//typedef int STDataType;
//
//#define N 10
//typedef struct Stack
//{
//	STDataType _a[N];
//	int _top; // 栈顶
//}Stack;



typedef struct BTNode* STDataType;


typedef struct Stack
{
	STDataType* _a;
	int _top;		// 栈顶
	int _capacity;  // 容量 
}Stack;




void StackInit(Stack* ps);
void StackDestory(Stack* ps);

void StackPush(Stack* ps, STDataType x);
void StackPop(Stack* ps);
STDataType StackTop(Stack* ps);
int StackEmpty(Stack* ps);
int StackSize(Stack* ps);
void StackPrint(Stack * ps);


void TestStack();

///////////////////////////////////////////////////


///////////////////////////////////////////////////
//栈.c

#include "Stack.h"

void StackInit(Stack* ps)
{
	assert(ps);
	ps->_a = (STDataType*)malloc(sizeof(STDataType));
	assert(ps->_a);
	ps->_top = 0;
	ps->_capacity = 1;
}

void StackDestory(Stack* ps)
{	
	assert(ps);
	if(ps->_a)
	{
		free(ps->_a);
		ps->_a = NULL;
		ps->_top = 0;
		ps->_capacity = 0;
	}
}


void StackPush(Stack* ps,STDataType x)
{
	assert(ps);
	if(ps->_top == ps->_capacity)
	{
		ps->_a = realloc(ps->_a,2*ps->_capacity*sizeof(STDataType));
		ps->_capacity *= 2;
	}
	ps->_a[ps->_top] = x;
	ps->_top++;
}

void StackPop(Stack* ps)
{
	assert(ps->_a);
	assert(ps->_top > 0);
	ps->_top--;
}

STDataType StackTop(Stack* ps)
{
	assert(ps->_a && ps->_top > -1);
	return ps->_a[ps->_top-1];
}

//空   0
//非空 1
int StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->_top == 0 ? 0 :1;
}

int StackSize(Stack* ps)
{
	return ps->_top;
}

void StackPrint(Stack *ps)
{
	int i;
	if(ps->_top == 0)
	{
		printf("栈为空,无法打印\n");
		return ;
	}
	printf("栈内容为:");
    for(i=ps->_top-1;i>=0;i--)
	{
        printf("%d ",ps->_a[i]);
    }
	printf("\n");
}
////////////////////////////////////////////////


/////////////////////////////////////////////
//队列.h

#pragma once
#include "BinaryTree.h"


typedef struct BTNode* QUDataType;


typedef struct QueueNode
{
	struct QueueNode* _next;
	QUDataType _data;
}QueueNode;


typedef struct Queue
{
	QueueNode* _front; // 队头
	QueueNode* _back;  // 队尾
}Queue;

void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);

QueueNode* BuyQueueNode(QUDataType x);
void QueuePush(Queue* pq, QUDataType x);
void QueuePop(Queue* pq);
QUDataType QueueFront(Queue* pq); 
QUDataType QueueBack(Queue* pq); 
int QueueSize(Queue* pq); 
int QueueEmpty(Queue* pq);
void QueuePrint(Queue* pq);

void TestQueue();

///////////////////////////////////////////



/////////////////////////////////////////////
//队列.c



#include "Queue.h"


void QueueInit(Queue* pq)
{
	assert(pq);
	pq->_back = NULL;
	pq->_front = NULL;
}


void QueueDestory(Queue* pq)
{
	assert(pq);
	while(pq->_front != NULL)
	{
		pq->_back = pq->_front->_next;
		free(pq->_front);
		pq->_front = pq->_back;
	}
	pq->_front = pq->_back = NULL;
}
QueueNode* BuyQueueNode(QUDataType x)
{
	QueueNode* newnode = NULL;
	newnode = (QueueNode*)malloc(sizeof(QueueNode));
	newnode->_data = x;
	newnode->_next = NULL;
	return newnode;
}

void QueuePush(Queue* pq, QUDataType x)
{
	QueueNode * node;
	assert(pq != NULL);
	node =  BuyQueueNode(x);
	if(pq->_front == NULL)
	{
		pq->_front = pq->_back = node;
	}
	else
	{
		pq->_back->_next = node;
		pq->_back = node;
	}
}


void QueuePop(Queue* pq)
{
	QueueNode * cur;
	assert(pq != NULL);
	cur = pq->_front->_next;
	free(pq->_front);
	pq->_front = cur;
	if(cur == NULL)
	{
		pq->_back = NULL;
	}
}


QUDataType QueueFront(Queue* pq)
{
	return pq->_front->_data;
}


QUDataType QueueBack(Queue* pq) 
{
	return pq->_back->_data;
}


int QueueSize(Queue* pq)
{
	int size = 0;
	QueueNode* cur = pq->_front;
	while(cur)
	{
		size++;
		cur = cur->_next;
	}
	return size;
}
int QueueEmpty(Queue* pq)
{
	return pq->_front == NULL ? 0:1;
}


//////////////////////////////////////////////

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值