Home > Back-end >  For post
For post

Time:11-18

The following code is a AVL implementation, is a single-threaded code, but at run time appear such circumstance: directly on the ides of Devc++ compile operation cannot run over, I guess it is out of memory or access to the wild pointer, but can't find the problem; But a single step can correct operation, a great god can help me to point out problems, please? Thank you

 
# include
#include
using namespace std;

Template
Struct AVLTreeNode {
AVLTreeNode * _left;
AVLTreeNode * _right;
AVLTreeNode * _parent;

K _key;
V _value;
Int _bf;//balance factor

AVLTreeNode (const K& The key, const V& Value)
: _key (key)
, _value (value)
, _left (NULL)
, _right (NULL)
, _parent (NULL)
, _bf (0)
{}
};

Template
The class AVLTree {
Typedef AVLTreeNode The Node;
Public:
AVLTree ()
: _root (NULL)
{}
Bool Insert (const K& The key, const V& Value)
{
If (_root==NULL)
{
_root=new Node (key, value);
return true;
}
Node * cur=_root;
The parent Node *=NULL;
While (cur)
{
If (cur - & gt; _key & gt; Key)
{
The parent=cur;
Cur=cur - & gt; _left;
}
Else if (cur - & gt; _key & lt; Key)
{
The parent=cur;
Cur=cur - & gt; _right;
}
The else
{
return false;
}
}
Cur=new Node (key, value);
If (the parent - & gt; _key & gt; Key)
{
The parent - & gt; _left=cur;
Cur - & gt; _parent=parent;
}
The else
{
The parent - & gt; _right=cur;
Cur - & gt; _parent=parent;
}
//update balance factor
//not balance, then rotate
While (the parent)
{
If (the parent - & gt; _right==cur)
The parent - & gt; _bf + +;
The else
The parent - & gt; _bf -;
//parent node balance factor is 0, the exit (on both sides of the parent node height, path length is the same, does not affect)
If (the parent - & gt; _bf==0)
break;
//parent node balance factor to 1 or 1 (from 0 + 1 or from 0 to 1), on both sides of the parent node height is different, so need to continue to update balance factor
Else if (the parent - & gt; _bf==1 | | the parent - & gt; _bf==1)
{
Cur=parent;
The parent=cur - & gt; _parent;
}
//parent node balance factor of 2 or 2, rotate
The else//(the parent - & gt; _bf==2 | | the parent - & gt; _bf==2) rotate
{
If (the parent - & gt; _bf==2)
{
If (cur - & gt; _bf==1)//right single
{
_RotateR (parent);
}
The else//(cur - & gt; _bf==1) single
{
_RotateLR (parent);
}
}
The else//(the parent - & gt; _bf==2)
{
If (cur - & gt; _bf==1)//left single
{
_RotateL (parent);
}
The else//(cur - & gt; _bf==1) right left single
{
_RotateRL (parent);
}
}
break;
}
}
}
The Node * Find (const K& Key)
{
If (_root==NULL)
return NULL;
Node * cur=_root;
While (cur)
{
If (cur - & gt; _key & gt; Key)
Cur=cur - & gt; _left;
Else if (cur - & gt; _key & lt; Key)
Cur=cur - & gt; _right;
The else
Return a cur.
}
return NULL;
}
Bool Remove (const K& Key)
{
If (_root==NULL)
return false;

The parent Node *=NULL;
Node * cur=_root;
While (cur)
{
If (cur - & gt; _key & gt; Key)
{
The parent=cur;
Cur=cur - & gt; _left;
}
Else if (cur - & gt; _key & lt; Key)
{
The parent=cur;
Cur=cur - & gt; _right;
}
The else
{
Node * del;
If (cur - & gt; _right==NULL)
{
Del=cur;
If (the parent==NULL)
{
_root=cur - & gt; _left;
//_root - & gt; _bf=0;
}
The else
{
If (the parent - & gt; _left==cur)
{
The parent - & gt; _left=cur - & gt; _left;
The parent - & gt; _bf + +;
}
The else
{
The parent - & gt; _right=cur - & gt; _left;
The parent - & gt; _bf -;
}
}
Delete the del.
}
Else if (cur - & gt; _left==NULL)
{
Del=cur;
If (the parent==NULL)
{
_root=cur - & gt; _right;
_root - & gt; _bf=0;
Cout & lt; <"Cur:" & lt; }
The else
{
If (the parent - & gt; _left==cur)
{
The parent - & gt; _left=cur - & gt; _right;
The parent - & gt; _bf + +;
}
The else
{
The parent - & gt; _right=cur - & gt; _right;
The parent - & gt; _bf -;
}
}
Delete the del.
}
The else
{
The parent=cur;
The Node * left=cur - & gt; _right;
While (left - & gt; _left)
{
The parent=left;
Left=left - & gt; _left;
}
Del=left;
Cur - & gt; _key=left - & gt; _key;
Cur - & gt; _value=https://bbs.csdn.net/topics/left-> _value;
If (the parent - & gt; _left==left)
{
The parent - & gt; _left=left - & gt; _right;
The parent - & gt; _bf + +;
}
The else
{
The parent - & gt; _right=left - & gt; _right;
The parent - & gt; _bf -;
}

Delete the del.
}
break;
}
}
If (cur==NULL)
{
return false;
}
While (the parent)
{
If (the parent - & gt; _bf==0)
{
break;
}
Else if (the parent - & gt; _bf==1 | | the parent - & gt; _bf==1)
{
break;
}
The else//parent - & gt; _bf=2 | | the parent - & gt; _bf=2
-{
If (the parent - & gt; _bf==2)
{
If (cur - & gt; _bf==1)
_RotateR (parent);
The else//cur - & gt; _bf=1
_RotateLR (parent);
}
The else
{
If (cur - & gt; _bf==1)
_RotateL (parent);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related