The problem is in your phrase:
It doesn't have to.the node that is being inserted doesn't know
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.
The problem is in your phrase:
It doesn't have to.the node that is being inserted doesn't know
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.
im not getting this. why is i changed it was set to p
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.
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
Let me be a little more obvious. Let's say I have
I then callCode:Bin_Tree *tr = create_tree();
Code:set_root( &(tr->root), node );tr's root was changed to n's left node.Code:void set_root( Bin_Node **root, Bin_Node *n ) { *root =n->left; }
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
If I follow you, yes, that's it.
were typing at the same time here lol
so following that train of thought to set the tree root to point to two i have to know what was pointing at 1
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.
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
you beat me to it lol
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
ok to take your examples further....
Code:f1( **p) { f2(&(*p))?? f2 (**p) or (***p)
*** 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