Thread: Please help with Binary Tree

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    15

    Question Please help with Binary Tree

    The error message says that:
    Cannot convert `void *` to `treNode *`

    It says the problem is here:
    *treePtr = malloc(sizeof(TreeNode));

    This is my code:
    -------------------------------------

    #include <stdio.h>
    #include <conio.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 * ,int);
    void inorder(TreeNodePtr);
    void preorder(TreeNodePtr);
    void postorder(TreeNodePtr);

    int main()
    {
    int i, item;
    TreeNodePtr rootPtr =NULL;

    srand(time( NULL ));

    printf("The #'s being placed in the tree are:\n");

    for(i=1;i<=10; i++)
    {
    item = rand() % 15;
    printf("%3d", item);
    insertNode( &rootPtr, item );
    }
    printf("Preorder\n");
    preorder( rootPtr );
    printf("Inorder\n");
    inorder(rootPtr);
    printf("Postorder\n");
    postorder(rootPtr);
    getch();
    return 0;
    }

    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("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);
    }
    }

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You need to compile it as C code rather than C++ code.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    malloc returns a void * which means that when you use malloc, you will need to cast the return value to the type of pointer you want.

    *treePtr = malloc(sizeof(TreeNode));
    Change the above line to:
    *treePtr = (treNode *)malloc(sizeof(TreeNode));

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >you will need to cast the return value to the type of pointer you want.

    No, you don't. Compile it as C.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Sorensen is right, you should compile it as C.

    But a different thing, shouldn't

    *treePtr = malloc(sizeof(TreeNode));

    be

    treePtr = malloc(sizeof(TreeNode));

    because malloc returns a pointer?

    And it seems you forget to free the allocated memory.

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    But a different thing, shouldn't

    *treePtr = malloc(sizeof(TreeNode));

    be

    treePtr = malloc(sizeof(TreeNode));

    because malloc returns a pointer?
    *treePtr is a pointer. treePtr is a pointer to a pointer.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    No, you don't. Compile it as C.
    Some pure C compilers will indentify this as an error just like a C++ compiler will (ie MIPSPro C 7.2).

    My view is that the stronger type checking you have the greater portability the code will have.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM