summaryrefslogtreecommitdiff
path: root/sem_4/Algo/TP7/ArbreBinaireRecherche.cpp
blob: ecd5af958437087ad1da4186c8cda4edabe47cff (plain)
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
#include "ArbreBinaireRecherche.h"

SommetABR::SommetABR(Valeur v){
  racine=v; SAG=NULL; SAD=NULL;Pere=NULL;
}

SommetABR::SommetABR(SommetABR& s){
  racine=s.racine; SAG=NULL; SAD=NULL;
  if (s.SAG) GrefferSAG(new SommetABR(*(s.SAG)));
  if (s.SAD) GrefferSAD(new SommetABR(*(s.SAD)));
}

ABR SommetABR::PlusPetit(){
  if (this->SAG==NULL){
    return this;
  }
  else {
    return this->SAG->PlusPetit();
  }
}

ABR SommetABR::RechercherValeur(Valeur v){
  if (this!= NULL){
    if( v < this->racine){
      this->SAG->RechercherValeur(v);
    }
    else if (v > this->racine){
      this->SAD->RechercherValeur(v);
    }
    else if ( v == this->racine){
      return this;
    }
  }
  return NULL;
}

void SommetABR::InsererValeur(Valeur v){
  if (v < racine){
    if (SAG!=NULL){
      SAG->InsererValeur(v);
    }
    else {
      GrefferSAG(SommetABR(v));
    }
  }
  else {
    if (SAD!=NULL){
      SAD->InsererValeur(v);
    }
    else {
      GrefferSAD(SommetABR(v));
    }
  }
}

ABR SommetABR::SupMin(){
  SupprimerValeur(PlusPetit()->racine);
  return this;
}


ABR SommetABR::SupprimerValeur(Valeur v){
  if ( this!=NULL){
    if (v < racine){
      SAG->SupprimerValeur(v);
    }
    else if ( v > racine){
      SAD->SupprimerValeur(v);
    }
    else {
      this->racine = this->SAD->racine;
    }
  }
  return NULL;
}





int main() {
  ABR A1=new SommetABR(11);
  ABR A2=new SommetABR(9);
  ABR A3=new SommetABR(14);
  ABR A4=new SommetABR(3);
  ABR A5=new SommetABR(20);
  A1->GrefferSAG(A2);
  A1->GrefferSAD(A3);
  A2->GrefferSAG(A4);
  A3->GrefferSAD(A5);
  std::cout<<A1->RechercherValeur(11)<<std::endl;
  return 1;
}

/* compiler avec g++  ArbreBinaireRecherche.cpp SortieLatex.cpp  */