insert into binary tree question..

This is a discussion on insert into binary tree question.. within the C Programming forums, part of the General Programming Boards category; regarding to what hk_mp5kpdw says: i understood that i should write Code: node *root my problem is with the start ...

  1. #16
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    regarding to what hk_mp5kpdw says:
    i understood that i should write
    Code:
    node *root
    my problem is with the start of the signature of this function
    my function returns a node pointer so
    when i will write
    Code:
    node  insert (node *root,int data);
    my function will return node
    but i need node pointer

    i changed it like you said
    it doesnt work
    Code:
    #include <stdio.h>
    
    typedef struct node{
      struct  node *left;
       struct node *right;
        int data;
    
    }node;
    node  insert (node *root,int data);
    
    int main(){
    
    node * root=NULL;
    root=insert(root,2);
    root=insert(root,1);
    root=insert(root,10);
    root=insert(root,5);
        return 0;
    }
    
    node   insert (node *root,int data){
     if (!root){
         root=malloc(sizeof(node));
         root->data=data;
         root->right=NULL;
         root->left=NULL;
    
     }//end if
    
     else{
         if (data<=root->data){ //start if check
              root->left=insert(root->left,data);
         }//end if check
         else {
              root->right=insert(root->right,data);
         }
    
     }//end else
          return root;
    }//end insert

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You do not want to return the node - you want to return the pointer to a node, that is absolutely sure.

    Sorry if I didn't notice that in previous posts.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so in a function signature which looks like this:
    Code:
    node   insert (node *root,int data){
    i expect it to return a node
    i think we should put an Astrix to indicate that it returns a node pointer
    ??
    Last edited by transgalactic2; 11-17-2008 at 03:26 PM.

  4. #19
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    You likely want:
    Code:
    node * insert(node *root,int data);
    ...as your "signature".

    Your return in that function should be:
    Code:
    return root;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #20
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this is the correct signature
    Code:
    node * insert(node *root,int data)
    ?
    Last edited by transgalactic2; 11-17-2008 at 10:09 PM.

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks it runs without errors

    Code:
    #include <stdio.h>
    
    typedef struct node{
      struct  node *left;
       struct node *right;
        int data;
    
    }node;
    node * insert (node *root,int data);
    
    int main(){
    
    node * root=NULL;
    root=insert(root,2);
    root=insert(root,1);
    root=insert(root,10);
    root=insert(root,5);
        return 0;
    }
    
    node  * insert (node *root,int data){
     if (!root){
         root=malloc(sizeof(node));
         root->data=data;
         root->right=NULL;
         root->left=NULL;
    
     }//end if
    
     else{
         if (data<=root->data){ //start if check
              root->left=insert(root->left,data);
         }//end if check
         else {
              root->right=insert(root->right,data);
         }
    
     }//end else
          return root;
    }//end insert

  7. #22
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    do i have to use free command on each node in here

    will it damage my computer if i will leave it as it i now
    (fill out my memory)

  8. #23
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    What makes you think that you should completely disregard basic rules of the English languaue, such as CAPITAL LETTERS at the start of a sentence, and a dot/period/fullstop at the end?
    If you're too lazy to write proper sentences and questions, then I can't be bothered reading them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #24
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Do i have to use free command on each node in here.

    Will it damage my computer if i will leave it as it i now?
    (fill out my memory)

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Quote Originally Posted by transgalactic2
    Do i have to use free command on each node in here.
    Yes, you could have a destroy function that will destroy the node and its children.

    Will it damage my computer if i will leave it as it i now?
    (fill out my memory)
    There would be memory leaks, but that will not damage your computer, and in fact you are probably using an OS where all the memory will be reclaimed by the OS when your program exits.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    After i finished changing the insert() function,i wanted to see the structure of the tree.
    So i built a printing function and inputted my tree in it.
    I didnt get any syntax errors but when i run the program,my computer (not the compiler) says
    "tree.exe encountered a problem and need to close.. ",then i get no output on the CMD box.
    Code:
     #include <stdio.h>
    
    typedef struct node{
      struct  node *left;
       struct node *right;
        int data;
    
    }node;
    node * insert (node *root,int data);
    void showtree(node *root);
    int main(){
    
    node * root=NULL;
    root=insert(root,2);
    root=insert(root,1);
    root=insert(root,10);
    root=insert(root,5);
    
    showtree(root);
        return 0;
    }
    
    node  * insert (node *root,int data){
     if (!root){
         root=malloc(sizeof(node));
         root->data=data;
         root->right=NULL;
         root->left=NULL;
    
     }//end if
    
     else{
         if (data<=root->data){ //start if check
              root->left=insert(root->left,data);
         }//end if check
         else {
              root->right=insert(root->right,data);
         }
    
     }//end else
          return root;
    }//end insert
    
    void showtree(node *root){
    
    showtree(root->left);
    if (!root){
        printf("&#37;d/n/t",root->data);
    
    }
    showtree(root->right);
    
    
    }

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    After i finished changing the insert() function,i wanted to see the structure of the tree.
    So i built a printing function and inputted my tree in it.
    I didnt get any syntax errors but when i run the program,my computer (not the compiler) says
    "tree.exe encountered a problem and need to close.. ",then i get no output on the CMD box.
    Code:
    if (!root){
        printf("%d/n/t",root->data);
    When is this printed?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    You are right i have changed this line into

    Code:
    if ((!root->left)&&(!root->right)){
    It checks if the node is a leaf.

    Still i get this bug from my computer that tree.exe encountered a problem.

    ??

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suggest you think about what you actually want your tree printing function to do, and under what conditions, and bear in mind that it may be a good thing if it actually works for empty trees as well. Remember that you can not EVER access a NULL pointer, so if root is NULL in the above if-statement, root->left will cause a memory access violation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #30
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    You are right,i put an IF on the whole inside of the function and it doesnt give me any bug.
    But it gave me not the output i have excpected.

    It gives me two numbers.
    Code:
    1
              5
    How this could happen i put four number in this tree??

    Code:
    #include <stdio.h>
    
    typedef struct node{
      struct  node *left;
       struct node *right;
        int data;
    
    }node;
    node * insert (node *root,int data);
    void showtree(node *root);
    int main(){
    
    node * root=NULL;
    root=insert(root,2);
    root=insert(root,1);
    root=insert(root,10);
    root=insert(root,5);
    
    showtree(root);
        return 0;
    }
    
    node  * insert (node *root,int data){
     if (!root){
         root=malloc(sizeof(node));
         root->data=data;
         root->right=NULL;
         root->left=NULL;
    
     }//end if
    
     else{
         if (data<=root->data){ //start if check
              root->left=insert(root->left,data);
         }//end if check
         else {
              root->right=insert(root->right,data);
         }
    
     }//end else
          return root;
    }//end insert
    
    void showtree(node *root){
    if (root){
    showtree(root->left);
    if ((!root->left)&&(!root->right)){
        printf("&#37;d\n\t",root->data);
    
    }
    showtree(root->right);
    
    }
    }
    Last edited by transgalactic2; 11-18-2008 at 10:28 AM.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 01:02 PM
  2. Binary Tree
    By Ideswa in forum C Programming
    Replies: 12
    Last Post: 10-25-2007, 12:24 PM
  3. 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
  4. binary search tree help
    By noob2c in forum C++ Programming
    Replies: 6
    Last Post: 11-09-2003, 01:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21