示例#1
0
        /// <summary>
        /// Método para obter o filho mais a direita de um nó.
        /// </summary>
        /// <param name="tree">Nó onde se realizará a busca.</param>
        /// <returns>Chave do filho mais a direita.</returns>
        private Key depperRightChild(ref AVL tree)
        {
            // Chave para ser retornada.
            Key result = null;

            // Verifica se o nó atual tem filho a direita.
            if (tree.getRightChild().KEY != null)
            {
                // Busca no filho a direita.
                AVL rightChild = (AVL)tree.getRightChild();
                result = depperRightChild(ref rightChild);
                // Realiza balanceamento
                tree.setThisRef(balance(tree));
            }
            // Caso não tenha filho a direita
            else
            {
                // O resultado recebe a chave do nó atual
                result = tree.KEY;
                // Referência do nó atua recebe filho a esquerda.
                tree.KEY      = tree.getLeftChild().KEY;
                tree.HEIGHT   = tree.getLeftChild().HEIGHT;
                tree.BALANCED = ((AVL)tree.getLeftChild()).BALANCED;
                tree.setRightChild(tree.getLeftChild().getRightChild());
                tree.setLeftChild(tree.getLeftChild().getLeftChild());
            }
            // Retorna chave encontrada.
            return(result);
        }
示例#2
0
        /// <summary>
        /// Método para Alterar a própria referência.
        /// </summary>
        /// <param name="tree">Nó para atualizar a referência atual</param>
        private void setThisRef(AVL tree)
        {
            // Verifica se o nó atual já é igual ao nó passado.
            if (!this.Equals(tree))
            {
                // Cria um nó auxiliar
                AVL aux = new AVL();
                // Atualiza valores
                aux.KEY      = KEY;
                aux.HEIGHT   = HEIGHT;
                aux.BALANCED = BALANCED;
                aux.setLeftChild(getLeftChild());
                aux.setRightChild(getRightChild());
                // Modifica a instância this, verificando se o valor do nó passado.
                switch (tree.KEY.VALUE.CompareTo(KEY.VALUE))
                {
                // Caso seja maior, o auxiliar vira filho a direita.
                case  1: key = tree.KEY;
                    balanced = tree.BALANCED;
                    height   = tree.HEIGHT;
                    setLeftChild(aux);
                    setRightChild(tree.getRightChild());
                    break;

                // Caso seja menor, o auxiliar vira filho a esquerda.
                case -1: KEY = tree.KEY;
                    balanced = tree.BALANCED;
                    height   = tree.HEIGHT;
                    setLeftChild(tree.getLeftChild());
                    setRightChild(aux);
                    break;
                }
            }
        }
示例#3
0
        /// <summary>
        /// Metodo para rotacionar o elemento a esquerda
        /// </summary>
        /// <param name="tree">Elemento a ser rotacionado</param>
        /// <returns>Árvore balanceada</returns>
        public AVL rotateLeft(AVL tree)
        {
            // Variável auxiliar.
            AVL aux = tree;
            // Filho esquerdo.
            AVL leftChild = (AVL)tree.getLeftChild();
            // Filho direito do filho esquerdo.
            AVL subTree2 = (AVL)leftChild.getRightChild();

            // Rotação
            leftChild.setRightChild(tree);
            tree.setLeftChild(subTree2);
            // Atualiza altura dos nós
            updateHeight(tree);
            switch (updateHeight(leftChild))
            {
            // Caso a rotação simples não funicone, é realizada uma rotação dupla.
            case -2: leftChild.setRightChild(rotateLeft((AVL)leftChild.getRightChild()));
                leftChild = rotateRight(leftChild);
                break;
            }
            // Árvore balanceada.
            return(leftChild);
        }