Example #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);
        }
Example #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;
                }
            }
        }
Example #3
0
        /// <summary>
        /// Metodo para rotacionar o elemento a direita
        /// </summary>
        /// <param name="tree">elemento a ser rotacionado</param>
        /// <returns>Árvore balanceada</returns>
        public AVL rotateRight(AVL tree)
        {
            // Variável auxiliar.
            AVL aux = tree;
            // Filho direito.
            AVL rightChild = (AVL)tree.getRightChild();
            // Filho esquerdo do filho a direita.
            AVL subTree2 = (AVL)rightChild.getLeftChild();

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