#include "BinarySearchTree.h" #include #include using namespace std; template class AVLTree : public BinarySearchTree { protected: /** function to set the height correctly **/ void _setHeight(typename BinaryTree::Iterator position) { //assume child's heights are correct and in aux field //get left child's height int leftheight = -1; if (position.hasLeftChild()) leftheight = BinaryTree::getAux(position.left()); //get right child's height int rightheight = -1; if (position.hasRightChild()) rightheight = BinaryTree::getAux(position.right()); BinaryTree::setAux(max(leftheight,rightheight)+1, position); }//end of _setHeight bool _isBalanced(typename BinaryTree::Iterator position) { int leftheight = -1; if (position.hasLeftChild()) leftheight = BinaryTree::getAux(position.left()); //get right child's height int rightheight = -1; if (position.hasRightChild()) rightheight = BinaryTree::getAux(position.right()); if (abs(rightheight - leftheight) < 2) return true; else return false; } typename BinaryTree::Iterator _getHigherChild(typename BinaryTree::Iterator position) { //get left child's height int leftheight = -1; if (position.hasLeftChild()) leftheight = BinaryTree::getAux(position.left()); //get left child's height int rightheight = -1; if (position.hasRightChild()) rightheight = BinaryTree::getAux(position.right()); if (leftheight > rightheight) return position.left(); else return position.right(); } public: void insert(const ItemType& value) { //call BinarySearchTree insert to put new node in first typename BinaryTree::Iterator it = BinarySearchTree::insert(value); cout << "past binary search tree insert" << endl; _setHeight(it); cout << "set the height of the new node" << endl; it = it.up(); cout << "moving up" << endl; while(!it.isNULL()) { _setHeight(it); //check if balanced, and pivot if (!_isBalanced(it)) { typename BinaryTree::Iterator z = it; typename BinaryTree::Iterator y = _getHigherChild(z); typename BinaryTree::Iterator x = _getHigherChild(y); if ((y == z.left() && x == y.left()) || (y == z.right() && x == y.right())) { BinaryTree::pivot(y); _setHeight(z); _setHeight(y); it = y; } else { BinaryTree::pivot(x); BinaryTree::pivot(x); _setHeight(y); _setHeight(z); _setHeight(x); it = x; } } it = it.up(); } } //end insert void remove(const ItemType& value) { //homework? } //end remove }; //end AVLTree class