Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <iostream>

using namespace std;

struct Maillon{
int data;
int inf;
int sup;
Maillon *left;
Maillon *right;
};

Maillon *creeMaillon(int data){


Maillon *maillon = new Maillon;
maillon->data = data;
maillon->left = maillon->right = NULL;
maillon->inf = maillon->sup = data;
return maillon;
}

int Inf(Maillon *root){


Maillon *temp = root;
while(temp->left != NULL){
temp = temp->left;
}

return temp->data;
}

int Sup(Maillon *root){


Maillon *temp = root;
while(temp->right != NULL){
temp = temp->right;
}

return temp->data;
}

Maillon *insererMaillon(Maillon *root, int data){


Maillon *temp = creeMaillon(data);
if(root == NULL)
return temp;
if(data < root->data) {
root->left = insererMaillon(root->left, data);
root->sup = Sup(root);
root->inf = Inf(root);
//root->inf = root->left->inf;
//root->sup = root->right->sup;
}
else if(data > root->data) {
root->right = insererMaillon(root->right, data);
root->sup = Sup(root);
root->inf = Inf(root);
//root->sup = root->right->sup;
//root->inf = root->left->inf;
}
else
return root;
}
void AfficherABR(Maillon *root){
if(root != NULL){
AfficherABR(root->left);
cout << root->data << " ";
AfficherABR(root->right);
}
}

int main(int argc, char** ) {

Maillon *root = creeMaillon(23);


root = insererMaillon(root, 6);

root = insererMaillon(root, 14);


root = insererMaillon(root, 10);
root = insererMaillon(root, 22);

root = insererMaillon(root, 11);


root = insererMaillon(root, 7);
root = insererMaillon(root, 8);

root = insererMaillon(root, 12);


root = insererMaillon(root, 3);

root = insererMaillon(root, 20);


root = insererMaillon(root, 2);

cout << "***** " ;


cout << root->sup << endl;
cout << root->inf << endl;

AfficherABR(root);

return 0;
}

You might also like