Exemple #1
0
 public void PosOrdem(Noh parent)
 {
     if (parent != null)
     {
         PosOrdem(parent.Esquerda);
         PosOrdem(parent.Direita);
         this.ListaNum.Add(parent.Valor);
     }
 }
Exemple #2
0
 public Arvore(int Tamanho)
 {
     this.Raiz     = null;
     this.ListaNum = new List <int>();
     for (int i = 0; i < Tamanho; i++)
     {
         Noh noh = new Noh();
         noh.Esquerda = null;
         noh.Direita  = null;
         noh.Valor    = 0;
     }
 }
Exemple #3
0
        private int ValorMinimo(Noh Noh)
        {
            int minv = Noh.Valor;

            while (Noh.Esquerda != null)
            {
                minv = Noh.Esquerda.Valor;
                Noh  = Noh.Esquerda;
            }

            return(minv);
        }
Exemple #4
0
        public bool Add(int value)
        {
            Noh before = null, after = this.Raiz;

            while (after != null)
            {
                before = after;
                if (value < after.Valor) //Is new Noh in left tree?
                {
                    after = after.Esquerda;
                }
                else if (value > after.Valor) //Is new Noh in right tree?
                {
                    after = after.Direita;
                }
                else
                {
                    //Exist same value
                    return(false);
                }
            }

            Noh newNoh = new Noh();

            newNoh.Valor = value;

            if (this.Raiz == null)//Tree is empty
            {
                this.Raiz = newNoh;
            }
            else
            {
                if (value < before.Valor)
                {
                    before.Esquerda = newNoh;
                }
                else
                {
                    before.Direita = newNoh;
                }
            }

            this.Quantidade++;
            this.ListaNum.Add(newNoh.Valor);
            return(true);
        }
Exemple #5
0
        private Noh Pesquisar(int value, Noh parent)
        {
            if (parent != null)
            {
                if (value == parent.Valor)
                {
                    return(parent);
                }
                if (value < parent.Valor)
                {
                    return(Pesquisar(value, parent.Esquerda));
                }
                else
                {
                    return(Pesquisar(value, parent.Direita));
                }
            }

            return(null);
        }
Exemple #6
0
        private Noh Remover(Noh parent, int key)
        {
            if (parent == null)
            {
                return(parent);
            }

            if (key < parent.Valor)
            {
                parent.Esquerda = Remover(parent.Esquerda, key);
            }
            else if (key > parent.Valor)
            {
                parent.Direita = Remover(parent.Direita, key);
            }

            // if value is same as parent's value, then this is the Noh to be deleted
            else
            {
                // Noh with only one child or no child
                if (parent.Esquerda == null)
                {
                    return(parent.Direita);
                }
                else if (parent.Direita == null)
                {
                    return(parent.Esquerda);
                }

                // Noh with two children: Get the inorder successor (smallest in the right subtree)
                parent.Valor = ValorMinimo(parent.Direita);

                // Delete the inorder successor
                parent.Direita = Remover(parent.Direita, parent.Valor);
            }

            return(parent);
        }
Exemple #7
0
 private int GetProfundidadeArv(Noh parent)
 {
     return(parent == null ? 0 : Math.Max(GetProfundidadeArv(parent.Esquerda), GetProfundidadeArv(parent.Direita)) + 1);
 }