二叉树代码无法正常工作

时间:2016-03-29 00:59:16

标签: c tree binary-tree binary-search-tree

我创建的代码是通过Insert函数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和kTH最小元素将不会返回所需的输出。而且,二叉树的显示是正确的。例如

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_smallest

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;
}

1 个答案:

答案 0 :(得分:0)

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

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);
            }

        }