void AVLTree::PostOrder(TreeNode *node, QAnimationGroup *group) { if (node == NULL) return; group->addAnimation(node->getTurnRedAnim()); TreePath *path = new TreePath(node, node->Lson, view); group->addAnimation(path->getToSonAnim()); PreOrder(node->Lson, group); group->addAnimation(path->getToParentAnim()); path = new TreePath(node, node->Rson, view); group->addAnimation(path->getToSonAnim()); PreOrder(node->Rson, group); group->addAnimation(path->getToParentAnim()); group->addAnimation(node->getPopAnim()); group->addAnimation(node->getTurnBlackAnim()); }
void AVLTree::Find(TreeNode *node, int x, QAnimationGroup *group) { if (node == NULL) return; TreePath *path; if (node->parent && group) { path = new TreePath(node->parent, node, view); group->addAnimation(path->getToSonAnim()); } if (group) group->addAnimation(node->getTurnRedAnim()); if (node->data == x) { if (group) group->addAnimation(node->getPopAnim()); else node->getPopAnim()->start(QAbstractAnimation::DeleteWhenStopped); } else if (node->data > x) Find(node->Lson, x, group); else Find(node->Rson, x, group); if (group) group->addAnimation(node->getTurnBlackAnim()); if (node->parent && group) { group->addAnimation(path->getToParentAnim()); } }
void AVLTree::Delete(TreeNode * &node, int x, QAnimationGroup *group) { if (node == NULL) return; if (group&& node->parent) group->addAnimation(node->parent->getTurnRedAnim()); TreePath *path; if (group && node->parent) { path = new TreePath(node->parent, node, view); group->addAnimation(path->getToSonAnim()); } TreeNode* parent = node->parent; if (x < node->data) { //如果x小于节点的值,就继续在节点的左子树中删除x Delete(node->Lson, x, group); if (group && node->parent) group->addAnimation(path->getToParentAnim()); if (2 == height(node->Rson) - height(node->Lson)) { if (node->Rson->Lson != NULL && (height(node->Rson->Lson)>height(node->Rson->Rson))) RL(node, group); else RR(node, group); } } else if (x > node->data) { Delete(node->Rson, x, group);//如果x大于节点的值,就继续在节点的右子树中删除x if (group && node->parent) group->addAnimation(path->getToParentAnim()); if (2 == height(node->Lson) - height(node->Rson)) { if (node->Lson->Rson != NULL && (height(node->Lson->Rson)>height(node->Lson->Lson))) LR(node, group); else LL(node, group); } } else//如果相等,此节点就是要删除的节点 { if (group) group->addAnimation(node->getPopAnim()); else node->getPopAnim()->start(QAbstractAnimation::DeleteWhenStopped); if (node->Lson && node->Rson)//此节点有两个儿子 { TreeNode* temp = node->Rson;//temp指向节点的右儿子 while (temp->Lson != NULL) temp = temp->Lson;//找到右子树中值最小的节点 //把右子树中最小节点的值赋值给本节点 if (group) group->addAnimation(node->getValueAnim(temp->data)); else node->data = temp->data; //node->freq = temp->freq; Delete(node->Rson, temp->data, group);//删除右子树中最小值的节点 if (group && node->parent) group->addAnimation(path->getToParentAnim()); if (2 == height(node->Lson) - height(node->Rson)) { if (node->Lson->Rson != NULL && (height(node->Lson->Rson)>height(node->Lson->Lson))) LR(node, group); else LL(node, group); } } else//此节点有1个或0个儿子 { TreeNode* temp = node; TreeNode* parent = node->parent; if (group && node->parent) group->addAnimation(path->getToParentAnim()); if (node->Lson == NULL)//有右儿子或者没有儿子 node = node->Rson; else if (node->Rson == NULL)//有左儿子 node = node->Lson; if (group) { QParallelAnimationGroup *anim = new QParallelAnimationGroup; anim->addAnimation(temp->getFadeOutAnim()); if (node) anim->addAnimation(node->setParent(parent)); group->addAnimation(anim); } else { temp->getFadeOutAnim()->start(QAbstractAnimation::DeleteWhenStopped); if (node) node->setParent(parent)->start(QAbstractAnimation::DeleteWhenStopped); } if (group) group->addAnimation(getPosAnim()); } } if (group && parent) group->addAnimation(parent->getTurnBlackAnim()); if (node == NULL) return; node->h = max(height(node->Lson), height(node->Rson)) + 1; return; }
void AVLTree::Insert(TreeNode * &node, TreeNode *parent, int x, QAnimationGroup *group) { if (group && parent) group->addAnimation(parent->getTurnRedAnim()); if (node == NULL) { QParallelAnimationGroup *anim = new QParallelAnimationGroup; node = new TreeNode(); view->addItem(node); node->data = x; node->setPos(parent ? parent->pos() : QPointF(0, 0)); anim->addAnimation(node->getFadeInAnim()); if (group) { anim->addAnimation(node->setParent(parent)); anim->addAnimation(getPosAnim()); group->addAnimation(anim); if (parent) group->addAnimation(parent->getTurnBlackAnim()); } else { anim->start(QAbstractAnimation::DeleteWhenStopped); } return; } TreePath *path; if (group && parent) { path = new TreePath(parent, node, view); group->addAnimation(path->getToSonAnim()); } if (node->data > x) { Insert(node->Lson, node, x, group); if (group && parent) group->addAnimation(path->getToParentAnim()); if (2 == height(node->Lson) - height(node->Rson)) { if (x < node->Lson->data) LL(node, group); else LR(node, group); } } else if (node->data < x) { Insert(node->Rson, node, x, group); if (group && parent) group->addAnimation(path->getToParentAnim()); if (2 == height(node->Rson) - height(node->Lson)) { if (x > node->Rson->data) RR(node, group); else RL(node, group); } } else if (group && parent) group->addAnimation(path->getToParentAnim()); if (group && parent) group->addAnimation(parent->getTurnBlackAnim()); node->h = max(height(node->Lson), height(node->Rson)) + 1; }