C 二叉树代码赢得';我不能正常工作

C 二叉树代码赢得';我不能正常工作,c,tree,binary-tree,binary-search-tree,C,Tree,Binary Tree,Binary Search Tree,我做的一个代码是通过插入函数nodeT*insertT(nodeT**pp,int-iValue)向二叉树输入值。如果没有值顺序的重要性,我的函数就是按顺序将值插入到树中,避免重复,并正确打印 Insert Values for Binary Tree(-1 to stop)4 6 8 10 12 14 16 -1 The contents of the tree are: 4 6 8 10 12 14 16 Input a number whitin the range of th

我做的一个代码是通过插入函数nodeT*insertT(nodeT**pp,int-iValue)向二叉树输入值。如果没有值顺序的重要性,我的函数就是按顺序将值插入到树中,避免重复,并正确打印

    Insert Values for Binary Tree(-1 to stop)4 6 8 10 12 14 16 -1
The contents of the tree are: 
4 6 8 10 12 14 16 
Input a number whitin the range of the tree to receive the kth Smallest
5
the smallest 5th element is: 12 
count of node: 7
但是,当我输入的值没有排序时,函数CountNode和第k个最小元素将不会返回所需的输出。此外,二叉树的显示是正确的。比如说

Insert Values for Binary Tree(-1 to stop)10 14 6 8 12 4 16 -1
The contents of the tree are: 
4 6 8 10 12 14 16 
Input a number whitin the range of the tree to receive the kth Smallest
5
the smallest 5th element is: 0 
count of node: 3
我使用了以下函数,inserT,k_

nodeT *insertT(nodeT  **pp, int iValue) 
{

    if(*pp == NULL)
    {
        *pp = allocateNodeT(iValue);
        return *pp;
    }
    if(iValue == (*pp)->iValue)
    {
        return *pp;
    }

    if(iValue < (*pp)->iValue)
    {
        return insertT(&(*pp)->pLeft, iValue);
    }
    else //(iValue > (*pp)->iValue)
    {
         return insertT(&(*pp)->pRight, iValue);
    }

}
int k_smallest(nodeT *pRoot, int iKey)
{
    int iSave;
    if(pRoot)
    {
        nodeT* pTraverse;
        pTraverse =pRoot;
        while(pTraverse)
        {
            if((pTraverse->iCount +1) == iKey)
            {
                iSave = pTraverse->iValue;
                break;
            }
            else if( iKey > pTraverse->iCount)
            {
                iKey = iKey-(pTraverse->iCount +1);
                pTraverse = pTraverse->pRight;
            }
            else
            {
                pTraverse = pTraverse->pLeft;
            }
        }
    }
    return iSave;
}
nodeT*insertT(nodeT**pp,intivalue)
{
如果(*pp==NULL)
{
*pp=分配数据(iValue);
返回*pp;
}
如果(iValue==(*pp)->iValue)
{
返回*pp;
}
如果(iValue<(*pp)->iValue)
{
返回insertT(&(*pp)->pLeft,iValue);
}
else/(iValue>(*pp)->iValue)
{
返回插图(&(*pp)->pRight,iValue);
}
}
int k_最小(节点*pRoot,int iKey)
{
INTISAVE;
如果(pRoot)
{
节点*pTraverse;
pTraverse=pRoot;
while(pTraverse)
{
如果((pTraverse->iCount+1)=iKey)
{
iSave=pTraverse->iValue;
打破
}
else if(iKey>pTraverse->iCount)
{
iKey=iKey-(pTraverse->iCount+1);
pTraverse=pTraverse->pRight;
}
其他的
{
pTraverse=pTraverse->pLeft;
}
}
}
回报我;
}

下面的代码是我创建BST的解决方案。它运行时没有错误,我相信代码正确分配了内存。然而,正如我在问题中所说的,当我输入非顺序值并调用函数
k_minimate
height
时,输出与按顺序输入值时不同(我在第一篇文章中有一个示例)

intmain(intargc,char*argv[])
{
char-szKey;
int iValue、iStop、iKey、iKth、iCount、iSumPath、iK;
iStop=-1;
节点*t1,**t2;
t1=NULL;
t2=&t1;
Tree=newTree();
//询问二叉树的值并以-1停止
printf(“为二叉树插入值(-1停止)”;
做
{
scanf(“%d”&iValue);
如果(iValue>0)
{
插入(t2,iValue);
}
}while(iValue!=iStop);
//显示树callinf函数DisplayTree的内容
printf(“树的内容是:\n”);
预打印(t1,0);
//为第k个最小值输入树范围内的数字
printf(“\n在树的范围内输入一个数字,以接收第k个最小的\n”);
scanf(“%d”和&iKey);
iKth=k_最小值(t2,iKey);
//打印第k个最小元素
printf(“最小的%dth元素是:%d\n”,iKey,iKth);
//计算节点数
iCount=iheight(t2);
printf(“节点计数:%d\n”,iCount);
printf(“\n”);
自由节点(t2);
}
/********************分配数据********************************************
节点集*AllocateNode(元素值))
目的:
此函数用于分配二叉树节点,分配元素值,
并将指针初始化为NULL。
它返回指向新分配节点的指针。。
参数:
I元素值,其中地址为搜索。
笔记:
返回值:
pNew
**********************************************************************/
节点*allocateNodeT(内部变量)
{
nodeT*pNew=(nodeT*)malloc(sizeof(nodeT));
pNew->iValue=iValue;
pNew->pLeft=NULL;
pNew->pRight=NULL;
返回pNew;
}
/********************插入********************************************
节点*插入(节点**pp,内部)
目的:
将iValue递归地插入到树中
参数:
I nodeT*pp指定根
I要插入到树上的值。
笔记:
**********************************************************************/
节点*插入(节点**pp,内部版本)
{
如果(*pp==NULL)
{
*pp=分配数据(iValue);
返回*pp;
}
如果(iValue==(*pp)->iValue)
{
返回*pp;
}
如果(iValue<(*pp)->iValue)
{
返回insertT(&(*pp)->pLeft,iValue);
}
else/(iValue>(*pp)->iValue)
{
返回插图(&(*pp)->pRight,iValue);
}
}

您是否可以向我们展示二叉树和节点的代码?@jrhee17我已经提交了代码的其余部分。请添加一些文本,以澄清这是否是您所问问题的解决方案,或者您是否在问题中发布了更多代码?如果您要发布更多的代码以回应评论,请编辑原始问题并添加,而不是在答案中发布。@gariepy该文本是否能更好地解释它?
int main(int argc, char*argv[])
    {
        char szKey;
        int iValue, iStop, iKey, iKth, iCount, iSumPath, iK;
        iStop = -1;
        nodeT *t1, **t2;
        t1 = NULL;
        t2 = &t1;
    Tree tree = newTree();

    //Ask for values for binary tree and stop with -1
    printf("Insert Values for Binary Tree(-1 to stop)");
    do
    {
        scanf("%d", &iValue);
        if(iValue > 0)
        {
            insertT(t2,iValue);
        }
    }while(iValue != iStop);

    // Show contents of the tree callinf function DisplayTree
    printf("The contents of the tree are: \n");
    prettyPrintT(t1, 0);

    //input a number within range of tree for kth smallest
    printf("\nInput a number whitin the range of the tree to receive the kth Smallest\n");
    scanf("%d", &iKey);
    iKth = k_smallest(t2, iKey);
    //Print the kth smallest element
    printf("the smallest %dth element is: %d \n", iKey, iKth);

    //Count the nodes
    iCount = iheight(t2);
    printf("count of node: %d\n", iCount);

    printf("\n");
    freeNode(t2);
}
    /******************** allocateNodeT ********************************************
        NodeT *allocateNodeT(Element value))
        Purpose:
            This function allocates a binary tree node, assigns the element value,
            and initializes the pointers to NULL.
            It returns a pointer to the newly allocated node..
        Parameters:
            I Element value   value where address is search.
        Notes:

        Return Value:
            pNew
        **********************************************************************/
        nodeT *allocateNodeT(int iValue)
        {
            nodeT *pNew = (nodeT*) malloc(sizeof(nodeT));
            pNew->iValue = iValue;
            pNew->pLeft = NULL;
            pNew->pRight =NULL;
            return pNew;
        }
        /******************** insertT ********************************************
        nodeT *insertT(nodeT  **pp, int iValue))
        Purpose:
            insert iValue into the tree recursively
        Parameters:
            I nodeT *pp                 Specifies the Root
            I int iValue                value to be inserted on the tree.
        Notes:

        **********************************************************************/
        nodeT *insertT(nodeT  **pp, int iValue) 
        {

            if(*pp == NULL)
            {
                *pp = allocateNodeT(iValue);
                return *pp;
            }
            if(iValue == (*pp)->iValue)
            {
                return *pp;
            }

            if(iValue < (*pp)->iValue)
            {
                return insertT(&(*pp)->pLeft, iValue);
            }
            else //(iValue > (*pp)->iValue)
            {
                 return insertT(&(*pp)->pRight, iValue);
            }

        }