Thread: memmory addresses not working out or me screwing somthing up

  1. #31
    Registered User
    Join Date
    May 2019
    Posts
    214
    The problem is in your phrase:

    the node that is being inserted doesn't know
    It doesn't have to.

    The recursive call knows what it's parent is. The node doesn't need to know.

    That's the point of the ** as I mention above.

  2. #32
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    im not getting this. why is i changed it was set to p

  3. #33
    Registered User
    Join Date
    May 2019
    Posts
    214
    Which part?

    Oh, I see.....yes, if all of the snippets were thought to be one body of code, sure.....but whatever p may be, i will be changed to it by f2, same as i = p would do.

    I supposed I should have suggested that p might have changed in the meantime, and i is updated to p through f2, same as i = p would do.
    Last edited by Niccolo; 06-13-2019 at 04:56 PM.

  4. #34
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    tree is empty
    i send in node 1
    it returns

    were not done yet as i kept finding out
    i then have to set tree->root to node 1

    i send in node 2
    it finds node one and goes to node 1's right and sends in node 1's address
    inserts 2 by *root = node
    returns
    i send node three in
    it finds node 1 and goes to node 1's right and sends in node 1's address
    it finds node 2 and goes to node 2's right and sends in node 2's address
    inserts 3 by *root = node
    returns

    each time i have sent in the address of the parent node.... r more precisely i have sent in the address of where the parent nodes right pointer is

  5. #35
    Registered User
    Join Date
    May 2019
    Posts
    214
    Let me be a little more obvious. Let's say I have
    Code:
    Bin_Tree *tr = create_tree();
    I then call

    Code:
    set_root( &(tr->root), node );
    Code:
    void set_root( Bin_Node **root, Bin_Node *n )
    {
      *root =n->left;
    }
    tr's root was changed to n's left node.

  6. #36
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    like you did with your function you sent in a pointer to a pointer and then changed the value it pointed at... ie in the case of the tree it was pointing at null but once we get to the point of insertion it gets set to point to the node

  7. #37
    Registered User
    Join Date
    May 2019
    Posts
    214
    If I follow you, yes, that's it.

  8. #38
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    were typing at the same time here lol

  9. #39
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    so following that train of thought to set the tree root to point to two i have to know what was pointing at 1

  10. #40
    Registered User
    Join Date
    May 2019
    Posts
    214
    Yes, and you will because at the entrance to the function you'll have the ** representing tree->root. It will be 1, and when you rotate that will modify tree->root, though it will only know it as a Bin_Node **

    The point there is that later, when you have a larger tree, your rotation may be anywhere in the tree, much lower, not affecting tree->root. The function will work the same, though, as the parameter will be passed as &((*root)->left) or &((*root)->right) in the recursive calls of insert_node

    In other words, at each level of the tree, descending, the subtree is not special, it looks like any tree (just smaller and smaller the more you descend), and should function the same at all levels, whether the source was Bin_Tree's root, or a deeply seated Bin_Node's left or right.
    Last edited by Niccolo; 06-13-2019 at 05:15 PM.

  11. #41
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    oh wait i think the penny has dropped (yes there was a loud echoing clang) i am rotating node to the left.... so **root is whats pointing at node
    in other words 1 is node node->left is 2 and node->left->left is 3
    so to make what ever is pointing at node 1 i would make *root point at 2

  12. #42
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    you beat me to it lol

  13. #43
    Registered User
    Join Date
    May 2019
    Posts
    214
    In yet another way to say it, &(tree->root) looks like a parent inside insert_node, and &((*root)->left) makes that Bin_Node's left look like a parent inside insert_node

  14. #44
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    ok to take your examples further....
    Code:
    f1( **p) { f2(&(*p))??
    f2 (**p) or (***p)

  15. #45
    Registered User
    Join Date
    May 2019
    Posts
    214
    *** is gettin' a little nuts


    I'm going to have to think about f2 or ***p over another cup of coffee....the earlier ones are all used up

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 08-19-2011, 06:53 PM
  2. Working with memory addresses
    By Fujy in forum C++ Programming
    Replies: 7
    Last Post: 01-18-2010, 09:31 AM
  3. Memmory Issues and Threads
    By ddod in forum Windows Programming
    Replies: 2
    Last Post: 08-13-2004, 10:30 AM
  4. Freeing memmory - Automatic?
    By Vber in forum C Programming
    Replies: 4
    Last Post: 04-23-2003, 04:43 AM
  5. Memmory Allocation
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 04-16-2002, 01:56 PM

Tags for this Thread