BUT2/DEV/DEV3.2/TP08_Arbres_suite/Q1Noeud.java

81 lines
1.8 KiB
Java
Raw Normal View History

2023-11-29 16:08:44 +01:00
import java.util.*;
public class Q1Noeud{
private int clef;
private Q1Noeud petit;
private Q1Noeud grand;
private int occurence;
public Q1Noeud(int clef){
this.clef = clef;
this.petit = null;
this.grand = null;
this.occurence = 1;
}
public int getKey(){
return this.clef;
}
public void add(int fils){
if (fils < this.clef){
if (this.petit == null){
this.petit = new Q1Noeud(fils);
}
else{
this.petit.add(fils);
}
}
if (fils > this.clef){
if (this.grand == null){
this.grand = new Q1Noeud(fils);
}
else{
this.grand.add(fils);
}
}
if (fils == this.clef){
this.occurence ++;
}
}
public void add(Q1Noeud newNoeud){
if (newNoeud.getKey() < this.clef){
if (this.petit == null){
this.petit = newNoeud;
}
else{
this.petit.add(newNoeud);
}
}
if (newNoeud.getKey() > this.clef){
if (this.grand == null){
this.grand = newNoeud;
}
else{
this.grand.add(newNoeud);
}
}
if (newNoeud.getKey() == this.clef){
this.occurence ++;
}
}
public Q1Noeud getPetit(){
return this.petit;
}
public Q1Noeud getGrand(){
return this.grand;
}
@Override
public String toString(){
String resultat = "";
int i;
for (i=0; i<this.occurence; i++){
resultat += (this.clef + " ");
}
return resultat;
}
}