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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    my plan is to start of with three nodes to the right. set some variable to 1st 2nd and 3rd nodes and see if i can make it do what i want. then the next stage is to add a node to the root nodes left and try rotate to the left. each time adding nodes till i end up with 5 or 6 nodes with the node being moved to the left is somewhere in the middle. then once i have mastered that do it all with right rotation. then see if i can call the same function twice for double left... if not sit and work out that from the beginning. to do all this weather the tree is balanced or not is unimportant at this stage its just about getting the rotation functions correct

    then once all that is working i can start work on the actual balancing factors and only have to plug in the appropriate function

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    214
    @cooper1200,

    There is no spoon (from "The Matrix").

    In other words, there is no tr. I know, you have one, it's Bin_Tree, but look at your insert_node. It takes a Bin_Node **. Inside insert_node, there is no tr, no Bin_Tree.

    You don't need it.

    Bin_Tree has a root, I know.

    When you call insert_node, you call that from insert (where you do have Bin_Tree). But look at that call.

    Code:
     insert_node( &root, node );
    Actually, that's a mistake. It shouldn't be the temporary Bin_Node *root;

    That call should be

    Code:
    insert_node( &( tree->root ), node );
    I had not noticed that before. Actually, I recall mentioning it, but never mind...it should be changed to the tree->root, not root.

    Now, look at the insert_node signature:

    Code:
     bool insert_node( Bin_Node ** root, Bin_Node *node )....
    root is a pointer to a pointer, which is modified with something like

    *root = node;

    Or whatever context is appropriate. That will assign Bin_Tree's node. Insert_Node has (or will have when you fix that as I pointed out) Bin_Tree's root.

    Via pointer to a pointer (the **)

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

  4. #4
    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.

  5. #5
    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

  6. #6
    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. #7
    Registered User
    Join Date
    May 2019
    Posts
    214
    If I follow you, yes, that's it.

  8. #8
    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.

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

  10. #10
    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

  11. #11
    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

  12. #12
    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

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

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

  15. #15
    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