树的一些基本用法

我们用先序创建树,算出它的先序,中序和后序,然后计算他的结点和叶子结点

最后计算树的深度。

 

目录

                                                                       树的基本用法

  • 先序
  • 中序
  • 后序

 

先序遍历就是先遍历根结点再遍历左右孩子,所以这课树的先序遍历就是

ABC##D#E#G##F###

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

#define MAX 20
typedef struct BTNode{
	char data;
	struct BTNode *lchild;
	struct BTNode *rchild;
}*BiTree;

void createBiTree( BiTree *t ) {
	char s;
	BiTree q;
	s = getchar();
	if ( s == '#') {
		*t = NULL;
		return;
	}
	q = ( BiTree )malloc( sizeof( struct BTNode ) );
	if ( q == NULL ) {
		printf("Memony alloc failure!");
		exit(0);
	}
	q->data = s;
	*t = q;
	createBiTree( &q->lchild );
	createBiTree( &q->rchild );
}

void preOrder( BiTree p ) {		//先序遍历二叉树
	if ( p != NULL ) {
		printf("%c", p->data );
		preOrder( p->lchild );
		preOrder ( p->rchild );
	}
}

void InOrder( BiTree p ) {//中序遍历二叉树
	if ( p != NULL ) {
		InOrder ( p->lchild );
		printf("%c", p->data );
		InOrder( p->rchild );
	}
}

//后序遍历二叉树
void PostOrder( BiTree p ) {
	if ( p != NULL ) {
		PostOrder( p->lchild );
		PostOrder( p->rchild );
		printf("%c", p->data );
	}
}

//先序遍历的非递归算法
void preOrder_n( BiTree p ) {
	BiTree stack[MAX],q;
	int top = 0,i;
	for ( i = 0; i < MAX; i++ )
		stack[i] = NULL;
	q = p;
	while ( q != NULL ) {
		printf("%c",q->data );
		if ( q->rchild != NULL )
			stack[top++] = q->rchild;
		if ( q->lchild != NULL )
			q = q->lchild;
		else
			if ( top > 0 )
				q = stack[--top];
			else
				q = NULL;
	}
}

//释放二叉树
void release( BiTree t ) {
	if ( t != NULL ) {
		release( t->lchild );
		release( t->rchild );
		free(t);
	}
}

//结点个数
int NodeCount( BiTree t ) {
	if ( t == NULL )
		return 0;
	else
		return NodeCount( t->lchild ) + NodeCount( t->rchild ) + 1;
}

//叶子结点
int LeafNode( BiTree t ) {
	if ( t == NULL )
		return 0;
	if ( t->lchild == NULL && t->rchild == NULL )
		return 1;
	return ( LeafNode( t->lchild ) + LeafNode( t->rchild ) );
}

//计算树的深度
int Depth( BiTree t ) {
	int n = 0, m = 0;
	if ( t == NULL )
		return 0;
	else {
		m = Depth( t->lchild );
		n = Depth ( t->rchild );
		if ( m > n )	return ( m + 1 );
		else	return ( n + 1 );
	}
}

int main()
{
	BiTree t = NULL;
	printf("\nplease input data:(exit for #)\n");
	createBiTree( &t );
	int ret = 0;
	ret = NodeCount( t );
	printf("\n\nthe tree number is: %d", ret );
	int ret2 = 0;
	ret2 = LeafNode( t );
	printf("\n\nthe Leaf Node is: %d ", ret2 );
	int ret3 = 0;
	ret3 = Depth( t );
	printf("\n\nthe tree depth is: %d", ret3 );
	printf("\n\nPreOrder the tree is:");
	preOrder( t );
	printf("\n\nInOrder the tree is:");
	InOrder( t );
	printf("\n\nPostOrder the tree is:");
	PostOrder( t );
	printf("\n\n先序遍历序列(非递归) :");
	preOrder_n( t );
	release( t );

	return 0;
}

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值