#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <time.h>
struct node
{
long data; //存放數據的一個或者多個項
long count;
struct node *pLeft; //左孩子 指向一個二叉樹
struct node *pRight; //右孩子 指向一個二叉樹
};
struct node * CreateNode(long value)
{
struct node *p=malloc(sizeof(struct node));
p->pLeft=p->pRight=NULL;
p->data=value;
p->count=1;
}
struct node * AddNode(struct node * pNode,long v)
{
//情況一 pNode==NULL
if (pNode==NULL)
{
return CreateNode(v);
}
// pNode->data=v
if (pNode->data==v)
{
pNode->count++;
return pNode;
}
//v大於結點數據
if (v>pNode->data)
{
if (pNode->pRight==NULL)
{
pNode->pRight=CreateNode(v);
return pNode->pRight;
}else return AddNode(pNode->pRight,v); //遞歸調用
}
//v小於 結點數據
if (v<pNode->data)
{
if (pNode->pLeft==NULL)
{
pNode->pLeft=CreateNode(v);
return pNode->pLeft;
}else return AddNode(pNode->pLeft,v); //遞歸調用
}
return NULL;
}
int main(void)
{
struct node*root;
long v,i;
printf("請輸入二叉樹根結點數值:");
scanf("%d",&v);
root=CreateNode(v);
for (i=0;i<=10;i++)
{
AddNode(root,i);
}
getchar();
return 0;
}