#ifndef AVLTREE_H #define AVLTREE_H #include "iostream" using namespace std; template<typename T> class AVLTree { friend ostream& operator<<(ostream& os, AVLTree<T> tree) { for (Iterator itr = tree.begin(); itr!=tree.end(); itr++) { os<<*itr<<" "; } return os; } protected: struct tree_node { T item; tree_node* parent; tree_node* left; tree_node* right; char balanceFactor; bool isHeader; }; typedef tree_node* Link; Link header; unsigned node_count; public: class Iterator { friend class AVLTree<T>; protected: Link link; Iterator(Link new_link):link(new_link){} public: Iterator(){} Iterator& operator++() { if (link->right != NULL) { link = link->right; return *this; } else { Link temp = link->parent; while (temp->right == link) { link = temp; temp = temp->parent; } if (link->right != temp) { link = temp; } } return *this; } Iterator operator++(int) { Iterator temp = *this; ++(*this); return temp; } Iterator& operator--() { if (link->isHeader) { link = header->right; return *this; } else if (link->left != NULL) { link = link->left; return *this; } else { Link temp = link->parent; while (temp->left == link) { link = temp; temp = temp->parent; } link = temp; } return *this; } Iterator operator--(int) { Link temp = *this; --(*this); return temp; } T& operator*() { return link->item; } bool operator==(const Iterator& otherIterator) { return link == otherIterator.link; } bool operator!=(const Iterator& otherIterator) { return link != otherIterator.link; } }; friend class Iterator; AVLTree() { header = new tree_node(); header->parent = NULL; header->left = header; header->right = header; header->isHeader = true; node_count = 0; } ~AVLTree() { destroy(header->parent); delete header; } void destroy(Link link) { if (link->left != NULL) { destroy(link->left); } if (link->right != NULL) { destroy(link->right); } delete link; } size_t size()const { return node_count; } Iterator begin() { return header->left; } Iterator end() { return header; } Iterator find(const T& item) { Link child = header->parent; while (child != NULL) { if (item < child->item) { child = child->left; } else if (item > child->item) { child = child->right; } else { return child; } } return header; } Iterator insert(const T& item) { Link parent = header; Link temp = header->parent; Link ancestor = NULL; if (header->parent == NULL) //空树 { insertItem(item,header,header->parent); header->left = header->parent; header->right = header->parent; return header->parent; } else //非空 { while (temp != NULL) { parent = temp; if (temp->balanceFactor != '=') { ancestor = temp; } if (item < temp->item) { temp = temp->left; } else //item >= temp->item { temp = temp->right; } } if (item < parent->item) // { insertItem(item,parent,parent->left); fixAfterInsertion(ancestor,parent->left); if (header->left == parent) { header->left = parent->left; } } else // { insertItem(item,parent,parent->right); fixAfterInsertion(ancestor,parent->right); if (header->right == parent) { header->right = parent->right; } } return temp; } } void fixAfterInsertion(Link ancestor, Link inserted) { Link root = header->parent; T item = inserted->item; if (ancestor == NULL) { if (item < root->item) { root->balanceFactor = 'L'; } else { root->balanceFactor = 'R'; } adjustPath(root,inserted); } else if ((ancestor->balanceFactor == 'L')&&(item >= ancestor->item) || ((ancestor->balanceFactor == 'R')&&(item < ancestor->item))) { ancestor->balanceFactor = '='; adjustPath(ancestor,inserted); } else if ((ancestor->balanceFactor == 'L')&&(item < ancestor->left->item)) rotate_right { ancestor->balanceFactor = '='; rotate_right(ancestor); adjustPath(ancestor->parent,inserted); } else if ((ancestor->balanceFactor == 'R')&&(item >= ancestor->right->item)) rotate_left { ancestor->balanceFactor = '='; rotate_left(ancestor); adjustPath(ancestor->parent,inserted); } else if ((ancestor->balanceFactor == 'L')&&(item >= ancestor->left->item)) { rotate_left(ancestor->left); rotate_right(ancestor); adjustLeftRight(ancestor,inserted); } else if ((ancestor->balanceFactor == 'R')&&(item < ancestor->right->item)) { rotate_right(ancestor->right); rotate_left(ancestor); adjustRightLeft(ancestor,inserted); } } void adjustLeftRight(Link ancestor,Link inserted) { T item = inserted->item; if (ancestor->parent == inserted) { ancestor->balanceFactor = '='; } else if (item >= ancestor->parent->item) { ancestor->balanceFactor = '='; ancestor->parent->left->balanceFactor = 'L'; adjustPath(ancestor,inserted); } else if (item < ancestor->parent->item) { ancestor->balanceFactor = 'R'; adjustPath(ancestor->parent->left,inserted); } } void adjustRightLeft(Link ancestor,Link inserted) { T item = inserted->item; if (ancestor->parent == inserted) { ancestor->balanceFactor = '='; } else if (item < ancestor->parent->item) { ancestor->balanceFactor = '='; ancestor->parent->right->balanceFactor = 'R'; adjustPath(ancestor,inserted); } else if (item >= ancestor->parent->item) { ancestor->balanceFactor = 'L'; adjustPath(ancestor->parent->right,inserted); } } void rotate_left(Link x) //3对6个 { Link y = x->right; x->right = y->left; if (y->left != NULL) { y->left->parent = x; } y->parent = x->parent; if (x->parent->parent == x) { x->parent->parent = y; } else if (x->parent->right == x) { x->parent->right = y; } else if (x->parent->left == x) { x->parent->left = y; } x->parent = y; y->left = x; } void rotate_right(Link x) { Link y = x->left; x->left = y->right; if (y->right != NULL) { y->right->parent = x; } y->parent = x->parent; if(x->parent->parent == x) { x->parent->parent = y; } else if (x->parent->left == x) { x->parent->left = y; } else if (x->parent->right == x) { x->parent->right = y; } x->parent = y; y->right = x; } void adjustPath(Link ancestor,Link inserted) { Link temp = ancestor; T item = inserted->item; while (temp != inserted) { if (item < temp->item) { temp->balanceFactor = 'L'; temp = temp->left; } else { temp->balanceFactor = 'R'; temp = temp->right; } } } void insertItem(const T& item, Link parent, Link& child) { child = new tree_node(); child->parent = parent; child->item = item; child->left = NULL; child->right = NULL; child->isHeader = false; child->balanceFactor = '='; node_count++; } }; #endif