package Arbre;

public class AVL extends ABR {

	private Float valeur;
	private AVL filsGauche, filsDroit;
	
	public AVL() {}
	
	public boolean isEmpty() {
		return this.valeur == null;
	}
	
	public void ajouter (Float valeur) {
		if(this.isEmpty()) {
			this.valeur = valeur;
			this.filsGauche = new AVL();
			this.filsDroit = new AVL();
		}
		else {
			if (valeur <= this.valeur) {
				this.filsGauche.ajouter(valeur);
			}
			else {
				this.filsDroit.ajouter(valeur);
			}
		}
	}
	
	public String structureToString() {
		if(isEmpty()) {
			return "[]";
		}
		else {
			return "[" + String.format("%1$.2f", this.valeur) + this.filsGauche.structureToString() + this.filsDroit.structureToString() + "]";
		}
	}
	
	public int getHauteur() {
		return this.isEmpty()?0:1+Math.max(this.filsGauche.getHauteur(), this.filsDroit.getHauteur());
	}
	
	public boolean isEquilibre() {
		return isEmpty()?true:Math.abs(this.filsGauche.getHauteur() - this.filsDroit.getHauteur()) <= 1;
	}
	
	public void equilibrage() {
		
	}
	
	public void rotationDroite() {
		float valeurTemp = this.valeur;
		this.valeur = this.filsGauche.valeur;
		this.filsGauche.valeur = valeurTemp;
		
		AVL arbre = this.filsGauche;
		this.filsGauche = this.filsGauche.filsGauche;
		arbre.filsGauche = arbre.filsDroit;
		arbre.filsDroit = this.filsDroit;
		this.filsDroit = arbre;
	}
	
	public void rotationGauche() {
		float valeurTemp = this.valeur;
		this.valeur = this.filsDroit.valeur;
		this.filsDroit.valeur = valeurTemp;
		
		AVL arbre = this.filsGauche;
		this.filsDroit = this.filsDroit.filsDroit;
		arbre.filsDroit = arbre.filsGauche;
		arbre.filsGauche = this.filsGauche;
		this.filsGauche = arbre;
	}
}
