Thread: Help with binary tree with bubble sorting

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Miami, Florida, United States
    Posts
    19

    Help with binary tree with bubble sorting

    I have the function for the binary tree here how do you implement the bubble sort in this program with a txt file to run this correctly?

    Create a program that sorts a given text file of integer type of numbers using bubble sort and prints on the screen the time elapsed for sorting. Then, the program should find a given value and display the position that the value was found on and the time taken to perform the search. The program then needs to read the file again and sort
    the file using a binary tree and display the time taken to sort. Lastly, you are
    supposed to use binary tree search to find the value and display the time elapsed to
    find the value.
    Your program has the following requirements:
    a. All program parameters should be send to the program through the command line arguments.
    b. The function scanf or cin should not be used at all in this program.
    c. The inputs and outputs of the program should look like the example below.


    C:>sort
    Invalid Usage. For proper usage try /?
    C:>sort /?
    This sort program compares bubble sorting and
    regular binary search with binary tree sorting
    and binary tree search. The run time for each
    algorithm is printed on the screen for
    comparison purposes.
    Usage: sort [filename.txt] [number]
    Where filename is the name of the file where the
    numbers to be sorted will be found and number is
    the number to look for
    C:\sort numbers.txt
    Invalid usage. Arguments are missing
    C:\sort numbegs.txt 18
    Could not find the file “numbegs.txt”. Please
    check the name and try again.C:>sort numbers.txt 1823
    Bubble Sorting Run Time: 120ms
    Executing Binary Search…
    Value 1823 found at row 32
    Binary Search Run Time: 15ms
    Binary Tree Sort Run Time: 68ms
    Executing Binary Tree Search…
    Value 1823 found at row 32
    Binary Tree Search Run Time: 8ms
    Sorting complete…



    My Program
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    struct treeNode {
       struct treeNode *leftPtr;
       int data;
       struct treeNode *rightPtr;
    };
    
    
    typedef struct treeNode TreeNode;
    typedef TreeNode *TreeNodePtr;
    
    
    void insertNode( TreeNodePtr *treePtr, int value );
    void inOrder( TreeNodePtr treePtr );
    void preOrder( TreeNodePtr treePtr );
    void postOrder( TreeNodePtr treePtr );
    
    
    
    
    int main( void )
    {
       unsigned int i;
       int item;
       TreeNodePtr rootPtr = NULL;
    
    
       srand( time( NULL ) );
       puts( "The numbers being placed in the tree are:" );
    
    
    
    
       for ( i = 1; i <= 10; ++i ) {
          item = rand() % 15;
          printf( "%3d", item );
          insertNode( &rootPtr, item );
       }
    
    
       puts( "\n\nThe preOrder traversal is:" );
       preOrder( rootPtr );
    
    
    
    
       puts( "\n\nThe inOrder traversal is:" );
       inOrder( rootPtr );
    
    
    
    
       puts( "\n\nThe postOrder traversal is:" );
       postOrder( rootPtr );
    }
    
    
    
    
    void insertNode( TreeNodePtr *treePtr, int value )
    {
    
    
       if ( *treePtr == NULL ) {
          *treePtr = malloc( sizeof( TreeNode ) );
    
    
    
    
          if ( *treePtr != NULL ) {
             ( *treePtr )->data = value;
             ( *treePtr )->leftPtr = NULL;
             ( *treePtr )->rightPtr = NULL;
          }
          else {
             printf( "%d not inserted. No memory available.\n", value );
          }
       }
       else {
          if ( value < ( *treePtr )->data ) {
             insertNode( &( ( *treePtr )->leftPtr ), value );
          }
    
    
    
    
          else if ( value > ( *treePtr )->data ) {
             insertNode( &( ( *treePtr )->rightPtr ), value );
          }
          else {
             printf( "%s", "dup" );
          }
       }
    }

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    You have the binary tree part going, you now need to read the data into an array of pointer-to-char also (or a linked list, would be better really) and then run bubblesort on that linked list or array of pointers. Compare those times. Once you have the sorted array do a binary-search on it to find the requested value (given on the command line). Then compare the result it took to do the binary search to looking up the value in your binary tree.

    You'll need to think about how you're going to handle sorting with duplicate values since your insertNode function just discards those.

    EDIT: Err realized you can't do a binary search on a linked list so you pretty much have to use an array of pointers for doing the bubblesort.
    Last edited by nonpuz; 12-02-2014 at 11:29 PM.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Location
    Miami, Florida, United States
    Posts
    19
    So like this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define FILENAME "numbers.txt"
    
    
    
    
    
    
    typedef struct list {
        int data;
        struct list *next;
    } LIST;
    
    
    
    
    LIST *append( LIST *, int );
    LIST *sort( LIST * );
    LIST *list_switch( LIST *, LIST * );
    void print_list( LIST * );
    
    
    struct treeNode {
       struct treeNode *leftPtr;
       int data;
       struct treeNode *rightPtr;
    };
    
    
    typedef struct treeNode TreeNode;
    typedef TreeNode *TreeNodePtr;
    
    
    void insertNode( TreeNodePtr *treePtr, int value );
    void inOrder( TreeNodePtr treePtr );
    void preOrder( TreeNodePtr treePtr );
    void postOrder( TreeNodePtr treePtr );
    
    
    
    
    int main( void )
    {
       unsigned int i;
       int item;
       TreeNodePtr rootPtr = NULL;
    
    
       srand( time( NULL ) );
       puts( "The numbers being placed in the tree are:" );
    
    
    
    
       for ( i = 1; i <= 10; ++i ) {
          item = rand() % 15;
          printf( "%3d", item );
          insertNode( &rootPtr, item );
       }
    
    
       puts( "\n\nThe preOrder traversal is:" );
       preOrder( rootPtr );
    
    
    
    
       puts( "\n\nThe inOrder traversal is:" );
       inOrder( rootPtr );
    
    
    
    
       puts( "\n\nThe postOrder traversal is:" );
       postOrder( rootPtr );
    }
    
    
    
    
    void insertNode( TreeNodePtr *treePtr, int value )
    {
    
    
       if ( *treePtr == NULL ) {
          *treePtr = malloc( sizeof( TreeNode ) );
    
    
    
    
          if ( *treePtr != NULL ) {
             ( *treePtr )->data = value;
             ( *treePtr )->leftPtr = NULL;
             ( *treePtr )->rightPtr = NULL;
          }
          else {
             printf( "%d not inserted. No memory available.\n", value );
          }
       }
       else {
          if ( value < ( *treePtr )->data ) {
             insertNode( &( ( *treePtr )->leftPtr ), value );
          }
    
    
    
    
          else if ( value > ( *treePtr )->data ) {
             insertNode( &( ( *treePtr )->rightPtr ), value );
          }
          else {
             printf( "%s", "dup" );
          }
       }
    }
    
    
    
    
    void inOrder( TreeNodePtr treePtr )
    {
    
    
       if ( treePtr != NULL ) {
          inOrder( treePtr->leftPtr );
          printf( "%3d", treePtr->data );
          inOrder( treePtr->rightPtr );
       }
    }
    
    
    
    
    void preOrder( TreeNodePtr treePtr )
    {
    
    
       if ( treePtr != NULL ) {
          printf( "%3d", treePtr->data );
          preOrder( treePtr->leftPtr );
          preOrder( treePtr->rightPtr );
       }
    }
    
    
    void postOrder( TreeNodePtr treePtr )
    {
    
    
       if ( treePtr != NULL ) {
          postOrder( treePtr->leftPtr );
          postOrder( treePtr->rightPtr );
          printf( "%3d", treePtr->data );
       }
    }

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    So you created a linked list structure. That works for bubblesort but you can't run a binary search on a linked list so you can't use that data structure. I would just use an array of strings, like so:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char ** argv)
    {
        char ** lines;
        size_t nlines = 0;
        char buf[BUFSIZ];
        size_t count = 0;
    
        /* Determine number of lines in file, set nlines to that number */
        /* tk; */
    
        lines = calloc(nlines, sizeof *lines);
        while (count < nlines && fgets(buf, sizeof buf/sizeof *buf, stdin)) {
            if ( (lines[count] = malloc(strlen(buf) + 1)) ) {
                strcpy(lines[count], buf);
                ++count;
            } else
                break;
        }
    
        /* Now bubblesort 'lines' */
        /* tk; */
    
            return 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2014
    Location
    Miami, Florida, United States
    Posts
    19
    Thanks got it to work now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sorting of strings
    By waqas94 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2013, 09:16 AM
  2. Bubble Sorting Problem
    By jappy512 in forum C Programming
    Replies: 3
    Last Post: 12-02-2009, 06:54 PM
  3. Bubble Sorting
    By yukapuka in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2008, 09:44 PM
  4. bubble sorting a 2-d array
    By stodd04 in forum C Programming
    Replies: 5
    Last Post: 03-17-2005, 01:40 PM
  5. help with my bubble sorting of arrays
    By Matt in forum C Programming
    Replies: 1
    Last Post: 12-11-2001, 04:43 PM

Tags for this Thread