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

C语言中的AVL树再平衡和遍历错误,c,runtime-error,traversal,infinite-loop,avl-tree,C,Runtime Error,Traversal,Infinite Loop,Avl Tree,代码: intmain(void)//AVL树 { TP TreeP=(TP)malloc(sizeof(struct AVLTree));//TP是指向树结构的指针 InitializeTree(TreeP);//初始化树 文件*FP; int I=0; FP=fopen(“Data.txt”,“r”);//打开文件流 而(!feof(FP)) { fscanf(FP,'%d',&I);//从文件中读取整数 InTree(I,&(TreeP->Root),TreeP);//将这些整数输入到树上

代码:

intmain(void)//AVL树
{
TP TreeP=(TP)malloc(sizeof(struct AVLTree));//TP是指向树结构的指针
InitializeTree(TreeP);//初始化树
文件*FP;
int I=0;
FP=fopen(“Data.txt”,“r”);//打开文件流
而(!feof(FP))
{
fscanf(FP,'%d',&I);//从文件中读取整数
InTree(I,&(TreeP->Root),TreeP);//将这些整数输入到树上的节点上
}
for(I=0;ISize;I++)
{
SearchNode(&(TreeP->Root));//搜索第一个不符合高度平衡条件的节点
}
遍历(&(TreeP->Root));//遍历树,按预先顺序输出所有节点
关闭(FP);
返回0;
}
无效搜索节点(NP*根)
{
if((*Root)!=NULL)//首先检查根是否存在
{
如果((*Root)->Left)!=NULL&((*Root)->Right)!=NULL))//那么如果它有左和右节点
{
如果(((*根)->左->高)>(((*根)->右->高)+1)|((*根)->右->高)>(((*根)->左->高)+1))//检查是否违反条件
{FindNode(根);}
否则//如果他们不这样做,它将继续遍历树
{
SearchNode(&(*根)->左);
SearchNode(&(*Root)->右);
}
}
}
}
//右-右,左-左,左-右->左-右,右-右->右-左
void rrBalance(NP*Root)//rr和ll平衡器只需更改指针
{
NP*Temp=根;
((*Root)->右->左)=(*Temp)->右;
根=&(*根)->右;
((*根)->右)=(*温度);
puts(“RRB”);//测试
}
无效余额(NP*根)
{
NP*Temp=根;
((*Root)->左->右)=(*Temp)->左;
根=&(*根)->左;
((*根)->左)=(*温度);
puts(“LLB”);//测试
}
void rlBalance(NP*Root)//双平衡器以不同的顺序运行ll和rr
{
rrBalance(&(*根)->右);
llBalance(Root);
puts(“RLB”);//测试
}
无效余额(NP*根)
{
llBalance(&((*根)->左);
rrBalance(根);
puts(“LRB”);//测试
}
void leftRebalance(NP*Root)//检查ll或lr是否平衡
{
puts(“LR”);//测试
如果((*根)->左->高>(((*根)->右->高)+1))
{llBalance(根);}
如果((*根)->右->高>(((*根)->左->高)+1),则为else
{lrBalance(根);}
}
void rightRebalance(NP*Root)//检查rr或rl是否平衡
{
如果((*根)->左->高>(((*根)->右->高)+1))
{rlBalance(根);}
如果((*根)->右->高>(((*根)->左->高)+1),则为else
{rrBalance(Root);}
puts(“RR”);//测试目的
}
void FindNode(NP*Root)//通过比较子节点的高度来决定节点是否需要右平衡函数或左平衡函数
{
如果((*根)->左->高>((*根)->右->高)+1)
{leftRebalance(&((*Root)->Left));}
否则如果((*根)->右->高>((*根)->左->高)+1)
{rightRebalance(&((*Root)->Right));}
puts(“FindNode”);//用于检查函数是否被访问-用于测试目的
}
--很抱歉,如果代码显示不正确,我还是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不应该是根)…我如何重新排列指针有问题吗?还有,为什么是无限循环


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

到目前为止,您尝试了什么?如果你不提供一些指导,发布代码并说“这不起作用”不太可能得到任何人的帮助。此外,告诉人们不要评论你的风格,因为你没有时间,这让人觉得你只是想让我们为你做功课,这(如果是这样的话)是非常不诚实的。很抱歉,我不清楚。并不是说我想让你为我做这件事,事实上,老实说,我更希望有人能指出一个错误,让我自己去改正,因为我更愿意自己去做。老实说,除了查看代码和结果,并在某些时候停止运行之外,我还没有用很多方法对此进行测试,主要是因为我对代码不起作用感到困惑。仔细看,我自己没有注意到任何逻辑错误…我希望有人会有一些想法。。。
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

}