nie-ii-year

lab stuff from undergrad second year.
git clone http://git.hanabi.in/repos/nie-ii-year.git
Log | Files | Refs | LICENSE

tree.cpp (2368B)


      1 #include <iostream>
      2 
      3 struct node{
      4     struct node* left;
      5     struct node* right;
      6     int data;
      7 };
      8 
      9 class bstree{
     10 public:
     11   node *root;
     12   bstree(){
     13     root = NULL;
     14   }
     15   bool isNULL(struct node* r){
     16     if(r == NULL)
     17       return true;
     18     return false;
     19   };
     20   bool isNULLroot(){
     21     if(root == NULL)
     22       return true;
     23     return false;
     24   };
     25   void createRoot(int data) {
     26     root = (struct node*)malloc(sizeof(struct node));
     27     if(isNULL(root)){
     28       printf("Insufficient memory.\n");
     29       return;
     30     }
     31     root->data = data;
     32     root->left = root->right = NULL;
     33   };
     34   struct node* getnode(int data){
     35     struct node* temp = (struct node *)malloc(sizeof(struct node));
     36     if(isNULL(temp))
     37       printf("Insufficient memory.\n");
     38     else{
     39       temp->left = temp->right = NULL;
     40       temp->data = data;
     41     }
     42     return temp;
     43   };
     44   void insert(int data, struct node* parent){
     45     if(data < parent->data)
     46       if(!isNULL(parent->left))
     47         insert(data, parent->left);
     48       else parent->left = getnode(data);
     49     else
     50       if(!isNULL(parent->right))
     51         insert(data, parent->right);
     52       else parent->right = getnode(data);
     53   };
     54   bool contains(int data, struct node *parent){
     55     if(isNULL(parent))
     56       return false;
     57     if(parent->data == data)
     58       return true;
     59     if(data < parent->data)
     60       return contains(data, parent->left);
     61     if(data > parent->data)
     62       return contains(data, parent->right);
     63   };
     64   void printInOrder(struct node* parent){
     65     if(!isNULL(parent->left))
     66       printInOrder(parent->left);
     67     printf("%d ", parent->data);
     68     if(!isNULL(parent->right))
     69       printInOrder(parent->right);
     70   };
     71 };
     72 
     73 int main(){
     74   bstree b;
     75   int choice, d;
     76   do{
     77     printf("Your choice> ");
     78     scanf("%d", &choice);
     79     switch (choice) {
     80       case 1:
     81               printf("Enter a number: ");
     82               scanf("%d", &d);
     83               if(b.isNULLroot())  b.createRoot(d);
     84               else b.insert(d, b.root);
     85               break;
     86       case 2:
     87               printf("Enter the element to be searched: ");
     88               scanf("%d", &d);
     89               printf("%d\n", b.contains(d, b.root));
     90               break;
     91       case 3:
     92               if(b.isNULL(b.root))  printf("Empty tree.\n");
     93               else b.printInOrder(b.root);
     94               break;
     95       case 4:
     96               choice = 0;
     97     }
     98   }while(choice);
     99   return 0;
    100 }