我有一个avl树给我一个错误(我相信一个seg错误),我不明白为什么。发生错误的函数被多次调用而不会导致错误。错误发生在srl函数中。
p1->left = p2->right;
是导致错误的代码
传递到avl树的char *导致此错误是
bgcolor="#c0ffff
为了澄清,添加到avl树的词干对象包含一个char *,我通过使用strcmp进行比较来按字母顺序组织树。
#ifndef Pray4theA_SearchEngine_AVLTree_h
#define Pray4theA_SearchEngine_AVLTree_h
#include <iostream>
#include <cctype>
#include <stdlib.h>
#include "StemObject.h"
using namespace std;
struct node
{
StemObject* element;
node *left;
node *right;
int height;
};
typedef struct node* nodeptr;
class AVLTree
{
public:
void insert(StemObject*,nodeptr &); //got em
StemObject * find(char*,nodeptr &); //got em
void makeempty(nodeptr &); //got em
//void preorder(nodeptr); //got em
//void inorder(nodeptr); //got em
//void postorder(nodeptr); //got em
int max(int,int); //got em
int bsheight(nodeptr); //got em
void del(StemObject*, nodeptr &);
nodeptr srl(nodeptr &);
nodeptr drl(nodeptr &);
nodeptr srr(nodeptr &);
nodeptr drr(nodeptr &);
int nonodes(nodeptr);
};
inline void AVLTree::insert(StemObject * stem,nodeptr &p)
{
std::cout << stem->getWord() << std::endl;
if (p == NULL)
{
p = new node;
p->element = stem;
p->left=NULL;
p->right = NULL;
p->height=0;
if (p==NULL)
{
cout<<"Out of Space\n"<<endl;
}
}
else
{
if (strcmp(stem->getWord(), p->element->getWord())< 0)
{
insert(stem,p->left);
if ((bsheight(p->left) - bsheight(p->right))==2)
{
//std::
if (strcmp(stem->getWord(), p->left->element->getWord()) < 0)
{
p=srl(p);
}
else
{
p = drl(p);
}
}
}
else if (strcmp(stem->getWord(), p->element->getWord()) > 0)
{
insert(stem,p->right);
if ((bsheight(p->right) - bsheight(p->left))==2)
{
if (stem > p->right->element)
if (strcmp(stem->getWord(),p->right->element->getWord()) > 0)
{
p=srr(p);
}
else
{
p = drr(p);
}
}
}
}
int m,n,d;
m=bsheight(p->left);
n=bsheight(p->right);
d=max(m,n);
p->height = d + 1;
}
inline StemObject * AVLTree::find(char* search,nodeptr &p)
{
if (p==NULL)
{
cout<<"Sorry! element not found\n"<<endl;
}
else
{
if (strcmp(search, p->element->getWord())< 0)
{
find(search,p->left);
}
else
{
if (strcmp(search, p->element->getWord())> 0)
{
find(search,p->right);
}
}
}
}
// Make a tree empty
inline void AVLTree::makeempty(nodeptr &p)
{
nodeptr d;
if (p != NULL)
{
makeempty(p->left);
makeempty(p->right);
d=p;
free(d);
p=NULL;
}
}
inline int AVLTree::max(int value1, int value2)
{
return ((value1 > value2) ? value1 : value2);
}
inline int AVLTree::bsheight(nodeptr p)
{
int t;
if (p == NULL)
{
return -1;
}
else
{
t = p->height;
return t;
}
}
inline nodeptr AVLTree:: srl(nodeptr &p1)
{
nodeptr p2;
p2 = p1->left;
std::cout << "srl" << std::endl;
p1->left = p2->right;
p2->right = p1;
p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1;
p2->height = max(bsheight(p2->left),p1->height) + 1;
return p2;
}
inline nodeptr AVLTree:: srr(nodeptr &p1)
{
nodeptr p2;
p2 = p1->right;
p1->right = p2->left;
p2->left = p1;
p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1;
p2->height = max(p1->height,bsheight(p2->right)) + 1;
return p2;
}
inline nodeptr AVLTree:: drl(nodeptr &p1)
{
p1->left=srr(p1->left);
return srl(p1);
}
inline nodeptr AVLTree::drr(nodeptr &p1)
{
p1->right = srl(p1->right);
return srr(p1);
}
inline int AVLTree::nonodes(nodeptr p)
{
int count=0;
if (p!=NULL)
{
nonodes(p->left);
nonodes(p->right);
count++;
}
return count;
}
#endif