二叉树链式存储及相关操作

      先序创建二叉树、后序递归遍历二叉树、中序非递归遍历二叉树、层次遍历二叉树、非递归算法求二叉树深度、递归算法求二叉树深度、求分支结点个数、判断是否为完全二叉树、先序遍历第k个结点的值、删除值为k的结点及其子树。

#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"

typedef struct TNode           //二叉树结构体定义
{
	int data;
	struct TNode *lchild, *rchild;
}TNode, *Tree;

typedef struct                 //栈结构体定义
{
	Tree data[100];
	int top;
} Stack;

typedef struct queue           //队列结构体定义
{
	Tree data[100];
	int front, rear;
} queue;

int i = 0;

void create(Tree &T);            //先序创建二叉树
void postorder(Tree T);          //后序递归遍历二叉树
void inorder(Tree T);            //中序非递归遍历二叉树
void levelorder(Tree T);         //层次遍历二叉树
void depth1(Tree T);             //非递归算法求二叉树深度
int depth2(Tree T);              //递归算法求二叉树深度
int  count(Tree T);              //求分支结点(度不为0的结点)个数
int Iscomplete(Tree T);          //判断是否为完全二叉树 
void preorder_k(Tree T, int k);  //先序遍历第k个节点的值
void delete_k(Tree T);           //删除值为k的结点及其子树
void search(Tree T, int k);      //寻找值为k的结点

int main()
{
	int k;
	Tree T;

	printf("创建一棵二叉树:\n");
	create(T);
	printf("后序递归遍历二叉树:\n");
	postorder(T);
	printf("\n");
	printf("中序非递归遍历二叉树:\n");
	inorder(T);
	printf("层次遍历二叉树:\n");
	levelorder(T);
	depth1(T);
	k = depth2(T);
	printf("该二叉树高度为:%d\n", k);
	k = count(T);
	printf("该二叉树分支结点个数为:%d\n", k);
	k = Iscomplete(T);
	if(k == 1)
		printf("该树是完全二叉树!\n");
	else
		printf("该树不是完全二叉树!\n");
	printf("你想知道第几个结点的值?请任意输入一个1-7的正整数。\n");
	scanf("%d", &k);
	preorder_k(T, k);
	printf("你想删除值为几的结点及其子树?请任意输入一个1-7的正整数。\n");
	scanf("%d", &k);
	search(T, k);
	printf("后序递归遍历二叉树:\n");
	postorder(T);
	printf("\n");

	return 1;
}

void create(Tree &T)          //先序创建二叉树
{
	int a;
	scanf("%d", &a);
	if(a != 0)
	{
		T = (Tree)malloc(sizeof(TNode));
		T->data = a;
		create(T->lchild);
		create(T->rchild);
	}
	else
		T=NULL;
}

void postorder(Tree T)           //后序递归遍历二叉树
{
	if(T != NULL)
	{
		postorder(T->lchild);
		postorder(T->rchild);
		printf("%d ", T->data);
	}
}

void inorder(Tree T)            //中序非递归遍历二叉树
{
	Stack S;
	S.top = -1;
	Tree p = T;

	while(p != NULL || S.top != -1)
	{
		while(p != NULL)
		{
			S.data[++S.top] = p;
			p = p->lchild;
		}
		if(S.top != -1)
		{
			p = S.data[S.top--];
			printf("%d ", p->data);
			p = p->rchild;
		}		
	}
	printf("\n");
}

void levelorder(Tree T)         //层次遍历二叉树
{
	queue q;
	q.front =q.rear = 0;
	Tree p = T;
	q.data[q.rear++] = p;
	while(q.front < q.rear)
	{	
		p = q.data[q.front++];	
		printf("%d ", p->data);
		if(p->lchild != NULL)
			q.data[q.rear++] = p->lchild;
		if(p->rchild != NULL)
			q.data[q.rear++] = p->rchild;
	}
	printf("\n");
}

void depth1(Tree T)            //非递归求二叉树深度
{
	int h = 0, last = 0;
	queue q;
	q.front = q.rear = -1;
	Tree p = T;
	q.data[++q.rear] = p;

	while(q.front < q.rear)
	{
		p = q.data[q.front++];
		if(p->lchild)
			q.data[++q.rear] = p->lchild;
		if(p->rchild)
			q.data[++q.rear] = p->rchild;
		if(q.front == last)
		{
			++h;
			last = q.rear + 1;
		}
	}
	printf("该二叉树高度为:%d\n", h);
}

int depth2(Tree T)            //递归求二叉树深度
{
	int h1, h2;
	if(T == NULL)
		return 0;
	else
	{
		h1 = depth2(T->lchild);
		h2 = depth2(T->rchild);
	}

	if(h1 >= h2)
		return h1 + 1;
	else
		return h2 + 1;
}

int count(Tree T)             //递归求分支结点个数
{
	int h1, h2;
	if(T == NULL)
		return 0;
	h1 = count(T->lchild);
	h2 = count(T->rchild);
	
	if(T->lchild != NULL || T->rchild != NULL)
		return h1 + h2 +1;
}

int Iscomplete(Tree T)       //判断是否为完全二叉树
{
	Tree p;
	queue q;
	q.front = q.rear = 0;
	if(!T)
		return 1;
	q.data[q.rear++] = T;
	while(q.front < q.rear)
	{
		p = q.data[q.front++];
		if(p)
		{
			q.data[q.rear++] = p->lchild;
			q.data[q.rear++] = p->rchild;
		}
		else
			while(q.front < q.rear)
			{
				p = q.data[q.front++];
				if(p)
					return 0;
			}
	}
	return 1;
}

void preorder_k(Tree T, int k)   //先序遍历第k个结点的值
{
	if(T != NULL)
	{
		if(++i == k)
			printf("第%d个值为%d!\n", i, T->data);
		preorder_k(T->lchild, k);
		preorder_k(T->rchild, k);
	}
}

void delete_k(Tree T)            //删除值为k的结点及其子树
{
	if(T != NULL)
		{
			delete_k(T->lchild);
			delete_k(T->rchild);
			free(T);
		}
}

void search(Tree T, int k)       //寻找值为k的结点
{
	Tree p;
	queue q;
	q.front = q.rear = -1;
	if(T != NULL)
	{
		if(k == T->data)
		{
			delete_k(T);
			exit(1);
		}
		q.data[++q.rear] = T;
		while(q.front < q.rear)
		{
			p = q.data[++q.front];

			if(p->lchild != NULL)
				if(p->lchild->data == k)
				{	
					delete_k(p->lchild);
					p->lchild = NULL;
				}
				else
					q.data[++q.rear] = p->lchild;

			if(p->rchild != NULL)
				if(p->rchild->data == k)
				{
					delete_k(p->rchild);
					p->rchild = NULL;
				}
				else
					q.data[++q.rear] = p->rchild;
		}
	}
}


你可能感兴趣的:(二叉树,递归算法,非递归算法,分支结点个数,删除子树)