GARANTIR DESCONTO

Fórum Este método deve contar o número de filhos totais, diretos ou não, de determinado nodo recebido por parâmetro. #572061

02/12/2016

0

Este método deve contar o número de filhos totais, diretos ou não, de determinado nodo recebido por parâmetro.
public int contaFilhosNodo(GeneralTree T);


public class Sorting {

	public static void insertionSort (int a[]) { 
    	System.out.println("Vetor Inicial:");
    	print(a);
        for (int i = 1; i < a.length; i++) { 
            int j = i; 
            int B = a[i]; 
            while ((j > 0) && (a[j-1] > B)) { 
                a[j] = a[j-1]; 
                j--;                 
            }
            a[j] = B;
            print(a);
        } 
    }
	
	 public static void bubbleSort(int a[]) { 
	    	
	        for (int i = a.length; --i>=0; ) { 
	            boolean flipped = false;
	            for (int j = 0; j<i; j++) { 
	                if (a[j] > a[j+1]) { 
	                    int T = a[j]; 
	                    a[j] = a[j+1]; 
	                    a[j+1] = T; 
	                    flipped = true; 
	                } // if 
	            } // for
	            if (!flipped) return;
	            print(a);
	        } // for
	    } 
	 
	 public static void quickSort (int[] a, int lo, int hi){
	        int i=lo, j=hi, h;
	        int x=a[(lo+hi)/2];
	   
	        //  partition
	        do
	        {    
	            while (a[i]<x) i++; 
	            while (a[j]>x) j--;
	            if (i<=j)
	            {
	                h=a[i]; a[i]=a[j]; a[j]=h;
	                i++; j--;
	            }
	        } while (i<=j);
	        //  recursion
	        if (lo<j) quickSort(a, lo, j);
	        if (i<hi) quickSort(a, i, hi);
	    }

	 public static void quickSort(int a[]) {
	        quickSort(a, 0, a.length-1);
	    }
	 
	 public static void selectionSort  (int a[]) {
	        int min=0, ch;
	        for  (int i=0; i<a.length-1; i++) {
	             min  =  i;         // mínimo inicial 
	             for  (int j  =  i + 1; j<a.length; j++)
	                if  (a [ j ]  <  a [ min ]) min  =  j;  // novo mínimo
	             ch  =  a [ i ];
	             a [ i ]  =  a [ min ] ;    // insere
	             a [ min ]  =  ch;
	             print(a);
	        }
	    }
	 
	 public static void print (int a[]) { 
	        System.out.println();
	        for (int i = 0; i < a.length; i++) System.out.print(a[i]+", ");
	    }
	 
	public static void main(String[] args) {
		int a []= {46, 30, 2, 1, 19, 89, 20};
        
        //int b [] = {10,3,9,11,8,7,1,5};
        
        
        System.out.println ("\\n\\nVetor original:");
        print(a);
        //System.out.println ("\\n\\n Ap—s selecao direta:");
        //bubbleSort(a);
        selectionSort(a);
        //insertionSort (b);
        print(a);
	}

}


public class GeneralTree {
    private Object key;
    private int degree;
    private List list; 

    public Object getKey ()
    { return key; }
    
    public GeneralTree (Object key) {
        this.key = key;
        degree = 0;
        list = new List ();
    }

    public GeneralTree getSubtree (int i) {
        if (i < 0 || i >= degree)
            throw new IndexOutOfBoundsException ();
        Node ptr = list.getFirst ();
        for (int j = 0; j < i; ++j)
            ptr = ptr.getNext ();
        return (GeneralTree) ptr.getData ();
    }

    public void attachSubtree (GeneralTree t) {
        list.insertAtBack (t);
        ++degree;
    }

    public GeneralTree detachSubtree (GeneralTree t) throws UnderflowException {
        list.remove (t);
        --degree;
        return t;
    }

    public static void main (String args[]){
        GeneralTree raiz = new GeneralTree("D");
        GeneralTree t2 = new GeneralTree("E");
        GeneralTree t3 = new GeneralTree("G");
        raiz.attachSubtree(t2);
        raiz.attachSubtree(t3);
        t2.attachSubtree(new GeneralTree("F"));
        t2.getSubtree(0).attachSubtree(new GeneralTree("A"));
        t2.getSubtree(0).attachSubtree(new GeneralTree("B"));
        t2.getSubtree(0).attachSubtree(new GeneralTree("N"));
        t2.getSubtree(0).attachSubtree(new GeneralTree("Y"));
        t3.attachSubtree(new GeneralTree("H"));
        t3.attachSubtree(new GeneralTree("J"));
        t3.attachSubtree(new GeneralTree("M"));
   
        t3.getSubtree(0).attachSubtree(new GeneralTree("I"));
        t3.getSubtree(1).attachSubtree(new GeneralTree("K"));
        t3.getSubtree(1).attachSubtree(new GeneralTree("L"));
    }

    
    
      public void print(GeneralTree T){
          System.out.print(T.getKey().toString() + "(" + T.degree +")" + "\\n..");
          print(T);
          System.out.println();
        }
         public void printr(GeneralTree T){
             Node current = (Node) T.list.getFirst();
             GeneralTree gt;
             for(int i =0; i<T.degree; i++){
                 gt = (GeneralTree) current.getData();
                 System.out.print(gt.getKey().toString() + "("+ gt.degree + ")" + ",");
                 print(gt);
                 current = current.getNext();
                }
            }
Ricardo

Ricardo

Responder

Posts

07/12/2016

Alex Andrade

Bom dia Gepeto,

Muito interessante seu código. Vou salvar em meus programas de modelo para quando precisar.
Você teria alguma dúvida referente à esse código ou postou para ficar de exemplo mesmo?
Responder

Gostei + 0

07/12/2016

Ricardo

A dúvida seria na implementação do método
public int contaFilhosNodo(GeneralTree T);
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar