示例#1
0
/*
 *    Internal method to insert into a subtree
 *       x is the item to insert
 *          t is the node that roots the subtree
 *             Set the new root of the subtree
 *             */
void insert( int x, AvlNode * & t )
{
        if( t == NULL )
                t = new AvlNode( x, NULL, NULL );
        else if( x < t->data )
        {
                insert( x, t->left );
                if( height( t->left ) - height( t->right ) == 2 )
                        if( x < t->left->data )
                                rotateWithRightChild( t );
                        else
                                doubleWithLeftChild( t );
        }
        else if( t->data < x )
        {
                insert( x, t->right );
                if( height( t->right ) - height( t->left ) == 2 )
                        if( x < t->left->data )
                                rotateWithRightChild( t );
                        else
                                doubleWithLeftChild( t );
        }
        else
                ; // Duplicate; do nothing
        t->height = max( height( t->left ), height( t->right ) ) + 1;
        
}
示例#2
0
  void bubbleUp( BSTNode<Data>* newNode )
  {
    while ( (newNode->parent != NULL) && 
            (newNode->parent->info < newNode->info) )
    {
      BSTNode<Data>* p = newNode->parent;
      BSTNode<Data>* grandp = newNode->parent->parent;
      if (grandp == NULL)
      {
        if (p->left == newNode)
	{
          p = rotateWithLeftChild(p);
	  BST<Data>::root = p;
	  p->parent = NULL;
        }
        else
	{
          p = rotateWithRightChild(p);
	  BST<Data>::root = p;
	  p->parent = NULL;
        }
      }
      else if (grandp->left == p)
      {
	if (p->left == newNode)
	{
          p = rotateWithLeftChild(p);
	  grandp->left = p;
	  p->parent = grandp;
        }
        else
	{
          p = rotateWithRightChild(p);
	  grandp->left = p;
	  p->parent = grandp;
        }
      }
      else
      {
	if (p->left == newNode)
	{
          p = rotateWithLeftChild(p);
	  grandp->right = p;
	  p->parent = grandp;
        }
        else
	{
          p = rotateWithRightChild(p);
	  grandp->right = p;
	  p->parent = grandp;
        }
      }

    }
  }
示例#3
0
void AVLTree::insert_helper(const City& city, Node * & t) {
  if( t == NULL ) {
    t = new Node( city, NULL, NULL, 0 );
  } else if( city < t->city ) {
    insert_helper( city, t->left );
    if( height( t->left ) - height( t->right ) == 2 ) {
      if( city < t->left->city ) {
	rotateWithLeftChild( t );
      } else {
	doubleWithLeftChild( t );
      }
    }
  } else if( t->city < city ) {
    insert_helper( city, t->right );
    if( height( t->right ) - height( t->left ) == 2 ) {
      if( t->right->city < city ) {
	rotateWithRightChild( t );
      } else {
	doubleWithRightChild( t );
      }
    }
  }
  else {
    // duplicate item so do nothing
  }

  t->height = MAX( height(t->left), height(t->right) ) + 1;
}
示例#4
0
void AVL<T>::deleteHelper(T &value, Node* &rt){
	if(rt == NULL)
		return;
	else if (value < rt->element){
		deleteHelper(value, rt->left);
		if(height(rt->right) - height(rt->left) == 2){
			if(rt->right->right == NULL)
				doubleRotateWithRightChild(rt);
			else
				rotateWithRightChild(rt);
		}
		rt->deltaHeight = max(height(rt->left), height(rt->right)) +1;
	}
	else if (value > rt->element){
		deleteHelper(value, rt->right);
		if(height(rt->left) - height(rt->right) == 2){
			if(rt->left->right == NULL)
				doubleRotateWithLeftChild(rt);
			else
				rotateWithLeftChild(rt);
		}
		rt->deltaHeight = max(height(rt->left),height(rt->right)) + 1;
	}
	else if(rt->left != NULL && rt->right != NULL){
		rt->element = findMin(rt->right)->element;
		deleteHelper(rt->element, rt->right);
	}
	else{
		Node* old = rt;
		rt = (rt->left != NULL) ? rt->left : rt->right;
		delete old;
		numNodes--;
	}
}
示例#5
0
/**
* Internal method to insert into a subtree.
* x is the item to insert.
* t is the node that roots the tree.
*/
void AvlTree::insert( const string & x, AvlNode * & t ) const {
    if ( t == NULL ) {
        t = new AvlNode( x, NULL, NULL );
    } else if ( x < t->element ) {
        insert( x, t->left );
        if ( height( t->left ) - height( t->right ) == 2 ) {
            if ( x < t->left->element ) {
                rotateWithLeftChild( t );
            } else {
                doubleWithLeftChild( t );
            }
        }
    } else if ( t->element < x ) {
        insert( x, t->right );
        if ( height( t->right ) - height( t->left ) == 2 ) {
            if ( t->right->element < x ) {
                rotateWithRightChild( t );
            } else {
                doubleWithRightChild( t );
            }
        }
    } else
        ;  // Duplicate; do nothing
    t->height = max( height( t->left ), height( t->right ) ) + 1;
}
 /**
  * Internal method to insert into a subtree.
  * x is the item to insert.
  * t is the node that roots the subtree.
  * Set the new root of the subtree.
  */
 void insert( const Comparable & x, AvlNode * & t )
 {
     if( t == NULL )
         t = new AvlNode( x, NULL, NULL );
     else if( x < t->element )
     {
         insert( x, t->left );
         if( height( t->left ) - height( t->right ) == 2 )
             if( x < t->left->element )
                 rotateWithLeftChild( t );
             else
                 doubleWithLeftChild( t );
     }
     else if( t->element < x )
     {
         insert( x, t->right );
         if( height( t->right ) - height( t->left ) == 2 )
             if( t->right->element < x )
                 rotateWithRightChild( t );
             else
                 doubleWithRightChild( t );
     }
     else
         ;  // Duplicate; do nothing
     t->height = max( height( t->left ), height( t->right ) ) + 1;
 }
 /**
  * Internal routine that performs a single or double rotation.
  * Because the result is attached to the parent, there are four cases.
  * Called by handleReorient.
  * item is the item in handleReorient.
  * theParent is the parent of the root of the rotated subtree.
  * Return the root of the rotated subtree.
  */
 RedBlackNode * rotate( const Comparable & item, RedBlackNode *theParent )
 {
     if( item < theParent->element )
     {
         item < theParent->left->element ?
             rotateWithLeftChild( theParent->left )  :  // LL
             rotateWithRightChild( theParent->left ) ;  // LR
         return theParent->left;
     }
     else
     {
         item < theParent->right->element ?
             rotateWithLeftChild( theParent->right ) :  // RL
             rotateWithRightChild( theParent->right );  // RR
         return theParent->right;
     }
 }
 /**
  * Split primitive for AA-trees.
  * t is the node that roots the tree.
  */
 void split( AANode * & t )
 {
     if( t->right->right->level == t->level )
     {
         rotateWithRightChild( t );
         t->level++;
     }
 }
示例#9
0
 void AATree<Comparable>::split( AANode<Comparable> * & t ) const
 {
     if( t->right->right->level == t->level )
     {
         rotateWithRightChild( t );
         t->level++;
     }
 }
示例#10
0
		void SplayTree<Key, Data>::splay(Key const &key, SplayNode<Key, Data> * & t) const
		{
			if (!t) return;

			SplayNode<Key, Data>       *leftTreeMax, *rightTreeMin;
			static SplayNode<Key, Data> header;

			header.left = header.right = NULL;

			leftTreeMax = rightTreeMin = &header;

			for ( ; ;)
				if (Compare(key, t->id) < 0) {
					if (t->left == NULL)
						break;

					if (Compare(key, t->left->id) < 0)
						rotateWithLeftChild(t);

					if (t->left == NULL)
						break;

					/* Link Right */
					rightTreeMin->left = t;
					rightTreeMin = t;
					t = t->left;
				} else if (Compare(t->id, key) < 0) {
					if (t->right == NULL)
						break;

					if (Compare(t->right->id, key) < 0)
						rotateWithRightChild(t);

					if (t->right == NULL)
						break;

					/* Link Left */
					leftTreeMax->right = t;
					leftTreeMax = t;
					t = t->right;
				} else
					break;

			leftTreeMax->right = t->left;
			rightTreeMin->left = t->right;
			t->left = header.right;
			t->right = header.left;

			header.left = header.right = NULL;
		}
示例#11
0
        void SplayTree<Comparable>::splay( const Comparable & x,
                                           SBinaryNode<Comparable> * & t ) const
        {
            SBinaryNode<Comparable> *leftTreeMax, *rightTreeMin;
            static SBinaryNode<Comparable> header;

            header.left = header.right = nullNode;
            leftTreeMax = rightTreeMin = &header;

            nullNode->element = x;   // Guarantee a match

            for( ; ; )
                if( x < t->element )
                {
                    if( x < t->left->element )
                        rotateWithLeftChild( t );
                    if( t->left == nullNode )
                        break;
                    // Link Right
                    rightTreeMin->left = t;
                    rightTreeMin = t;
                    t = t->left;
                }
                else if( t->element < x )
                {
                    if( t->right->element < x )
                        rotateWithRightChild( t );
                    if( t->right == nullNode )
                        break;
                    // Link Left
                    leftTreeMax->right = t;
                    leftTreeMax = t;
                    t = t->right;
                }
                else
                    break;

	    // ADDED BY WOLF: debugging a double free on remove(1) on an empty tree.
	    // ADDED BY WOLF: no longer need to guarantee a match!
            nullNode->element = ITEM_NOT_FOUND;

            leftTreeMax->right = t->left;
            rightTreeMin->left = t->right;
            t->left = header.right;
            t->right = header.left;
        }
示例#12
0
void SplayTree<Comparable>::splay( const Comparable & x,
                                   BinaryNode<Comparable> * & t ) const
{
    BinaryNode<Comparable> *leftTreeMax, *rightTreeMin;
    static BinaryNode<Comparable> header;

    header.left = header.right = nullNode;
    leftTreeMax = rightTreeMin = &header;

    nullNode->element = x;   // Guarantee a match

    for( ; ; )
        if( x < t->element )
        {
            if( x < t->left->element )
                rotateWithLeftChild( t );
            if( t->left == nullNode )
                break;
              // Link Right
            rightTreeMin->left = t;
            rightTreeMin = t;
            t = t->left;
        }
        else if( t->element < x )
        {
            if( t->right->element < x )
                rotateWithRightChild( t );
            if( t->right == nullNode )
                break;
              // Link Left
            leftTreeMax->right = t;
            leftTreeMax = t;
            t = t->right;
        }
        else
            break;

    leftTreeMax->right = t->left;
    rightTreeMin->left = t->right;
    t->left = header.right;
    t->right = header.left;
}
示例#13
0
void AVLTree::_insert(AVLNode*& node, Node*& data)
{
    if (node == nullptr)
    {
        node = new AVLNode(data);
    }
    else
    {
        if (data->getWord() > node->data->getWord())
        {
            _insert(node->right, data);
            if (height(node->right) - height(node->left) == 2)
            {
                if (data->getWord() > node->right->data->getWord())
                {
                    rotateWithRightChild(node);
                }
                else
                {
                    doubleWithRightChild(node);
                }
            }
        }
        else
        {
            _insert(node->left, data);
            if (height(node->left) - height(node->right) == 2)
            {
                if (data->getWord() < node->left->data->getWord())
                {
                    rotateWithLeftChild(node);
                }
                else
                {
                    doubleWithLeftChild(node);
                }
            }
        }

    }
    node->height = (max(height(node->left), height(node->right)) + 1);
}
示例#14
0
void AVLTree::insert(Node* data)
{
    if(root == nullptr)
    {
      root = new AVLNode(data);
    }
    else
    {
        if (data->getWord() > root->data->getWord())
        {
            _insert(root->right, data);
            if (height(root->right) - height(root->left) == 2)
            {
                if (data->getWord() > root->right->data->getWord())
                {
                   rotateWithRightChild(root);
                }
                else
                {
                    doubleWithRightChild(root);
                }
            }
        }
        else
        {
            _insert(root->left, data);
            if (height(root->left) - height(root->right) == 2)
            {
                if (data->getWord() < root->left->data->getWord())
                {
                    rotateWithLeftChild(root);
                }
                else
                {
                    doubleWithLeftChild(root);
                }
                }
        }
    }
    root->height = max(height(root->left), height(root->right)) + 1;
}
示例#15
0
void AVL<T>::insertHelper(T val, Node* &rt){
 if(rt == NULL){
		rt = getNode(val);
		numNodes++;
	}else if(val < rt->element){
		insertHelper(val, rt->left);
		if(height(rt->left) - height(rt->right) == 2)
			if(val < rt->left->element)
				rotateWithLeftChild(rt);
			else 
				doubleRotateWithLeftChild(rt);
	}else if(val > rt->element){
		insertHelper(val, rt->right);
		if(height(rt->right) - height(rt->left) == 2)
			if(rt->right->element < val)
				rotateWithRightChild(rt);
			else
				doubleRotateWithRightChild(rt);
	}
	rt->deltaHeight = max(height(rt->left), height(rt->right)) + 1;
}
示例#16
0
文件: avltree.hpp 项目: glyredsun/gtl
	static void balance(Node* &t)
	{
		if (t == nullptr) {
			return;
		}

		if (height(t->left) - height(t->right) > ALLOWED_IMBALANCE) {
			if (height(t->left->left) >= height(t->left->right))
				rotateWithLeftChild(t);
			else
				doubleWithLeftChild(t);
		}
		else if (height(t->right) - height(t->left) > ALLOWED_IMBALANCE) {
			if (height(t->right->right) >= height(t->right->left))
				rotateWithRightChild(t);
			else
				doubleWithRightChild(t);
		}
		
		t->height = 1 + max(height(t->left), height(t->right));
	}
示例#17
0
/**
* Double rotate binary tree node: first right child.
* with its left child; then node k1 with new right child.
* For AVL trees, this is a double rotation for case 3.
* Update heights, then set new root.
*/
void AvlTree::doubleWithRightChild( AvlNode * & k1 ) const {
    rotateWithLeftChild( k1->right );
    rotateWithRightChild( k1 );
    DoubleRotations++;
}
示例#18
0
/**
* Double rotate binary tree node: first left child.
* with its right child; then node k3 with new left child.
* For AVL trees, this is a double rotation for case 2.
* Update heights, then set new root.
*/
void AvlTree::doubleWithLeftChild( AvlNode * & k3 ) const {
    rotateWithRightChild( k3->left );
    rotateWithLeftChild( k3 );
    DoubleRotations++;
}
示例#19
0
void AVL<T>::doubleRotateWithRightChild(Node* &rt){
	rotateWithLeftChild(rt->right);
	rotateWithRightChild(rt);
}
示例#20
0
void AVLTree::doubleWithRightChild(Node * & t) {
  rotateWithLeftChild( t->right );
  rotateWithRightChild( t );
}
示例#21
0
void AVLTree::remove_helper(Node * & t, const std::string& name) {
  if( t == NULL ) return; // wasn't able to find the node
  else if(name < t->city.name) {
    remove_helper( t->left, name );
  }
  else if(name > t->city.name) {
    remove_helper( t->right, name );
  }
  else { // else found the node
    // found the node to delete
    
    // Case 1: node has no children
    if((!t->left) && (!t->right)) {
      delete t;
      t = NULL;
    }
    // Case 2a: node has 1 child on left
    else if(!t->right) {
      Node * save_left = t->left;
      delete t;
      t = save_left;
    }

    //Case 2b: node has 1 child on right
    else if(!t->left) {
      Node * save_right = t->right;
      delete t;
      t = save_right;
    }

    // Case 3: node has 2 children
    else {
      // find the inorder successor, left most child of right subtree
      Node * succ = t->right;
      
      // case 1: succ has no left child
      if(!succ->left) {
	delete t;
	t = succ;
      }
      // case 2: succ does have left child
      else {
	while(succ->left) succ = succ->left;
	Node * save_right = succ->right;
	t->city = succ->city;
	delete succ;
	succ = save_right;
      }
    }
  }

  if(!t) return;

  t->height = MAX( height(t->left), height(t->right) ) + 1;
  const int balance = getBalance( t );
  const int balance_left = getBalance( t->left );
  const int balance_right = getBalance( t->right );
  
  // Case 1: Left Left Case
  if((balance > 1) && (balance_left >= 0)) {
    rotateWithLeftChild(t);
  }
  // Case 2: Left Right Case
  else if((balance > 1) && (balance_left < 0)) {
    rotateWithRightChild(t->left);
    rotateWithLeftChild(t);
  }
  // Case 3: Right Right
  else if((balance < -1) && (balance_right <= 0)) {
    rotateWithRightChild(t);
  }
  // Case 4: Right Left
  else if((balance < -1) && (balance_right > 0)) {
    rotateWithLeftChild(t->right);
    rotateWithRightChild(t);
  }
  
}
示例#22
0
void AVLTree::doubleWithRightChild(AVLNode*& k3)
{
    rotateWithLeftChild(k3->right);
    rotateWithRightChild(k3);
}
示例#23
0
 /**
  * Double rotate binary tree node: first left child
  * with its right child; then node k3 with new left child.
  * For AVL trees, this is a double rotation for case 2.
  * Update heights, then set new root.
  */
 void doubleWithLeftChild( AvlNode * & k3 )
 {
     rotateWithRightChild( k3->left );
     rotateWithLeftChild( k3 );
 }