二叉链表的创建

完成下列程序,该程序以二叉链表作存储结构,构建如图1所示的二叉树,并依次进行二叉树的前序、中序、后序及层次遍历。

#include"stdio.h"
#include"stdlib.h"
typedef  char  DataType;
typedef struct Node 
{
	DataType data;
	struct Node *leftChild;	
	struct Node *rightChild;
}BiTreeNode;

void  PrintData(DataType x)
{
	printf("%c",x);
}

/*按先序序列建立一棵二叉树*/
void CreatBiTree(BiTreeNode **T)
{
	char  ch;
	scanf("%c",&ch);
	if(ch==' ')   {*T=NULL;return;}
	else
	{
		if(!(*T=(BiTreeNode*)malloc(sizeof(BiTreeNode)))){printf("Overflow.\n");exit(0);}
		(*T)->data =ch;
		CreatBiTree(&((*T)->leftChild ));
		CreatBiTree(&((*T)->rightChild ));
	}
}

void PreOrder(BiTreeNode *t, void (*Visit)(DataType item))
/*前序遍历二叉树t,访问操作为Visit()函数*/
{
	if(t != NULL)
	{
		Visit(t->data);
		PreOrder(t->leftChild, Visit);
		PreOrder(t->rightChild, Visit);
	}
}

void InOrder(BiTreeNode *t, void (*Visit)(DataType item)) /*中序t */
{
	if(t != NULL)
	{
		InOrder(t->leftChild, Visit);
		Visit(t->data);
		InOrder(t->rightChild, Visit);
	}
}
 
void PostOrder(BiTreeNode *t, void (*Visit)(DataType item)) /*后序 */
{
	if(t != NULL)
	{
		PostOrder(t->leftChild, Visit);
		PostOrder(t->rightChild, Visit);
		Visit(t->data);
	}
}


int BTreeDepth(BiTreeNode *T)
{
	int leftdep,rightdep;
	if (T==NULL)  return(0);
	else
	{
		leftdep=BTreeDepth(T->leftChild);
		rightdep=BTreeDepth(T->rightChild);
		if (leftdep>rightdep)
			return(leftdep+1);
		else
			return(rightdep+1);
	}
}



int NodeCount(BiTreeNode *T)
{
	if (T==NULL)  return(0);
	else 
		return(NodeCount(T-> leftChild)+NodeCount(T-> rightChild)+1);
}


int LeafCount(BiTreeNode *T)
{
	if (T==NULL)  return(0);
	else
		if (T-> leftChild ==NULL && T-> rightChild ==NULL) return(1);
		else return(LeafCount(T-> leftChild)+LeafCount(T-> rightChild));
}


int OneChildCount(BiTreeNode *T)
{
	if (T==NULL)   return(0);
	else
		if((T-> leftChild ==NULL && T-> rightChild!=NULL) ||(T-> leftChild!=NULL && T-> rightChild ==NULL))
			return(OneChildCount(T-> leftChild)+OneChildCount(T-> rightChild)+1);
        else
			return(OneChildCount(T-> leftChild)+OneChildCount(T-> rightChild));
}

int TwoChildCount(BiTreeNode *T)
{
	if (T==NULL) return(0);
	else
		if (T-> leftChild ==NULL || T-> rightChild ==NULL)
			return(TwoChildCount(T-> leftChild)+TwoChildCount(T-> rightChild));
		else
			return(TwoChildCount(T-> leftChild)+TwoChildCount(T-> rightChild)+1);		
}

//主函数在任务一的基础上进行适当的增添
int main()
{
	int choice;
	BiTreeNode *root;

	printf("下面建立一棵二叉树,结点数据输入按先序次序。\n");
	printf("输入数据请注意,每一个非空结点的所有孩子数据都要给出\n");
	printf("若孩子为空,请用空格符表示。\n");
	printf("请输入二叉树结点的数据,这里设定为字符:\n");
	CreatBiTree(&root);

	printf("================================\n");
	printf("1:先序序列:\n");
	printf("2:中序序列:\n");
	printf("3:后序序列:\n");
	printf("4:二叉树的深(高)度:\n");
	printf("5:二叉树的结点数:\n");
	printf("6:二叉树的叶子结点数:\n");
	printf("7:二叉树的度为2的结点数:\n");
	printf("8:二叉树的度为1的结点数:\n");
	printf("其它值则退出:\n");
	printf("================================\n");
	
	
	do
	{
		printf("请输入对应的操作码:");
		scanf("%d",&choice);
		switch(choice)
		{
			case 1:printf("\n先序序列为:");
				   PreOrder(root,PrintData);printf("\n");break;
			case 2:printf("\n中序序列为:");
				   InOrder(root,PrintData);printf("\n");break;
			case 3:printf("\n后序序列为:");
				   PostOrder(root,PrintData);printf("\n");break;
			case 4:printf("二叉树的深度是:%d\n",BTreeDepth(root));break;
			case 5:printf("二叉树的结点数是:%d\n",NodeCount(root));break;
			case 6:printf("二叉树的叶子数是:%d\n",LeafCount(root));break;
			case 7:printf("二叉树的度为2的结点数是:%d\n",TwoChildCount(root));break;
			case 8:printf("二叉树的度为1的结点数是:%d\n",OneChildCount(root));break;
		}
	}while(choice>0&&choice<9);
	return 0;
}

这里是引用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烈焰星辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值