C语言中的AVL树重新平衡和遍历错误

时间:2013-04-12 18:35:04

标签: c runtime-error traversal infinite-loop avl-tree

CODE:

int main(void) //AVL Tree

{
    TP TreeP=(TP)malloc(sizeof(struct AVLTree)); //TP is a Pointer to a Tree Structure
    InitializeTree(TreeP); //Initializes the Tree
    FILE * FP;
    int I=0;
    FP = fopen("Data.txt", "r"); //Open the file stream
    while(!feof(FP))
    {
        fscanf(FP,"%d",&I); //read integers from file
        InTree(I,&(TreeP->Root),TreeP); //input those integers onto nodes, which are places on the tree
    }
    for (I=0;I<(TreeP->Size);I++)
    {
        SearchNode(&(TreeP->Root)); //searches for the first occurrence of a node disobeying the height balance condition
    }
    Traversal(&(TreeP->Root)); //traverse the Tree outputting all node in Pre Order
    close(FP);
    return 0;
}

void SearchNode(NP * Root)
{
    if ((*Root)!=NULL) //first checks if the root exists
    {
        if (((*Root)->Left)!=NULL && (((*Root)->Right)!=NULL)) //then if it has left and right nodes 
        {
            if(((*Root)->Left->Height) > (((*Root)->Right->Height)+1) || ((*Root)->Right->Height) > (((*Root)->Left->Height)+1)) //checks if they disobey the condition
            {FindNode(Root);}
            else //if they don't it continues to traverse the tree
            {
                SearchNode(&((*Root)->Left));
                SearchNode(&((*Root)->Right));
            }
        }
    }
}

//RR -> Right Right, LL -> Left Left, LR -> Left Right, RL -> Right Left

void rrBalance(NP * Root) //the rr and ll balancers simply change the pointers

{

    NP * Temp= Root;
    ((*Root)->Right->Left)=((*Temp)->Right);
    Root= &((*Root)->Right);
    ((*Root)->Right)=(*Temp);
    puts("RRB"); //testing

}

void llBalance(NP * Root)

{

    NP * Temp= Root;
    ((*Root)->Left->Right)=((*Temp)->Left);
    Root= &((*Root)->Left);
    ((*Root)->Left)=(*Temp);
    puts("LLB"); //testing

}

void rlBalance(NP * Root) //the double balancers run both an ll and an rr in different orders

{

    rrBalance(&((*Root)->Right));
    llBalance(Root);
    puts("RLB"); //testing

}

void lrBalance(NP * Root)

{

    llBalance(&((*Root)->Left));
    rrBalance(Root);
    puts("LRB"); //testing

}

void leftRebalance(NP * Root) //checks whether ll or lr balance

{

    puts("LR"); //testing
    if ((*Root)->Left->Height > (((*Root)->Right->Height)+1))
    {llBalance(Root);}
    else if ((*Root)->Right->Height > (((*Root)->Left->Height)+1))
    {lrBalance(Root);}

}

void rightRebalance(NP * Root) //checks whether rr or rl balance

{

    if((*Root)->Left->Height > (((*Root)->Right->Height)+1))
    {rlBalance(Root);}
    else if((*Root)->Right->Height > (((*Root)->Left->Height)+1))
    {rrBalance(Root);}
    puts("RR"); //testing purposes

}

void FindNode(NP * Root) //decides whether the node needs a right or left balance function by comparing the heights of the child nodes

{

    if((*Root)->Left->Height > ((*Root)->Right->Height)+1)
    {leftRebalance(&((*Root)->Left));}
    else if ((*Root)->Right->Height > ((*Root)->Left->Height)+1)
    {rightRebalance(&((*Root)->Right));}
    puts("FindNode"); //placed to check whether the function was accessed - for testing purposes

}

- 如果代码显示不当,我很抱歉,我还是Stackoverflow的新手 - 没有包含遍历函数,因为虽然我的问题的一部分在遍历中,但代码工作正常,InTree()函数也是如此,它是用不同的问题编写的,现在工作正常。

问题:程序应该获取文件中的所有整数,将它们放在AVL树上,将它们重新排序为平衡的BST并再次输出。但是,在运行程序时,我的输出没有按预期返回。 我的第一次尝试返回树的前几个节点,然后停留在文件流中的值“76”,这是在无限循环中输出 看到我决定让它在76之前结束(在我的遍历中添加一个exit()函数),我看到输入仍未正确排序

文件输入:1,45,23,76,222,11,0,10,90,11,19,18,43,56,32,22,55,66,77,111,2,7, 88,100,445,667,89,99,455,677,200,23 计划外:1,0,45,23,11,10,2,7,19,18,22,43,32

对我来说,程序输出看起来不正确(什么让它离开的是1不应该是根)...我是如何重新排列指针的?另外,为什么无限循环?

注意:请不要评论我编码的语法是什么,当编译器没有返回任何语法错误时,我不能浪费任何时间进行轻微的重新调整

0 个答案:

没有答案
相关问题