Advertisement
nontawat1996

Gift-HW2

Mar 9th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct treeNode{
  4.     struct treeNode *leftPtr;
  5.     int data;
  6.     struct treeNode *rightPtr;
  7. };
  8.  
  9. typedef struct treeNode TreeNode;
  10. typedef TreeNode *TreeNodePtr;
  11. void inOrder( TreeNodePtr treePtr )
  12. {
  13.    // if tree is not empty, then traverse
  14.    if ( treePtr != NULL ) {
  15.       inOrder( treePtr->leftPtr ); //Recursion to the left
  16.       printf( "%3d", treePtr->data );  //print the value
  17.       inOrder( treePtr->rightPtr ); //Recursion to the right
  18.    } // end if
  19. } // end function inOrder
  20.  
  21. void insertNode(TreeNodePtr *root, int value){
  22.    TreeNodePtr newnode,treePtr;
  23.    // if tree is empty
  24.    if ( *root == NULL ) {*root = (TreeNode*)malloc(sizeof(TreeNode));
  25.       // if memory was allocated, then assign data
  26.     if ( *root != NULL ) {
  27.          ( *root )->data = value;
  28.          ( *root )->leftPtr = NULL;
  29.          ( *root )->rightPtr = NULL;
  30.     } // end if
  31.     else {
  32.          printf( "%d not inserted. No memory available.\n", value );
  33.     } // end else
  34.    } // end if
  35.    else { // tree is not empty
  36.         treePtr=*root;
  37.         newnode = (TreeNodePtr)malloc(sizeof(TreeNodePtr));
  38.         newnode->leftPtr = NULL;
  39.         newnode->rightPtr = NULL;
  40.         newnode->data=value;
  41.         while(treePtr!=newnode)
  42.         {
  43.             if(value<=(treePtr)->data)
  44.             {
  45.               if(!treePtr->leftPtr) (treePtr)->leftPtr=newnode;
  46.                treePtr=(treePtr)->leftPtr;
  47.             }
  48.             else
  49.             {
  50.              if(!treePtr->rightPtr) {(treePtr)->rightPtr=newnode; }
  51.              treePtr=(treePtr)->rightPtr;
  52.             }
  53.  
  54.         }
  55.     }
  56. }
  57.  
  58. void printTree(TreeNodePtr treePtr, int n ){
  59.     int a=n,i;
  60.     if ( treePtr != NULL ){
  61.         printTree( treePtr->rightPtr,a+1 );
  62.         for(i=0;i<n;i++) printf("        ");
  63.  
  64.         printf( "%3d\n", treePtr->data );
  65.         printTree( treePtr->leftPtr,a+1 );
  66.     }
  67. }
  68.  
  69. int main( int argv ,char *argc[])
  70. {
  71.    int a,b=0;
  72.    unsigned int i; // counter to loop from 1-10
  73.    int item; // variable to hold random values
  74.    TreeNodePtr rootPtr = NULL; // tree initially empty
  75.    srand( time( NULL ) );
  76.    puts( "The numbers being placed in the tree are:" );
  77.     for ( i = 1; i < argv; i++ ) {
  78.           a=atoi(argc[i]);
  79.           //item = rand() % 15; // insert random values between 0 and 14 in the tree
  80.           //printf( "%3d", a );
  81.           insertNode( &rootPtr, a );
  82.        } // end for
  83.  
  84.    // traverse the tree preOrder
  85.    printTree( rootPtr,b );
  86. } // end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement