#include<stdio.h>
#include<stdlib.h>
typedef struct _Tree
{
int nValue;
struct _Tree *pLeft;
struct _Tree *pRight;
}Tree;
void InsertTree(Tree **tree,int nInsertValue)
{
if(*tree==NULL)
{
(*tree)=(Tree*)malloc(sizeof(Tree*));
(*tree)->nValue=nInsertValue;
(*tree)->pLeft=NULL;
(*tree)->pRight=NULL;
}
else
{
if((*tree)->nValue>nInsertValue)
{
if((*tree)->pLeft==NULL)
{
(*tree)->pLeft=(Tree*)malloc(sizeof(Tree*));
(*tree)->pLeft->nValue=nInsertValue;
(*tree)->pLeft->pLeft=NULL;
(*tree)->pLeft->pRight=NULL;
}
else
{
InsertTree(&(*tree)->pLeft,nInsertValue);
}
}
else
{
if((*tree)->pRight==NULL)
{
(*tree)->pRight=(Tree*)malloc(sizeof(Tree*));
(*tree)->pRight->nValue=nInsertValue;
(*tree)->pRight->pLeft=NULL;
(*tree)->pRight->pRight=NULL;
}
else
{
InsertTree(&(*tree)->pRight,nInsertValue);
}
}
}
}
//void InsertTree(Tree **tree,int nInsertValue)
//{
// if(*tree==NULL)
// {
// (*tree)=(Tree*)malloc(sizeof(Tree*));
// (*tree)->nValue=nInsertValue;
// (*tree)->pLeft=NULL;
// (*tree)->pRight=NULL;
// return ;
// }
// else
// {
// while(1)
// {
// if((*tree)->nValue<nInsertValue)
// {
// if((*tree)->pRight==NULL)
// {
// (*tree)->pRight=(Tree*)malloc(sizeof(Tree*));
// (*tree)->pRight->nValue=nInsertValue;
// (*tree)->pRight->pLeft=NULL;
// (*tree)->pRight->pRight=NULL;
// return ;
// }
// tree=&((*tree)->pRight);
// }
// else
// {
// if((*tree)->pLeft==NULL)
// {
// (*tree)->pLeft=(Tree*)malloc(sizeof(Tree*));
// (*tree)->pLeft->nValue=nInsertValue;
// (*tree)->pLeft->pLeft=NULL;
// (*tree)->pLeft->pRight=NULL;
// return ;
// }
// tree=&((*tree)->pLeft);
// }
// }
// }
//}
void CreateTree(Tree **tree,int *arr,int nLength)
{
int i;
for(i=0;i<nLength;i++)
{
InsertTree(tree,arr[i]);
}
}
//void MidPrintf(Tree *tree)
//{
// if(tree)
// {
// MidPrintf(tree->pLeft);
// printf("%d ",tree->nValue);
// MidPrintf(tree->pRight);
// }
//}
void InsertList(Tree *tree,Tree **pHead,Tree **pEnd)
{
if(tree)
{
InsertList(tree->pLeft,pHead,pEnd);
if(*pHead==NULL)
{
(*pHead)=tree;
(*pEnd)=tree;
}
else
{
(*pEnd)->pRight=tree;
tree->pLeft=(*pEnd);
(*pEnd)=tree;
}
InsertList(tree->pRight,pHead,pEnd);
}
}
void CreateList(Tree **tree)
{
Tree *pHead=NULL;
Tree *pEnd=NULL;
InsertList((*tree),&pHead,&pEnd);
(*tree)=pHead;
}
void Printf(Tree *tree)
{
while(tree->pRight!=NULL)
{
printf("%d ",tree->nValue);
tree=tree->pRight;
}
printf("%d\n",tree->nValue);
while(tree!=NULL)
{
printf("%d ",tree->nValue);
tree=tree->pLeft;
}
}
int main()
{
int arr[10]={4,3,2,5,6,7,8,1,9,0};
Tree *tree=NULL;
CreateTree(&tree,arr,10);
CreateList(&tree);
Printf(tree);
system("pause");
return 0;
}