qq_24350087
o0Destiny0o
2015-12-12 11:02

二叉搜索AVL树的插入算法

  • 算法
  • c++
  • 存储

定义: bool Insert(AVLNode *& ptr, E& e1);
实现:bool AVLTree::Insert(AVLNode *& ptr, E& e1)
{
AVLNode *pr = NULL, *p = ptr, *q; int d;
Stack *>st;
while (p != NULL)
{
if (e1 == p->data)return false;
pr = p;st.Push(pr);
if (e1 < p->data)p = p->left;
p = p->right;
}
p = new AVLNode(e1);
if (p == NULL) { cerr << "存储空间不足!" << endl;exit(1); }
if (pr == NULL) { ptr = p;return true; }
if (e1 < pr->data)pr->left = p;
else pr->right = p;
while (st.IsEmpty() == false)
{
st.Pop(pr);
if (p == pr->left)pr->bf--;
else pr->bf++;
if (pr->bf == 0)break;
if (pr->bf == 1 || pr->bf = -1)p = pr;
else
{
d = (pr->bf < 0) ? -1 : 1;
if (pr->bf == d)
{
if (d == -1)RotateR(pr);
else RotateL(pr);
}
else
{
if (d == -1)RotateLR(pr);
else RotateRL(pr);
}
break;
}
}
if (st.IsEmpty() == true)ptr = pr;
else
{
st.getTop(q);
if (q->data > pr->data)q->left = pr;
else q->right = pr;
}
return true;
}

编译时显示无法解析的外部命令是怎么回事

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐

换一换