structnode { int ele; node *left; node *right; int height; node(int i, node *l, node *r, int h): ele(i), left(l), right(r), height(h) {} };
classavl { private: node *root;
node* leftleft(node *T){ node *temp = T -> left; T -> left = temp -> right; temp -> right = T; T -> height = ((height(T -> left) > height(T -> right)) ? height(T -> left) : height(T -> right)) + 1; temp -> height = ((T -> height > height(temp -> left)) ? T -> height : height(temp -> left)) + 1; return temp; } node* rightright(node *T){ node *temp = T -> right; T -> right = temp -> left; temp -> left = T; T -> height = ((height(T -> left) > height(T -> right)) ? height(T -> left) : height(T -> right)) + 1; temp -> height = ((T -> height > height(temp -> right)) ? T -> height : height(temp -> right)) + 1; return temp; } node* leftright(node *T){ T -> left = rightright(T -> left); return leftleft(T); } node* rightleft(node *T){ T -> right = leftleft(T -> right); return rightright(T); } intheight(node* t){ if (t == 0) return-1; else return t -> height; }
node* realInsert(int i, node *T){ if (T == 0) { T = new node(i, 0, 0, 0); } elseif (i < T -> ele) { T -> left = realInsert(i, T -> left); if (height(T -> left) - height(T -> right) == 2) if (i < T -> left -> ele) T = leftleft(T); else T = leftright(T); } elseif (i > T -> ele) { T -> right = realInsert(i, T -> right); if (height(T -> right) - height(T -> left) == 2) if (i > T -> right-> ele) T = rightright(T); else T = rightleft(T); } T -> height = ((height(T -> left) > height(T -> right)) ? height(T -> left) : height(T -> right)) + 1; return T; }
boolrealRemove(node *&T, int i){ if (!T) returnfalse; if (i < T -> ele) { if (realRemove(T -> left, i) == false) { returnfalse; } elseif (height(T -> right) - height(T -> left) == 2) { if (height(T -> right -> left) <= height(T -> right -> right)) T = rightright(T); else T = rightleft(T); } else { T -> height = ((height(T -> left) > height(T -> right)) ? height(T -> left) : height(T -> right)) + 1; } } elseif (i > T -> ele) { if (realRemove(T -> right, i) == false) { returnfalse; } elseif (height(T -> left) - height(T -> right) == 2) { if (height(T -> left -> right) <= height(T -> left -> left)) T = leftleft(T); else T = leftright(T); } else { T -> height = ((height(T -> left) > height(T -> right)) ? height(T -> left) : height(T -> right)) + 1; } } else { if (T -> right && T -> left) { node *temp = T -> right; if (temp -> left) { while (temp -> left && temp -> left -> left) { temp = temp -> left; } int key = temp -> left -> ele; node *oldT = T; realRemove(T, key); oldT -> ele = key; } else { int key = temp -> ele; node *oldT = T; realRemove(T, key); oldT -> ele = key; } } elseif (T -> left || T -> right) { node *temp = (T -> left) ? T -> left : T -> right; delete T; T = temp; } else { delete T; T = 0; } } returntrue; } public: avl(): root(0) {} ~avl() { clear(); root = 0; } void preorder(void(*fun)(node*), node* T) { if (T == 0) return; fun(T); preorder(fun, T -> left); preorder(fun, T -> right); } void postorder(void(*fun)(node*), node* T) { if (T == 0) return; postorder(fun, T -> left); postorder(fun, T -> right); fun(T); } void inorder(void(*fun)(node*), node* T) { if (T == 0) return; inorder(fun, T -> left); fun(T); inorder(fun, T -> right); }
评论