Question about adding to a BST with * and **

This is a discussion on Question about adding to a BST with * and ** within the C Programming forums, part of the General Programming Boards category; Hello, So, I wrote a code last week using pointer to a pointer to add a node to a BST ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    Question about adding to a BST with * and **

    Hello,

    So, I wrote a code last week using pointer to a pointer to add a node to a BST

    Code:
    orest addTree(Forest *root, int val)
    {
    if (*root== NULL)
    {
    //if the root node is null (tree is empty)
    //this means if this is your first node
    //or if you've made your way down the tree
    //and this is where you need to insert the node
    *root=malloc(sizeof(Tree));
    (*root)->value=val;
    (*root)->left=NULL;
    (*root)->right=NULL;
    }
    else if (val < (*root)->value)
    {
    //if the value you want to insert is less than the
    //value in the node, then recursive call this function, going left
    addTree(&(*root)->left, val);
    } 
    else if (val >= (*root)->value)
    {
    //if the value you want to insert is greater or = than the
    //value in the node, then recursive call this function, going right
    addTree(&(*root)->right, val);
    }
    But now as I try to do this with just using single pointers
    Code:
    Forest buildBST(Forest root, int val)
    {
    if(root==NULL)
    {
    root=malloc(sizeof(Tree));
    root->value=val;
    root->left=NULL;
    root->right=NULL;
    
    
    }
    else if (val <= root->value)
    {
    buildBST(root->left, val);
    }
    else if (val > root->value)
    {
    buildBST(root->right, val);
    }
    return root;
    But it doesn't seem to work.

    Is there something different that needs to be done when implementing BST's given a pointer to a pointer rather than juts a pointer?


    Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,469
    Yes. C only does pass by value. Therefore, if you want to change the value of something in a function, and have that change persist after the function, then you need to pass a pointer to that thing. If you want to change an int, you pass a pointer to an int, and change the int through the pointer. Same thing applies if that thing is already a pointer: you pass a pointer to a pointer. If Forest is a pointer to something, and you wish to change what it points to, i.e. you want it to hold the value of malloc after the function is done, you have to pass it as Forest *root, and use *root = malloc in the function.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Awesome - you're attempting a BST without this
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a local Variable to a Vector Question
    By cpsmusic in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2011, 08:34 AM
  2. Question about adding integers to char
    By blueshift1980 in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2009, 12:51 PM
  3. quick question (adding characters)
    By Cactus in forum C Programming
    Replies: 2
    Last Post: 09-24-2005, 03:54 PM
  4. adding to C++
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 01-06-2003, 12:02 PM
  5. VS .NET .lib adding
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-24-2002, 04:43 PM

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