inserção em uma árvore binaria desordenada

0 respostas
kaberdock

Estou criando uma ide utilizando drag and drop onde o usuário arrastaria blocos de código criano assim um código fonte (como se fosse um quebra-cabeça). para armazenar estes blocos e depois gerar o código fonte estou tentando utilizar uma árvore binaria desordenada. Para realizar a busca estou utilizando uma função pré-ordem iniciando pela raiz e percorrendo toda a árvore até achar o nó onde teremos um filho adicionado. Entretanto a busca sempre retorna o nó raiz, o código segue abaixo:

Esta é a busca pelo nó, o parâmetro node na primeira chamada será o nó raiz
public BlockNode getBlockNode(BlockNode node, Block block) {
		BlockNode ptr = node;
		
		if (ptr.getBlock().igual(block)) {
			System.out.println(ptr.getBlock().getIdentifier());
			return ptr;
		}
		if (ptr.getRightChild() != null) {
			System.out.println("entrou direita");
			ptr = ptr.getRightChild();
			this.getBlockNode(ptr, block);
		}
		if (ptr.getBottomChild() != null) {
			System.out.println("entrou abaixo");
			ptr = ptr.getBottomChild();
			this.getBlockNode(ptr, block);
		}
		return null;
	}

e ai vão as duas inserções

public void addRightChild(Block parent, Block child) {
		BlockNode node = new BlockNode(child);
		BlockNode p = null;
		p = this.getBlockNode(root, parent);
		if (p != null) {
			p.addRightChild(node);
			System.out.println(p.getBlock().getTitle() + " adicionou filho direito " + node.getBlock().getTitle());
		}

	}

	public void addBottomChild(Block parent, Block child) {
		BlockNode node = new BlockNode(child);
		BlockNode p = null;
		p = this.getBlockNode(root, parent);
		if (p != null) {
			p.addBottomChild(node);
			System.out.println(p.getBlock().getTitle() + " adicionou filho abaixo: " + node.getBlock().getTitle());
		}
	}
vou adicionar aqui a classe block para que possam ver a função de comparação
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Block extends JPanel {

	private String title;
	private Integer group;
	private List rightSupportedGroup;
	private List bottomSupportedGroup;
	private static int id = 0;
	private int identifier;

	public static int MAIN_GROUP = 0;
	public static int LOOP_GROUP = 1;
	public static int CONDITION_GROUP = 2;
	
	public Block() {
		this.identifier = id;
		updateId();
		//System.out.println(this.getIdentifier());
	}

	public Block(String title, int group, String image) {
		this.title = title;
		this.setOpaque(false);
		this.setPreferredSize(new Dimension(120, 120));
		JLabel bg = new JLabel(new ImageIcon(image));
		bg.setLayout(null);
		JLabel rot = new JLabel(this.title);
		rot.setBounds(50, 50, 40, 25);
		bg.add(rot);
		this.add(bg);
		this.group = group;
		this.rightSupportedGroup = new ArrayList<Integer>();
		this.bottomSupportedGroup = new ArrayList<Integer>();
		// this.setPreferredSize(new Dimension(120,120));
	}

	public void addRightSupportedGroup(Integer group) {
		this.rightSupportedGroup.add(group);
	}

	public void addBottomSupportedGroup(Integer group) {
		this.bottomSupportedGroup.add(group);
	}

	/*
	 * Esta função verifica se o bloco pode ter um bloco adicionado do lado
	 * direito
	 */
	public boolean isRightSupportedBlock(Block block) {

		for (int i = 0; i < this.rightSupportedGroup.size(); ++i)
			if (block.getGroup().equals(this.rightSupportedGroup.get(i))) {

				return true;
			}
		System.out.println("entrou na comparação");
		return false;
	}

	/*
	 * Esta função verifica se o bloco pode ter um bloco adicionado abaixo
	 */
	public boolean isBottomSupportedBlock(Block block) {

		for (int i = 0; i < this.bottomSupportedGroup.size(); ++i)
			if (block.getGroup().equals(this.bottomSupportedGroup.get(i))) {

				return true;
			}
		System.out.println("entrou na comparação");
		return false;
	}

	public Integer getGroup() {
		return this.group;
	}

	public String getTitle() {
		return this.title;
	}

	public int getIdentifier() {
		return this.identifier;
	}

	public boolean igual(Block block) {
		if (this.identifier == block.getIdentifier())
			return true;
		return false;
	}

	public String toString(){
		return this.title;
	}
	public static void resetId() {
		id = 0;
	}
	public static void updateId(){
		id++;
	}
}

como dito acima, a minha busca sempre está retornando o nó raiz.
Obrigado pela atenção.

Criado 26 de setembro de 2011
Respostas 0
Participantes 1