1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
| public class Arbre {
int val;
Arbre Fg;
Arbre Fd;
int tab[];
int cmpt=0;
int k=(this.taille());
static int i=0,h=0,y=0,x=0;
static int t[]=new int[100];
int t1[]=new int[100];
private Arbre Parent;
public Arbre getParent() {
return Parent;
}
public void setParent(Arbre parent) {
Parent = parent;
}
Arbre(int val){
this.val=val;
this.Fg=null;
this.Fd=null;
}
Arbre(int val,Arbre Fg,Arbre Fd){
this.val=val;
this.Fg=Fg;
this.Fd=Fd;
}
public int getValeur() {
return(getValeur());
}
public void setValeur(int val) {
this.val = val;
}
public Arbre getFg() {
return Fg;
}
public void setFG(Arbre Fg) {
Fg = Fg;
}
public Arbre getFd() {
return Fd;
}
public void setFD(Arbre Fd) {
Fd = Fd; }
public void inserer(int x){
if (this.val!=x){
if (this.val>x){
if(this.Fg!=null){
Fg.inserer(x);
}
else
{
Arbre A= new Arbre(x);
Fg=A;
}
}
if (this.val<x){
if(this.Fd!=null){
this.Fd.inserer(x);
}
else
{
Arbre A= new Arbre(x);
Fd=A;
return;
}
}}
else{
System.out.println("la valeur "+this.val+" existe,insertion impossible");
}
}
public int hauteur(Arbre A){
if(A!=null){
if(A.getFg()!=null && A.getFd()!=null){
y=hauteur(A.getFd())+1;
x= hauteur(A.getFd())+1;
return(x<y)? y : x;
}else{
if(A.getFg()!=null && A.getFd()==null){
y=hauteur(A.getFg())+1;
x=0;
return(x<y)? y : x; }
else{
if(A.getFd()!=null && A.getFg()==null){
y=hauteur(A.getFd())+1;
x=0;
return(x<y)? y : x; }
else{
return 0;
}
}
}
}else{
return -1;
}
}
*/
public int taille(){
int y=0;int x=0;
if (this.Fg!=null) {
y= (Fg.taille())+1;
}
if (this.Fd!=null){
x=(Fd.taille())+1;
}
return(x+y);
}
public void parcours(Arbre A){
if(this.Fd!=null ){
this.Fd.parcours(A);
}
t[i]=this.val;
if(i!=A.taille())
System.out.print(t[i]+" ");
i++;
if (i==A.taille())
System.out.println(t[i]+" ");
if (this.Fg!=null){
this.Fg.parcours(A);
}
if(i==A.taille()+1){
System.out.println("le tableau trié par ordre croissant est le suivant:");
while(i>1){
System.out.print(t[i-1]+" ");
i--;
if(i==1){
System.out.println(t[i-1]+" ");
}
}}}
public void equilibrer(Arbre A){
if(A.Fg.hauteur(A.Fg)>=A.Fd.hauteur(A.Fd)+1){
if( A.getFg().getFg().hauteur(A.Fg.Fg)<A.getFg().getFd().hauteur(A.Fg.Fd)){
A.rotationD1(A);
}
else{
rotationD2(A);
}
{
if (A.Fd.Fd.hauteur(A.Fd.Fd)>A.Fd.Fg.hauteur(A.Fg.Fg)){
rotationG1(A);
}
else
{
rotationG2(A);
}
}}
}
private void rotationG2(Arbre A) {
this.val=A.Fd.Fg.getValeur();
Fg=new Arbre(this.val,A.Fg,A.Fd.Fg.Fg);
Fd=new Arbre(A.Fd.getValeur(),A.Fd.Fg.Fd,A.Fd.Fd);
}
private void rotationG1(Arbre A) {
this.val=A.Fd.getValeur();
Fg=new Arbre(this.getValeur(),A.Fg,A.Fd.Fg);
Fd=A.Fd.Fd;
}
private void rotationD2(Arbre A) {
this.val=A.Fg.Fd.getValeur();
Fg=new Arbre(A.Fg.getValeur(),A.Fg.Fg,A.Fg.Fd);
Fd=new Arbre(this.val,A.Fg.Fd.Fd,A.Fd);
}
private void rotationD1(Arbre A) {
this.val=A.Fg.getValeur();
Fg=A.Fg.Fg;
Fd=new Arbre(this.val,A.Fg.Fd,A.Fd);
}
} |
Partager