farwest1
Nouveau membre
Bonsoir à tous,
J'ai réalisé un programme d'implementation d'arbre.
Je souhaiterai pouvoir y ajouer, inserer, rechercher une valeur, et terminer par un équilbrage.
Je pense qu'il faut que j'utilise les fonctions insert, suppr, estdans.
Mais je ne sais pas ou les placer, j'avoue avoir un peu de mal entre méthode, classe, heritage, public, static, void.
Quelqu'un peut-il me mettre sur le chemin.
Je joins mon travail.
D'avance merci.
[cpp] class Arbre{
private Noeud racine;
private class Noeud{
int rac;
Noeud gauche;
Noeud droit;
Noeud(int r)
{rac=r;}
Noeud(int r, Noeud gauche, Noeud droit){
rac=r;
gauche=gauche;
droit=droit;
}
}
Arbre(){}
Arbre(Noeud e){racine=e;}
Arbre(int r){racine= new Noeud(r);}
Arbre(int r, Arbre fg, Arbre fd){
racine=new Noeud(r,fg.racine,fd.racine);
}
boolean estVide(){
return (racine==null);
}
Arbre filsGauche(){
return new Arbre(racine.gauche);
}
Arbre filsDroit(){
return new Arbre(racine.droit);
}
int hauteur(){
int h,hd,hg;
if (estVide()) return -1;
hd=filsDroit().hauteur();
hg=filsGauche().hauteur();
return (hd<hg)?hg+1:hd+1;
}
int minimum(){
if (filsGauche().estVide()) return racine.rac;
else return filsGauche().minimum();
}
void ajouter(int m){
Arbre a;
if (estVide()) {
racine = new Noeud (m);
} else {
if (m<racine.rac) {
a=filsGauche();
a.ajouter(m);
racine.gauche=a.racine;
} else {
if (m>racine.rac) {
a=filsDroit();
a.ajouter(m);
racine.droit=a.racine;
}
}
}
}
void afficheAmeliore(String s, boolean b, boolean sommet, int profondeur){
if (!estVide()) {
filsDroit().afficheAmeliore(s+((!b && !sommet)?"|\t":"\t"),true,false,profondeur-1);
if (!(filsGauche().estVide() && filsDroit().estVide())) System.out.println(s+racine.rac+" ----\t|");
else System.out.println(s+racine.rac);
filsGauche().afficheAmeliore(s+((b && !sommet)?"|\t":"\t"),false,false,profondeur-1);
} else {
for (int i=1; i<java.lang.Math.pow(2,profondeur+1);i++) System.out.println(s);
}
}
void afficheArbre(){
afficheAmeliore("",true,true,hauteur());
// affiche("",true,true);
}
}
class EssaiArbre{
/
static Arbre arbreComplet(int i,int j){
Arbre a;
int milieu;
if (i==j) a=new Arbre(i);
else {
if (j<i) a=new Arbre();
else {
milieu=(i+j)/2;
a=new Arbre(milieu,arbreComplet(i,milieu-1),arbreComplet(milieu+1,j));
}
}
return a;
}
static Arbre getArbre(){
Arbre a = new Arbre();
int[] tab={8,12,14,4,6,2,17,5,7,21,17};
for (int i=0;i<tab.length;i++) a.ajouter(tab);
return a;
}
public static void main(String arg[]){
Arbre a;
a = getArbre();
System.out.println("Voici l'arbre :");
a.afficheArbre();
System.out.println("\n");
}
}[/cpp]
J'ai réalisé un programme d'implementation d'arbre.
Je souhaiterai pouvoir y ajouer, inserer, rechercher une valeur, et terminer par un équilbrage.
Je pense qu'il faut que j'utilise les fonctions insert, suppr, estdans.
Mais je ne sais pas ou les placer, j'avoue avoir un peu de mal entre méthode, classe, heritage, public, static, void.
Quelqu'un peut-il me mettre sur le chemin.
Je joins mon travail.
D'avance merci.
[cpp] class Arbre{
private Noeud racine;
private class Noeud{
int rac;
Noeud gauche;
Noeud droit;
Noeud(int r)
{rac=r;}
Noeud(int r, Noeud gauche, Noeud droit){
rac=r;
gauche=gauche;
droit=droit;
}
}
Arbre(){}
Arbre(Noeud e){racine=e;}
Arbre(int r){racine= new Noeud(r);}
Arbre(int r, Arbre fg, Arbre fd){
racine=new Noeud(r,fg.racine,fd.racine);
}
boolean estVide(){
return (racine==null);
}
Arbre filsGauche(){
return new Arbre(racine.gauche);
}
Arbre filsDroit(){
return new Arbre(racine.droit);
}
int hauteur(){
int h,hd,hg;
if (estVide()) return -1;
hd=filsDroit().hauteur();
hg=filsGauche().hauteur();
return (hd<hg)?hg+1:hd+1;
}
int minimum(){
if (filsGauche().estVide()) return racine.rac;
else return filsGauche().minimum();
}
void ajouter(int m){
Arbre a;
if (estVide()) {
racine = new Noeud (m);
} else {
if (m<racine.rac) {
a=filsGauche();
a.ajouter(m);
racine.gauche=a.racine;
} else {
if (m>racine.rac) {
a=filsDroit();
a.ajouter(m);
racine.droit=a.racine;
}
}
}
}
void afficheAmeliore(String s, boolean b, boolean sommet, int profondeur){
if (!estVide()) {
filsDroit().afficheAmeliore(s+((!b && !sommet)?"|\t":"\t"),true,false,profondeur-1);
if (!(filsGauche().estVide() && filsDroit().estVide())) System.out.println(s+racine.rac+" ----\t|");
else System.out.println(s+racine.rac);
filsGauche().afficheAmeliore(s+((b && !sommet)?"|\t":"\t"),false,false,profondeur-1);
} else {
for (int i=1; i<java.lang.Math.pow(2,profondeur+1);i++) System.out.println(s);
}
}
void afficheArbre(){
afficheAmeliore("",true,true,hauteur());
// affiche("",true,true);
}
}
class EssaiArbre{
/
static Arbre arbreComplet(int i,int j){
Arbre a;
int milieu;
if (i==j) a=new Arbre(i);
else {
if (j<i) a=new Arbre();
else {
milieu=(i+j)/2;
a=new Arbre(milieu,arbreComplet(i,milieu-1),arbreComplet(milieu+1,j));
}
}
return a;
}
static Arbre getArbre(){
Arbre a = new Arbre();
int[] tab={8,12,14,4,6,2,17,5,7,21,17};
for (int i=0;i<tab.length;i++) a.ajouter(tab);
return a;
}
public static void main(String arg[]){
Arbre a;
a = getArbre();
System.out.println("Voici l'arbre :");
a.afficheArbre();
System.out.println("\n");
}
}[/cpp]