コード例 #1
0
ファイル: tree.cpp プロジェクト: lcosta/um_uc_prog_tp2
TreeNode * Tree::insert(GenericData * elem, TreeNode *tree) {
	if(tree == NULL) {
		tree = new TreeNode(elem, NULL, NULL);
	}
	else {
		if(elem->getId() < tree->getElem()->getId()) {
			tree->setLeft(insert(elem, tree->getLeft()));
			if(height(tree->getLeft()) - height(tree->getRight()) > 1) {
				if(elem->getId() < tree->getLeft()->getElem()->getId()) {
          tree = singleRotateLeft(tree);
				}
				else {
					tree = doubleRotateLeft(tree);
				}
			}
		}
		else if(elem->getId() > tree->getElem()->getId()) {
			tree->setRight(insert(elem, tree->getRight()));
			if(height(tree->getRight()) - height(tree->getLeft()) > 1) {
				if(elem->getId() > tree->getRight()->getElem()->getId()) {
					tree = singleRotateRight(tree);
				}
				else {
					tree = doubleRotateRight(tree);
				}
			}
		}
	}
	return tree;
}
コード例 #2
0
node* doubleRotateRightLeft(node* subtree){
	printf("Single right rotate...\n");
	subtree->right = singleRotateRight(subtree->right);
	printf("Single left rotate...\n");
	subtree = singleRotateLeft(subtree);
	return subtree;
}
コード例 #3
0
ファイル: map.cpp プロジェクト: M-Sayed/Generic-BST
template<class T1, class T2> void DS::map<T1, T2>::_insertAVL(node* current, node* prv, T1 key, T2 value) {
	if(current == null){
		current = new node();
		current->value = value, current->key = key, current->parent = prv, current->leftHeight = current->rightHeight = 0;
		if(prv) {
			if(prv->key < key) prv->right = current, prv->rightHeight = 1;
			else prv->left = current, prv->leftHeight = 1;
		} else root = current;

		return;
	}

	_insertAVL(current->key < key ? current->right : current->left, current, key, value);
	current->rightHeight = current->right ? _max(current->right) + 1 : 0;
	current->leftHeight = current->left ? _max(current->left) + 1 : 0;
	int diff = abs(current->rightHeight - current->leftHeight);
	node* tmp;
	if(diff > 1) { // AVL tree policy is violated - :o - and needs balancing. :D
		if(current->rightHeight > current->leftHeight) { // left-rotation
			tmp = current->right;
			if(tmp->rightHeight > tmp->leftHeight) {  // single-rotation
				singleRotateLeft(current);
			}else { // double-rotation
				doubleRotateLeft(current);
			}
		}else { // right rotation
			tmp = current->left;
			if(tmp->leftHeight > tmp->rightHeight) { // single-rotation
				singleRotateRight(current);
			}else { // double-rotation
				doubleRotateRight(current);
			}
		}
	}
}
コード例 #4
0
ファイル: 4.26.c プロジェクト: leicj/books
// 查找节点
void find(Tree *tree, int val)
{
    Tree *root = tree;
    Tree *parentnode = NULL;
    Tree *grandnode = NULL;
    tree = tree->left;

    while (tree) {
        if (tree->val == val) {
            break;
        } else if (tree->val > val) {
            tree = tree->left;
        } else {
            tree = tree->right;
        }
    }

    if (tree && tree->val == val) {
        parentnode = tree->parent;
        while (parentnode->val != INT_MIN) {
            grandnode = parentnode->parent;
            if (grandnode->val == INT_MIN) {
                if (parentnode->left == tree) {
                    singleRotateLeft(parentnode);
                } else {
                    singleRotateRight(parentnode);
                }
                return;
            } else {
                if (grandnode->left == parentnode && parentnode->left == tree) {
                    singleRotateLeft(grandnode);
                    singleRotateLeft(parentnode);
                } else if (grandnode->left == parentnode && parentnode->right == tree) {
                    doubleRotateLeft(grandnode);
                } else if (grandnode->right == parentnode && parentnode->right == tree) {
                    singleRotateRight(grandnode);
                    singleRotateRight(parentnode);
                } else if (grandnode->right == parentnode && parentnode->left == tree) {
                    doubleRotateRight(grandnode);
                }
                parentnode = tree->parent;
            }
        }
    }
}
コード例 #5
0
ファイル: 4.26.c プロジェクト: leicj/books
// 左-右旋转
Tree *doubleRotateLeft(Tree *tree)
{
    tree->left = singleRotateRight(tree->left);

    return singleRotateLeft(tree);
}
コード例 #6
0
ファイル: tree.cpp プロジェクト: lcosta/um_uc_prog_tp2
TreeNode * Tree::doubleRotateLeft(TreeNode* node){
	node->setLeft(singleRotateRight(node->getLeft()));
	return singleRotateLeft(node);
}