思路:每来一个节点,都从头开始判断是属于左子树还是右子树,假如判断属于左子树,(然后再用左子树的根节点来判断属于左子树中的左子树还是右子树)----->适合用递归,直到走到叶子节点,再插入
用递归 str1 为先序
LinkTree *dd(LinkTree *r)
{
return r;
}
LinkTree *CreateLinkTree(LinkTree *root,TElemType *str1,TElemType *str2)
{
LinkTree *r = root;
r ->data = *str1;
str1++;
while(*str1)
{
r = root;
LinkTree *pnew = (LinkTree *)malloc(sizeof(LinkTree));
pnew ->data = *str1;
pnew ->lchild = pnew ->rchild = NULL;
pnew ->num = pnew ->Same_Size = 0;
while(1)
{
if(r ->data > *str1 && r ->lchild)
{
r = dd(r ->lchild);
continue;
}
else if(r ->data > *str1 && !r ->lchild)
{
r ->lchild = pnew;
break;
}
if(r ->data < *str1 && r ->rchild)
{
r = dd(r ->rchild);
}
else if(r ->data < *str1 && !r ->rchild)
{
r ->rchild = pnew;
break;
}
}
str1++;
}
return root;
}