Page 231 - DCAP407_DATA_STRUCTURE
P. 231

Data Structure



                          void preorder(struct node *r)                     //Define preorder function
                          {
                          /*Checks if r is not equal to NULL*/
                          if(r!=NULL)

                          {
                                 printf("\t %d",r->data);                   //Prints the data of the node
                                 / *Recursively call preorder passing the address of the left sub-tree*/
                                 preorder(r->left);
                                 / *Recursively call preorder passing the address of the right sub-tree*/
                                 preorder(r->right);
                          }
                          }

                          void postorder(struct node *r)                    //Define postorder function
                          {
                          if(r!=NULL)                                       //Checks if r is not equal to NULL
                          {
                                 / *Recursively call postorder passing the address of the left sub-tree*/
                                 postorder(r->left);

                                 / *Recursively call postorder passing the address of the left sub-tree*/
                                 postorder(r->right);
                                 printf("\t %d",r->data);      //Prints the data of the node
                          }
                          }
                          void main()
                          {

                                 int no;                      //Declare variable no
                                 int choice;                  //Declare variable choice
                                 clrscr();
                                 printf("\n Enter the root:");
                                 scanf("%d",& no);            //Reads the number entered
                                 root=make(no);               //Initialize the number to root

                                 p=root;                      // Value of root is then assigned to variable p
                                 while(1)                     //Checks the conditions provided in while loop
                                        {
                                                /*Prints the statement “Enter another number*/
                                                printf("\n Enter another number:"); scanf("%d", &no);
                                                //Reads the number entered
                                                /*Conditional statement, check if no is equal to -1*/



                          224                     LOVELY PROFESSIONAL UNIVERSITY
   226   227   228   229   230   231   232   233   234   235   236