Page 232 - DCAP407_DATA_STRUCTURE
P. 232

Unit 12:  Binary Tree Traversals and Operations



                                     if(no==-1)
                                            break;         //If condition is true, the if loop breaks
                                     p=root;               //Assign value of root to p variable
                                     q=root;               //Assign value of root to q variable

                                     /*Check if no is not equal to variable p and q not equal to NULL*/
                                     while(no!=p->data && q!=NULL)
                                     {
                                            p=q;
                                            if(no<p->data)        //Check if no is less than variable p
                                            /*Set q to the left sub-tree of p*/
                                                   q=p->left;
                                            else

                                            /*Set q to the right sub-tree of p*/
                                                   q=p->right;
                                     }
                                     /*Check if variable no is less than p variable with data*/
                                     if(no<p->data)
                                     {



                                            /*prints the node of left tree*/
                                            printf("\n Left branch of %d is %d",p->data,no); left(p,no);
                                     }
                                     else
                                     {
                                            right(p,no);

                                            /*prints the node of right tree*/
                                            printf("\n Right Branch of %d is %d",p->data,no);//
                                     }
                                     while(1)
                                     {
                                     printf("\n 1.Inorder Traversal  \n  2.Preorder Traversal  \n 3.Postorder
                                     Traversal \n 4.Exit");
                                     printf("\n Enter choice:");
                                     scanf("%d",&choice);         // Reads the choice entered
                                     switch(choice)
                                     {

                                     /*Switches to inorder function and performs inorder traversal*/
                                            case 1 :inorder(root);



                                        LOVELY PROFESSIONAL UNIVERSITY                          225
   227   228   229   230   231   232   233   234   235   236   237