Help:Transclusion/How Transclusion Works

This is an old revision of this page, as edited by 210.167.91.2 (talk) at 08:13, 11 July 2007. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

// BST (Binary Search Tree)

// Total Program Lines - 235

  1. include <iostream.h>
  2. include <stdlib.h>
  3. include <conio.h>

struct tree {

       int num;
       tree* left;
       tree* right;

}nn;

tree* head = NULL; tree* root;

void main() {

       int n=0;
       void create();
       void print();
       do {
               clrscr();
               cout<<"\t\t\t\t::::CHOICES::::\n\n";
               cout<<"\t 1 - Create\n\n";
               cout<<"\t 2 - Print\n\n";
               cout<<"\t 3 - Exit\n\n";
               cout<<"\nYOUR CHOICE IS:\t";
               cin>>n;
               cout<<"\n\n";
               switch(n) {
                       case 1: {
                               cout<<"\t\tCreate ::\n";
                               create();
                               cout<<"\n";
                               getch();
                               break;
                       }
                       case 2: {
                               cout<<"\t\tPrint ::\n";
                               print();
                               cout<<"\n";
                               getch();
                               break;
                       }
                       default: {
                               break;
                       }
               }
       }
       while(n < 3);

}

void create() {

       void insertNode(tree*,tree*);
       char choice;
       if (root == NULL) {
               do {
                       tree* nn;
                       nn = new tree;
                       cout<<"\n\nEnter Element :: ";
                       cin>>nn->num;
                       if(root == NULL) {
                               root = nn;
                               nn->left = NULL;
                               nn->right = NULL;
                       }
                       insertNode(root,nn);
                       cout<<"\n\tDo You want to enter more Elements y/n :: ";
                       cin>>choice;
               }
               while(choice == 'y' || choice == 'Y');
       }
       else {
               cout<<"\n\n\tTree is already created";
       }

}

void insertNode(tree* head,tree* nn) {

       if(head->num > nn->num) {
               if(head->left == NULL) {
                       head->left = nn;
                       nn->left = NULL;
                       nn->right = NULL;
               }
               else {
                       insertNode(head->left,nn);
               }
       }
       else if(head->num <= nn->num) {
               if(head->right == NULL) {
                       head->right = nn;
                       nn->left = NULL;
                       nn->right = NULL;
               }
               else {
                       insertNode(head->right,nn);
               }


       }

}

void print() {

       void printSubTree(tree*,int);
       head = root;
       int order;
       if (head == NULL) {
               cout<<"\n\n\tTree is Empty";
       }
       else {
               cout<<"\n\nPre-Order Traversal - 1";
               cout<<"\n\nPost-Order Traversal - 2";
               cout<<"\n\nIn-Order Traversal - 3";
               cout<<"\n\nPrint Tree Structure - 4";
               cout<<"\n\nYour Choice :: ";
               cin>>order;
               printSubTree(head,order);
       }

}

void printSubTree(tree* head,int order) {

       if(order == 1) {
               cout<<" "<<head->num;
               if(head->left != NULL) {
                       printSubTree(head->left,order);
               }
               if(head->right != NULL) {
                       printSubTree(head->right,order);
               }
       }
       else if(order == 2) {
               if(head->left != NULL) {
                       printSubTree(head->left,order);
               }
               if(head->right != NULL) {
                       printSubTree(head->right,order);
               }
               cout<<" "<<head->num;
       }
       else if(order == 3) {
               if(head->left != NULL) {
                       printSubTree(head->left,order);
               }
               cout<<" "<<head->num;
               if(head->right != NULL) {
                       printSubTree(head->right,order);
               }
       }

}