Arvore generica

5 respostas
X

preciso de um metodo que recebe uma arvore generica como parametro como posso identificar se ela é uma arvore generica ou uma arvore binaria

5 Respostas

tnaires

Veja a quantidade de filhos de um nó qualquer ora. Se tiver apenas dois filhos no máximo, é uma árvore binária.

X

Isso eu sei mas como eu conto quantos filhos tenho num nó??

davidbuzatto

Isso eu sei mas como eu conto quantos filhos tenho num nó??

Depende de como vc armazena os filhos…

L

Isso eu sei mas como eu conto quantos filhos tenho num nó??

Dentro da classe Nó, crie uma lista de filhos, e quando você for inserir os filhos, você simplesmente dá um add nessa lista. Dai pra saber se é binária ou não, basta dar um filhos.size <= 2 para todos os nós. Se para todos der true, é binária.

Essa é uma forma de fazer, mas existem muitas outras.

X

Eu tenho estas duas classes so preciso implementar um metodo adicional que diferencie se é generica ou nao

// Baseada na Implementacao do Livro de Bruno Preiss 
public class GeneralTree {
    protected Object key;
    protected int degree;
    protected 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) {
		list.remove (t);
		--degree;
		return t;
    }
}
public class NaryTree {
    protected Object key;
    protected int degree;
    protected NaryTree[] subtree;

    public NaryTree (int degree) {
		key = null;
		this.degree = degree;
		subtree = null;
    }

    public NaryTree (int degree, Object key) {
		this.key = key;
		this.degree = degree;
		subtree = new NaryTree[degree];
		for (int i = 0; i < degree; ++i)
		    subtree [i] = new NaryTree (degree);
    }
    
    public boolean isEmpty () { return key == null; }

    public Object getKey (){
		if (isEmpty ())
		    throw new RuntimeException ();
		return key;
    }

    public void attachKey (Object object) {
		if (!isEmpty ())
		    throw new RuntimeException ();
		key = object;
		subtree = new NaryTree[degree];
		for (int i = 0; i < degree; ++i)
		    subtree [i] = new NaryTree (degree);
    }

    public NaryTree getSubtree (int i) {
		if (isEmpty ())
		    throw new RuntimeException ();
		return subtree [i];
    }

    public void attachSubtree (int i, NaryTree t) {
		if (isEmpty () || !subtree [i].isEmpty ())
		    throw new RuntimeException ();
		subtree [i] = t;
    }

    public NaryTree detachSubtree (int i) {
		if (isEmpty ())
		    throw new RuntimeException ();
		NaryTree result = subtree [i];
		subtree [i] = new NaryTree (degree);
		return result;
	    }
}
Criado 24 de novembro de 2009
Ultima resposta 25 de nov. de 2009
Respostas 5
Participantes 4