Return to Snippet

Revision: 38991
at January 11, 2011 19:41 by tjakopec


Updated Code
struct element {
    int label;
    element *left,*right;
};
typedef element *cvor;
typedef element bt;

bt* InitB(int x, bt* T){
    T = new element;
    T->label = x;
    T->left = NULL;
    T->right = NULL;
    return T;
}//initB

void CreateLeftB(int x, cvor n, bt* T){
    if(n->left) {
        cout << "Greska" << endl << endl;
        return;
    }
    cvor novi = new element;
    n->left = novi;
    novi->label = x;
    novi->left = NULL;
    novi->right = NULL;
}//CreateLeftB

void CreateRightB(int x, cvor n, bt* T){
    if(n->right) {
        cout << "Greska" << endl << endl;
        return;
    }
    cvor novi = new element;
    n->right = novi;
    novi->label = x;
    novi->left = NULL;
    novi->right = NULL;
}//createRightB

cvor ParentB(cvor n, bt* T){
    if (n == T) return NULL;
        cvor roditelj = NULL;
        if (T->left){
                if (T->left == n) return T;
                else roditelj =  ParentB(n, T->left);
        }
        if(T->right){
                if (T->right == n) return T;
                else roditelj = ParentB(n, T->right);
        }       
        return roditelj;
}//parentB

int LabelB(cvor n, bt* T){
    return n->label;
}//labelB

void ChangeLabelB(int x, cvor n, bt* T){
    if(!n) return;
    n->label = x;
}//changeLabelB

cvor LeftChildB(cvor n, bt* T){
    if(!n || !n->left) return NULL;
    return n->left;  
}//leftCildB

cvor RightChildB(cvor n, bt* T){
    if(!n || !n->right) return NULL;
    return n->right;  
}//rightCildB

cvor RootB(bt* T){
    if(!T) return NULL;
    return T;
}//rootB

void DeleteB(cvor n, bt* T){
    static bool jednom = false;
    if(!jednom) {
      cvor roditelj = ParentB(n, T);
      if(roditelj->left == n) roditelj->left = NULL;
      else roditelj->right = NULL;
      jednom = true;
    }

    if(n->left) DeleteB(n->left, T);
    if(n->right) DeleteB(n->right, T);
    delete n;
}//deleteB

Revision: 38990
at January 11, 2011 19:31 by tjakopec


Initial Code
1.
      struct element {
   2.
      int label;
   3.
      element *left,*right;
   4.
      };
   5.
      typedef element *cvor;
   6.
      typedef element bt;
   7.
       
   8.
      bt* InitB(int x, bt* T){
   9.
      T = new element;
  10.
      T->label = x;
  11.
      T->left = NULL;
  12.
      T->right = NULL;
  13.
      return T;
  14.
      }//initB
  15.
       
  16.
      void CreateLeftB(int x, cvor n, bt* T){
  17.
      if(n->left) {
  18.
      cout << "Greska" << endl << endl;
  19.
      return;
  20.
      }
  21.
      cvor novi = new element;
  22.
      n->left = novi;
  23.
      novi->label = x;
  24.
      novi->left = NULL;
  25.
      novi->right = NULL;
  26.
      }//CreateLeftB
  27.
       
  28.
      void CreateRightB(int x, cvor n, bt* T){
  29.
      if(n->right) {
  30.
      cout << "Greska" << endl << endl;
  31.
      return;
  32.
      }
  33.
      cvor novi = new element;
  34.
      n->right = novi;
  35.
      novi->label = x;
  36.
      novi->left = NULL;
  37.
      novi->right = NULL;
  38.
      }//createRightB
  39.
       
  40.
      cvor ParentB(cvor n, bt* T){
  41.
      if (n == T) return NULL;
  42.
      cvor roditelj = NULL;
  43.
      if (T->left){
  44.
      if (T->left == n) return T;
  45.
      else roditelj = ParentB(n, T->left);
  46.
      }
  47.
      if(T->right){
  48.
      if (T->right == n) return T;
  49.
      else roditelj = ParentB(n, T->right);
  50.
      }
  51.
      return roditelj;
  52.
      }//parentB
  53.
       
  54.
      int LabelB(cvor n, bt* T){
  55.
      return n->label;
  56.
      }//labelB
  57.
       
  58.
      void ChangeLabelB(int x, cvor n, bt* T){
  59.
      if(!n) return;
  60.
      n->label = x;
  61.
      }//changeLabelB
  62.
       
  63.
      cvor LeftChildB(cvor n, bt* T){
  64.
      if(!n || !n->left) return NULL;
  65.
      return n->left;
  66.
      }//leftCildB
  67.
       
  68.
      cvor RightChildB(cvor n, bt* T){
  69.
      if(!n || !n->right) return NULL;
  70.
      return n->right;
  71.
      }//rightCildB
  72.
       
  73.
      cvor RootB(bt* T){
  74.
      if(!T) return NULL;
  75.
      return T;
  76.
      }//rootB
  77.
       
  78.
      void DeleteB(cvor n, bt* T){
  79.
      static bool jednom = false;
  80.
      if(!jednom) {
  81.
      cvor roditelj = ParentB(n, T);
  82.
      if(roditelj->left == n) roditelj->left = NULL;
  83.
      else roditelj->right = NULL;
  84.
      jednom = true;
  85.
      }
  86.
       
  87.
      if(n->left) DeleteB(n->left, T);
  88.
      if(n->right) DeleteB(n->right, T);
  89.
      delete n;
  90.
      }//deleteB

Initial URL

                                

Initial Description

                                

Initial Title
bstablo_pokazivac.h

Initial Tags

                                

Initial Language
C++