remoção com erro

2 respostas
Jauns

Senhores(as) boa noite… Primeiramente ja fiz diversas pesquisas (Guj/Google) e por infelicidade minha, naum consigo achar onde ta o bendito erro nesta função de remoção de uma arvore binaria, so falta a remoção, restante esta tudo certo…,

se caso alguem pode me ajudar… gracias

public static void delete(int valor) 
	{
		if (A==null){
			System.out.println("Vazio");
		}else{
			int x=valor;
			no aux=A;
			no ant=A;
			while((aux!=null)&& (x!=aux.info))
			{
				if(x<aux.info)
				{
					ant=aux;
					aux=aux.esq;
				}
				else if(x>aux.info)
				{
					ant=aux;
					aux=aux.dir;
				}
			}
			if (aux!=null)
			{
				if((aux.esq==null)&&(aux.dir==null))
				{
					if(x<ant.info)
						ant.esq=null;
					else
						ant.dir=null;
				}
				else if((aux.esq!=null)&&(aux.dir==null))
				{
					if(x<ant.info)//
						ant.esq=aux.esq;
					else
						ant.dir=aux.esq;
				}
				else if((aux.esq==null)&&(aux.dir!=null))
				{
					if(x<ant.info)
						ant.esq=aux.dir;
					else
						ant.dir=aux.dir;
				}
				else if((aux.esq!=null)&&(aux.dir!=null))
				{
					no troca=aux.esq;
					while(troca.dir!=null)
					{
						ant=troca;
						troca=troca.dir;
					}
					aux.info=troca.info;
					if(troca.esq!=null)
						ant.dir=troca.esq;
					else
						ant.dir=null;
				}
			}
		}				
	}

2 Respostas

Joao.Gabriel

O que é “A”?

Jauns

Nó principal

static no A = null;
package Arv_Binaria_teste;

import javax.swing.JFrame;

public class Pr_Arvore extends JFrame {

	private static Pr_LinePanel linePanel = null;

	public static JFrame j = null;

	public static int x;
	public static int y;
	public static int xx;
	public static int yy;
	public static int in;

	public static int[] vx = { 240, 120, 60, 30, 15 };
	public static int[] vy = { 80, 80, 80, 80, 80 };

	static no A = null;


	static int cont = 0;

	public static class no {

		int info;

		no esq;
		no dir;

		int px;
		int py;
		int pax;
		int pay;

		int altura;

		public no(int x, no e, no d, int xp, int yp, int pax, int pay, int alt) {
			this.info = x;
			this.esq = e;
			this.dir = d;
			this.px = xp;
			this.py = yp;
			this.pax = pax;
			this.pay = pay;
			this.altura = alt;
		}
	}

	public static void Pre_Ordem(no A) {
		if (A != null) {
			System.out.print(A.info + " - ");
			Pre_Ordem(A.esq);
			Pre_Ordem(A.dir);

		}
	}

	public static void Ordem_central(no A, int cont) {
		if (A != null) {
			Ordem_central(A.esq, cont);
			cont++;
			System.out.print(A.info + " - ");
			Ordem_central(A.dir, cont);
		}
	}

	public static void Pos_Ordem(no A) {
		if (A != null) {
			Pos_Ordem(A.esq);
			Pos_Ordem(A.dir);
			System.out.print(A.info + " - ");
		}
	}

	public void localiza() {
		if (A == null)
			System.out.println("Vazio");
		else {
			no aux = A;
			while ((aux != null) && (x != aux.info)) {
				if (x < aux.info)
					aux = aux.esq;
				else
					aux = aux.dir;
			}
			if (aux == null)
				System.out.println("Dados naum Localizado");
			else
				System.out.println("Dado encontrado, altura " + aux.altura
						+ " contando a partir do 0");

		}
	}

	public void folha(no A) {
		if (A != null) {
			Pre_Ordem(A.esq);
			Pre_Ordem(A.dir);
			if ((A.dir == null) && (A.esq == null))
				System.out.println(A.info + " F ");
		}
	}

	public static int maior(no A) {
		if (A != null)
			if (A.dir != null)
				maior(A.dir);

		return A.info;
	}

	public static int menor(no A) {
		if (A != null)
			if (A.esq != null)
				menor(A.esq);

		return A.info;

	}
	

	public static void Insere(int valor) {

		int x = valor;

		no temp = new no(x, null, null, 0, 0, 0, 0, 0);
		if (A == null) {
			A = temp;
			A.altura = 0;
			A.px = 480;
			A.py = 100;
			A.pax = 480;
			A.pay = 100;
			setLinePos(temp);
		} else {

			no ant = A;
			no aux = A;
			while ((aux != null) && (x != aux.info) && (aux.altura < 5)) {
				if (x < aux.info) {
					ant = aux;
					aux = aux.esq;
				} else if (x > aux.info) {
					ant = aux;
					aux = aux.dir;
				}
			}

			if (aux == null) {
				if (x < ant.info) {
					temp.px = ant.px - vx[ant.altura];
					temp.py = ant.py + vy[ant.altura];

					temp.pax = ant.px;
					temp.pay = ant.py;

					temp.altura = ant.altura + 1;

					ant.esq = temp;
					setLinePos(A);
				} else {
					temp.px = ant.px + vx[ant.altura];
					temp.py = ant.py + vy[ant.altura];

					temp.pax = ant.px;
					temp.pay = ant.py;

					temp.altura = ant.altura + 1;

					ant.dir = temp;
					setLinePos(A);
				}
			} else if (x == aux.info)
				System.out.println("Já existe");
			else
				System.out.println("Ultrapassa o limite da Árvore \n"
						+ "Árvore definida para altura 6 camadas");
		}
	}


	public static void delete(int valor) {
		if (A == null) {
			System.out.println("Vazio");
		} else {
			int x = valor;
			no aux = A;
			no ant = A;
			while ((aux != null) && (x != aux.info)) {
				if (x < aux.info) {
					ant = aux;
					aux = aux.esq;
				} else if (x > aux.info) {
					ant = aux;
					aux = aux.dir;
				}
			}
			if (aux != null) {
				if ((aux.esq == null) && (aux.dir == null)) {
					if (x < ant.info)
						ant.esq = null;
					else
						ant.dir = null;
				} else if ((aux.esq != null) && (aux.dir == null)) {
					if (x < ant.info)//
						ant.esq = aux.esq;
					else
						ant.dir = aux.esq;
				} else if ((aux.esq == null) && (aux.dir != null)) {
					if (x < ant.info)
						ant.esq = aux.dir;
					else
						ant.dir = aux.dir;
				} else if ((aux.esq != null) && (aux.dir != null)) {
					no troca = aux.esq;
					while (troca.dir != null) {
						ant = troca;
						troca = troca.dir;
					}
					aux.info = troca.info;
					if (troca.esq != null)
						ant.dir = troca.esq;
					else
						ant.dir = null;
				}
			}
		}
	}
}
Criado 14 de setembro de 2009
Ultima resposta 14 de set. de 2009
Respostas 2
Participantes 2