二叉树的创建和遍历、删除

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

typedef struct  BinaryTreeNode
{
	struct BinaryTreeNode *pLeft;
	struct BinaryTreeNode *pRight;
	int mValue;
}BINARYTREENODE;

void CreateBinaryTree(BinaryTreeNode **pRoot)
{
	int value = 0;
	printf("Please Input a None Zero Value : ");
	scanf("%d",&value);
	printf("Value = %d\n",value);
	if(value == 0)
	{
		*pRoot = NULL;
		return;
	}
	else
	{
		*pRoot = (BINARYTREENODE *)malloc(sizeof(BINARYTREENODE));
		printf("*pRoot = %x\n",*pRoot);
		printf("*pRoot = %x\n",pRoot);
		(*pRoot)->mValue = value;
		CreateBinaryTree(&((*pRoot)->pLeft));
		CreateBinaryTree(&((*pRoot)->pRight));
	}
}

void DeleteBinaryTree(BINARYTREENODE ** pRoot)
{
	if ((*pRoot)->pLeft == NULL && (*pRoot)->pRight == NULL)
	{	
		printf("Delete Node is %d\n",(*pRoot)->mValue);
		*pRoot = NULL;
		free(*pRoot);		
		return;
	}
	else
	{
		DeleteBinaryTree(&((*pRoot)->pLeft));
		DeleteBinaryTree(&((*pRoot)->pRight));
	}
}
void ShowBinaryTree(BINARYTREENODE * pRoot)
{
	if (NULL!=pRoot)
	{
		printf("%d ",pRoot->mValue);
	}
}

void PerOrder(BINARYTREENODE * pRoot)
{
	if (NULL!=pRoot)
	{
		ShowBinaryTree(pRoot);
		PerOrder(pRoot->pLeft);
		PerOrder(pRoot->pRight);
	}
}

void InOrder(BINARYTREENODE * pRoot)
{
	if (NULL!=pRoot)
	{
		InOrder(pRoot->pLeft);
		ShowBinaryTree(pRoot);		
		InOrder(pRoot->pRight);
	}
}

void PostOrder(BINARYTREENODE * pRoot)
{
	if (NULL!=pRoot)
	{
		PostOrder(pRoot->pLeft);	
		PostOrder(pRoot->pRight);
		ShowBinaryTree(pRoot);	
	}
}

int main()
{
	BINARYTREENODE * pRoot = NULL;

	CreateBinaryTree(&pRoot);
	printf("%x\n",pRoot);	
	printf("Find All Pre Order of Binary Tree : ");
	PerOrder(pRoot);
	printf("\n");

	printf("Find All In Order of Binary Tree : ");
	InOrder(pRoot);
	printf("\n");

	printf("Find All Post Order of Binary Tree : ");
	PostOrder(pRoot);
	printf("\n");

	printf("Before Delete %x\n",pRoot);
	while (pRoot!=NULL)
	{
		DeleteBinaryTree(&pRoot);
	}
	
	printf("After Delete %x\n",pRoot);
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值