Advertisement
amine99

Untitled

May 20th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node {
  5.    char c;
  6.    node *sag;
  7.    node *sad;
  8. };
  9. typedef struct node *tree;
  10.  
  11. tree make_node(char c,tree sag,tree sad) {
  12.    tree t;
  13.    t = (tree)malloc(sizeof(t));
  14.    t->c = c;
  15.    t->sag = sag;
  16.    t->sad = sad;
  17.    return t;
  18. }
  19.  
  20. tree make_leaf(int x) {
  21.    return make_node(x,NULL,NULL);
  22. }
  23.  
  24. tree make_tree() {
  25.    return make_node('*',make_node('+',make_leaf('14'),make_node('/',make_leaf('8'),make_leaf('2'))),make_node('-',make_leaf('5'),make_leaf('3')));
  26. }
  27.  
  28. void prefix(tree t) {
  29.    if(t != NULL) {
  30.       cout << t->c << " ";
  31.       prefix(t->sag);
  32.       prefix(t->sad);
  33.    }
  34. }
  35.  
  36. void infix(tree t) {
  37.    if(t != NULL) {
  38.       infix(t->sag);
  39.       cout << t->c << " ";
  40.       infix(t->sad);
  41.    }
  42. }
  43.  
  44. void postfix(tree t) {
  45.    if(t != NULL) {
  46.       postfix(t->sag);
  47.       postfix(t->sad);
  48.       cout << t->c << " ";
  49.    }
  50. }
  51.  
  52. queue<tree> q;
  53.  
  54. void bfs(tree t) {
  55.    if(t != NULL)
  56.       q.push(t);
  57.    while(!q.empty()) {
  58.       tree x = q.front();
  59.       q.pop();
  60.       cout << x->c << " ";
  61.       if(x->sag != NULL)
  62.          q.push(x->sag);
  63.       if(x->sad != NULL)
  64.          q.push(x->sad);
  65.    }
  66. }
  67.  
  68. int height(tree t) {
  69.    if(t == NULL)
  70.       return 0;
  71.    else {
  72.       int maxL = height(t->sag);
  73.       int maxR = height(t->sad);
  74.       return max(maxL,maxR)+1;
  75.    }
  76. }
  77.  
  78. bool isLeaf(tree node) {
  79.    return node->sad == NULL && node->sag == NULL;
  80. }
  81.  
  82. int leaf(tree t) {
  83.    if(t == NULL)
  84.       return 0;
  85.    if(isLeaf(t))
  86.       return 1;
  87.    return leaf(t->sad)+leaf(t->sag);
  88. }
  89.  
  90. tree srch(tree t,char c) {
  91.    tree found;
  92.    if(t == NULL)
  93.       found = NULL;
  94.    else {
  95.       found = srch(t->sag,c);
  96.       if(!found)
  97.          found = srch(t->sad,c);
  98.    }
  99.    return found;
  100. }
  101.  
  102. int main() {
  103.    tree root;
  104.    root = make_tree();
  105.    cout << "Prefix : ";
  106.    prefix(root);
  107.    cout << "\nInfix : ";
  108.    infix(root);
  109.    cout << "\nPostfix : ";
  110.    postfix(root);
  111.    cout << "\nEn largeur : ";
  112.    bfs(root);
  113.    cout << "\nHeigth : " << height(root);
  114.    cout << "\nLeaf : " << leaf(root);
  115.    cout << endl;
  116.    if(srch(root,'*') != NULL)
  117.       cout << srch(root,'*')->c;
  118.    else
  119.       cout << "NOT found";
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement