Professional Documents
Culture Documents
#include <iostream>
class Node {
public:
Node* lchild;
int data;
Node* rchild;
int height;
};
class AVL{
public:
Node* root;
// Insert
Node* rInsert(Node* p, int key);
// Traversal
void Inorder(Node* p);
void Inorder(){ Inorder(root); }
Node* getRoot(){ return root; }
// Delete
Node* Delete(Node* p, int key);
};
return hl > hr ? hl + 1 : hr + 1;
}
return hl - hr;
}
pl->rchild = p;
p->lchild = plr;
// Update height
p->height = NodeHeight(p);
pl->height = NodeHeight(pl);
// Update root
if (root == p){
root = pl;
}
return pl;
}
pr->lchild = p;
p->rchild = prl;
// Update height
p->height = NodeHeight(p);
pr->height = NodeHeight(pr);
// Update root
if (root == p){
root = pr;
}
return pr;
}
pl->rchild = plr->lchild;
p->lchild = plr->rchild;
plr->lchild = pl;
plr->rchild = p;
// Update height
pl->height = NodeHeight(pl);
p->height = NodeHeight(p);
plr->height = NodeHeight(plr);
// Update root
if (p == root){
root = plr;
}
return plr;
}
Node* AVL::RLRotation(Node *p) {
Node* pr = p->rchild;
Node* prl = pr->lchild;
pr->lchild = prl->rchild;
p->rchild = prl->lchild;
prl->rchild = pr;
prl->lchild = p;
// Update height
pr->height = NodeHeight(pr);
p->height = NodeHeight(p);
prl->height = NodeHeight(prl);
// Update root
if (root == p){
root = prl;
}
return prl;
}
// Update height
p->height = NodeHeight(p);
return p;
}
// Update height
p->height = NodeHeight(p);
return p;
}
int main() {
AVL tree;
tree.Inorder();
cout << endl;
tree.Delete(tree.root, 28);
tree.Inorder();
cout << endl;
return 0;
}