我已经为 avl 编写了一个代码,它在执行过程中执行/中途后给了我以下错误。
我猜它是某种内存泄漏问题。有人可以指出应该修复什么吗?代码块给出了一个弹出分段错误。错误 :Unhandled exception at 0x77B2A710 (ntdll.dll) in ADS Project.exe: 0xC0000005: Access violation writing location 0x00000014.
代码:
{
InputGenerator ip;
int numbers[1000000];
ip.RandomInput(numbers, 1000000);
AVL *avlTree = (AVL *) malloc(1000000* sizeof(AVL *));
AVL *root = NULL;
auto avlOps = make_shared < AVL > ();
int input = 0;
*avlTree = AVL(numbers[0]);
root = avlTree;
avlTree++;
int balanceFac = 0;
input = 1;
while (input != 1000000)
{
//cout << "sorting : ";
//avlOps->InorderPrint(root);
cout << endl;
cout << "Inserting : " << numbers[input] << endl;
*avlTree = AVL(numbers[input]);
avlOps->Insert(avlTree, root);
// check if rotation is required.
AVL * tempNo=avlTree->GetParent();
while(tempNo!=NULL)
{
int balFac=0;
AVL* node1=NULL;
AVL* node2=NULL;
AVL* node3=NULL;
int rCase=0;
balFac=avlOps->GetBalanceFactor(tempNo);
if(balFac>1||balFac<-1)
{
node1=tempNo;
if(balFac>0)
{
node2=node1->GetLChild();
balFac=avlOps->GetBalanceFactor(node2);
if(balFac>0)
{
node3=node2->GetLChild();
rCase=1;
}
else
{
node3=node2->GetRChild();
rCase=3;
}
}
else
{
node2=node1->GetRChild();
balFac=avlOps->GetBalanceFactor(node2);
if(balFac>0)
{
node3=node2->GetLChild();
rCase=4;
}
else
{
node3=node2->GetRChild();
rCase=2;
}
}
root=avlOps->Rotation(node1,node2,node3,root,rCase);
}
tempNo=tempNo->GetParent();
}
cout<<endl;
cout << "Root :" << root->GetKey() << endl;
cout << "******" << endl;
avlTree++;
input++;
}
avlOps->InorderPrint(root);
return 0;
}