#include #include /* AVL node */ template class AVLnode { public: T key; int balance; AVLnode *left, *right, *parent; AVLnode(T k, AVLnode *p) : key(k), balance(0), parent(p), left(NULL), right(NULL) {} ~AVLnode() { delete left; delete right; } }; /* AVL tree */ template class AVLtree { public: AVLtree(void); ~AVLtree(void); bool insert(T key); void deleteKey(const T key); void printBalance(); private: AVLnode *root; AVLnode* rotateLeft ( AVLnode *a ); AVLnode* rotateRight ( AVLnode *a ); AVLnode* rotateLeftThenRight ( AVLnode *n ); AVLnode* rotateRightThenLeft ( AVLnode *n ); void rebalance ( AVLnode *n ); int height ( AVLnode *n ); void setBalance ( AVLnode *n ); void printBalance ( AVLnode *n ); }; /* AVL class definition */ template void AVLtree::rebalance(AVLnode *n) { setBalance(n); if (n->balance == -2) { if (height(n->left->left) >= height(n->left->right)) n = rotateRight(n); else n = rotateLeftThenRight(n); } else if (n->balance == 2) { if (height(n->right->right) >= height(n->right->left)) n = rotateLeft(n); else n = rotateRightThenLeft(n); } if (n->parent != NULL) { rebalance(n->parent); } else { root = n; } } template AVLnode* AVLtree::rotateLeft(AVLnode *a) { AVLnode *b = a->right; b->parent = a->parent; a->right = b->left; if (a->right != NULL) a->right->parent = a; b->left = a; a->parent = b; if (b->parent != NULL) { if (b->parent->right == a) { b->parent->right = b; } else { b->parent->left = b; } } setBalance(a); setBalance(b); return b; } template AVLnode* AVLtree::rotateRight(AVLnode *a) { AVLnode *b = a->left; b->parent = a->parent; a->left = b->right; if (a->left != NULL) a->left->parent = a; b->right = a; a->parent = b; if (b->parent != NULL) { if (b->parent->right == a) { b->parent->right = b; } else { b->parent->left = b; } } setBalance(a); setBalance(b); return b; } template AVLnode* AVLtree::rotateLeftThenRight(AVLnode *n) { n->left = rotateLeft(n->left); return rotateRight(n); } template AVLnode* AVLtree::rotateRightThenLeft(AVLnode *n) { n->right = rotateRight(n->right); return rotateLeft(n); } template int AVLtree::height(AVLnode *n) { if (n == NULL) return -1; return 1 + std::max(height(n->left), height(n->right)); } template void AVLtree::setBalance(AVLnode *n) { n->balance = height(n->right) - height(n->left); } template void AVLtree::printBalance(AVLnode *n) { if (n != NULL) { printBalance(n->left); std::cout << n->balance << " "; printBalance(n->right); } } template AVLtree::AVLtree(void) : root(NULL) {} template AVLtree::~AVLtree(void) { delete root; } template bool AVLtree::insert(T key) { if (root == NULL) { root = new AVLnode(key, NULL); } else { AVLnode *n = root, *parent; while (true) { if (n->key == key) return false; parent = n; bool goLeft = n->key > key; n = goLeft ? n->left : n->right; if (n == NULL) { if (goLeft) { parent->left = new AVLnode(key, parent); } else { parent->right = new AVLnode(key, parent); } rebalance(parent); break; } } } return true; } template void AVLtree::deleteKey(const T delKey) { if (root == NULL) return; AVLnode *n = root, *parent = root, *delNode = NULL, *child = root; while (child != NULL) { parent = n; n = child; child = delKey >= n->key ? n->right : n->left; if (delKey == n->key) delNode = n; } if (delNode != NULL) { delNode->key = n->key; child = n->left != NULL ? n->left : n->right; if (root->key == delKey) { root = child; } else { if (parent->left == n) { parent->left = child; } else { parent->right = child; } rebalance(parent); } } } template void AVLtree::printBalance() { printBalance(root); std::cout << std::endl; } int main(void) { AVLtree t; std::cout << "Inserting integer values 1 to 10" << std::endl; for (int i = 1; i <= 10; ++i) t.insert(i); std::cout << "Printing balance: "; t.printBalance(); }