Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

c - Printf with scanf asking for input. Loop error with BST

I have been trying to see why printf does not break the loop after printing the input of the file. The .c file is BST and I am now in the middle of testing whether a tree has been built but can't seem to exit the printf loop. I need the printf loop for the correct output for the code. Any suggestions of why this error is happening. Full code and output is shown.

#include "bst.h"
#include <stdio.h>
#include <stdlib.h>


///Author: Joe W

//arbitrary list of temp nodes

TreeNode *new_node, *root, *tmp, *parent;
int elemArray[100], i1, i2, i0;

/*
Insert a new node into the tree by referencing the root and using recursion
*/

    TreeNode* getN(int dataElem) {
        TreeNode* temp;
        temp = malloc(sizeof(TreeNode*));
        temp-> left = NULL;
        temp-> right = NULL;
        temp->data =  dataElem;
        return temp;
    } 



TreeNode* addNodeToTree(TreeNode *root, int data) {
    TreeNode* newNode = malloc(sizeof(TreeNode*));
    if (root == NULL)
    {

                                            // thingy too. Creating a function too,
                                            // for you to look at.
        root = newNode;
        return root;
    }
    else
    {
        TreeNode* parent = malloc(sizeof(TreeNode*));
        TreeNode* current = malloc(sizeof(TreeNode*));
        parent = current = root;
        // This loop will actually help us, find the `parent`, 
        // of the `newNode`(which is to be inserted)
        while (current != NULL)
        {
            parent = current;
            if (current->data > data)
                current = current->left;
            else if (current->data < data)
                current = current->right;
        }
        // Now once, we have found, the node, under which the
        // newNode will be placed, now we have to check, whether
        // newNode will become a `left child/right child` of the 
        // parent.
        newNode = getN(data);
        if (parent->data > data)
            parent->left = newNode;
        else if (parent->data < data)
            parent->right = newNode;


        return root;
    }
}


void build_Tree(TreeNode** root, const int elements[], const int count) {

    //TreeNode* node = malloc(sizeof(TreeNode*));
    //node->left = node ->right = NULL;



    for ( i0 = 0; i0 < count; ++i0 ){
        *root = addNodeToTree(*root, elements[count]);
    }

}

This compiles fine. The error begins after the file takes user input and begins to display the code. with printf statements.

int main( int argc, char *argv[] ) {

    //Throw error is *Argv[] is not an integer
    //assuming it was an integer
    int cnt = atoi( argv[1] );
    printf( "number is %d
", cnt );
    //
    printf("Enter %i integer values to place in tree:
", cnt);
    for ( i1 = 0; i1 < cnt; ++i1) {
        scanf( "%d", &elemArray[i1] );
    }

    //first ouput "INput Values"

    printf( " Input Values:
 " );  
     for ( i2 = 0; i2 < cnt; ++i2) {
               printf( "%d
", elemArray[i2] );
               printf("building tree0
");
        }
    printf("building tree");
    TreeNode** root = malloc(sizeof(TreeNode*));
    printf("building tree");
    build_Tree(root, elemArray, cnt );
    printf("Tree Built");
    printf( "Preorder:
 ");

    //traverse
    //TreeNode* tempN = malloc(sizeof(TreeNode*));
    //tempN->data= 5;

    traverse( *root, PREORDER);             //pass the pointer of root to traverse the tree

    //traverse a single node 
    printf( "Inorder:
 ");

    printf( "Postorder:
 ");


    //Build tree with each element

    return 0;
}

void traverse( const TreeNode* root, const TraversalType type ) {

    if ( type == PREORDER) {
             if (root != NULL)
             {
                printf("%d", root->data);
                traverse( root->left, PREORDER);
                traverse( root-> right, PREORDER);
            }
    }
}




    /**
    void insertNode(TreeNode** root, TreeNode* new_node) {

       if (new_node-> data < *root-> data) {
          if (*root-> left == NULL) 
             *root-> left == new_node;
          else
            insert(*root->left, new_node);
       }

      if (new_node->data > *root->data) {
        if(*root-> right ==NULL)
          *root->right = new_node;
        else
          insert(*root->right, new_node);
      }
    }
    **/


//question1: what is the 

This is where I begin to run the code

jw8453@idaho:~/Courses/CS243/Homework/5$ make
gcc -ggdb  -c bst.c
gcc -ggdb -o bst bst.o  -lm
jw8453@idaho:~/Courses/CS243/Homework/5$ ./bst 5
number is 5
Enter 5 integer values to place in tree:
1
2
3
4
5
 Input Values:
 1
building tree0
2
building tree0
3
building tree0
4
building tree0
5
building tree0



this isnt supposed to be in the printf loop

Note: Question is a continuation of BST build tree double pointers

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is wrong:

TreeNode* newNode = malloc(sizeof(TreeNode*));

instead

TreeNode* newNode = malloc(sizeof(TreeNode));

or

TreeNode* newNode = malloc(sizeof(*newNode));

Here:

    TreeNode* parent = malloc(sizeof(TreeNode*));
    TreeNode* current = malloc(sizeof(TreeNode*));
    parent = current = root;

you have a memory leak, you reserve space for parent and current and then you assign those variables to another address, same for:

TreeNode* newNode = malloc(sizeof(TreeNode*));
...
newNode = getN(data);

I suggest

void setN(TreeNode *node, int dataElem) {
    node->left = NULL;
    node->right = NULL;
    node->data = dataElem;
}
...
setN(newNode, data);

instead of

TreeNode* getN(int dataElem) {
    TreeNode* temp;
    temp = malloc(sizeof(TreeNode*));
    temp-> left = NULL;
    temp-> right = NULL;
    temp->data = dataElem;
    return temp;
}
...
newNode = getN(data);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...