天天看點

使用C語言如何建立一個二叉樹

使用C語言建立一個二叉樹如下

#include<stdio.h>
#include<stdlib.h>
typedef struct tree
{
	int nValue;
	struct tree *pLeft;
	struct tree *pRight;
}Tree;
void CreateTree(Tree **ptree)
{	
	printf("請輸入節點值\n");
	int num;
	scanf("%d",&num);
	if(num == 0)return;
	(*ptree) = (Tree*)malloc(sizeof(Tree));
	(*ptree)->nValue = num;
	(*ptree)->pLeft = NULL;
	(*ptree)->pRight = NULL;
	CreateTree(&((*ptree)->pLeft));
	CreateTree(&((*ptree)->pRight));
}
int main()
{
	Tree *ptree = NULL;
	CreateTree(&ptree);
	return 0;
}

           

繼續閱讀