/** * 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; }
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; }
/** * 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; }
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); }
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; }
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)); }