AVL Tree实现

时间:2012-02-28 15:34:05

标签: c++ data-structures avl-tree

这是我的AvlTree实现代码,但有一个错误,当我运行时,它表示运行时失败:P未初始化以及如何修复我的代码? 这是

#include "avltree.h";
#include "fatal.h";
//#include<iostream>
#include<stdlib.h>
//using namespace std;
struct AvlNode
{
    ElementType Element;
    AvlTree left;
    AvlTree right;
    int Height;
    };
AvlTree MakeEmpty(AvlTree T)
{

    if (T!=NULL)
    {
        MakeEmpty(T->left);
    MakeEmpty(T->right);
    free(T);
}
     return NULL;
}
Position Find(ElementType X,AvlTree T)
{
    if (T==NULL)
         return NULL;
    if (X<T->Element)
        return Find(X,T->left);
    else
        if (X>T->Element)
            return Find(X,T->right);
        else
            return T;
    }
Position FindMin(AvlTree T)
{

      if (T==NULL)
           return NULL;
      else
          if (T->left=NULL)
              return T;
          else
              return FindMin(T->left);


}

Position FindMax(AvlTree T){
     if(T!=NULL)
         while(T->right!=NULL)
             T=T->right;
     return T;





}

static int Height(Position P)
{
    if (P==NULL)
         return -1;
    else 
        return P->Height;


}

static int Max(int Lhs,int Rhs){
     return Lhs>Rhs?Lhs:Rhs;

}



static Position rotateleft(Position K2)

{

    Position K1;
    K1=K2->left;
    K2->left=K1->right;
    K1->right=K2;
    K2->Height=Max(Height(K2->left),Height(K2->right))+1;
    K1->Height=Max(Height(K1->left),K2->Height)+1;
    return K1;
    }
static Position rotateright(Position K1)
{

    Position K2;
    K2=K1->right;
    K1->right=K2->left;
    K2->left=K1;
    K1->Height=Max(Height(K1->left),Height(K1->right))+1;
    K2->Height=Max(Height(K2->right),K1->Height)+1;
    return K2;




}
static Position DoubleLeft(Position K3)
{
    K3->left=rotateright(K3->left);
    return rotateleft(K3);



}

static Position DoubleRight(Position K1){

    K1->right=rotateleft(K1->right);
    return rotateright(K1);


}
AvlTree Insert(ElementType X,AvlTree T)
{

    if (T==NULL)
    {

        T=(AvlNode*)malloc(sizeof(struct AvlNode));
        if (T==NULL)
            FatalError("Out of space");
        else
        {

            T->Element=X;T->Height=0;
            T->left=T->right=NULL;



        }



    }
    else
        if (X<T->Element)
        {

            T->left=Insert(X,T->left);
            if (Height(T->left)-Height(T->right)==2)
                if (X<T->left->Element)
                    T=rotateleft(T);
                else
                    T=DoubleLeft(T);
                    }

        else
            if (X>T->Element)
            {
                T->right=Insert(X,T->right);
    if(Height(T->right)-Height(T->left)==2)
        if(X>T->right->Element)
            T=rotateright(T);
        else
            T=DoubleRight(T);
}


            T->Height = Max( Height( T->left ), Height( T->right ) ) + 1;
 return T;


}
AvlTree Delete( ElementType X, AvlTree T )

{
    printf( "Sorry; Delete is unimplemented; %d remains\n", X );

    return T;


}
ElementType Retrieve(Position P)

{
    return P->Element;

}

int main(){

    AvlTree T;
    Position P;
    int i;
    int j=0;
    T=MakeEmpty(NULL);
    for (i=0;i<50;i++,j=(j+7)%50)
        T=Insert(j,T);
    for (i=0;i<50;i++)
        if (P==Find(i,T)==NULL || Retrieve(P)!=i)
             printf( "Error at %d\n", i );

    printf( "Min is %d, Max is %d\n", Retrieve( FindMin( T ) ),
 Retrieve( FindMax( T ) ) );
    return 0;
}

4 个答案:

答案 0 :(得分:2)

我认为错误是您使用了double = here:

 if (P==Find(i,T)==NULL || Retrieve(P)!=i)
    printf( "Error at %d\n", i );

我的想法是让P存储Find的值,对吗?

答案 1 :(得分:2)

您尚未包含文件“avltree.h”,因此任何人都无法编译此代码。

然而,有一个突出的错误:

if (P==Find(i,T)==NULL || Retrieve(P)!=i)
     ^^ this is not an assignment

我想你想要:

if ((P=Find(i,T))==NULL || Retrieve(P)!=i)

答案 2 :(得分:2)

我认为这是问题所在:

if (P==Find(i,T)==NULL || Retrieve(P)!=i)

由于P未分配且尚未初始化,因此P将不会NULL,这意味着第一个条件将返回falseRetrieve()将使用未初始化的P调用。

应该是:

if ( (P = Find(i,T)) == NULL || Retrieve(P)!=i)

答案 3 :(得分:1)

int main(){

    AvlTree T ();
    Position P ();