I am working on a Binary search tree.
So,here is the structure used for representing the node:
typedef struct TreeNode
{
int num;
struct TreeNode *left,*right;
}TREENODE;
To insert a node in the tree I have the following method signatire
void InsertNode(TREENODE **root,int data);
In the above method why do we require double pointer.We can use a single pointer!
Are we using double pointer to avoid duplication?
No, this is needed in case of rebalancing. After rebalancing root could be changed.
Ok, I will expand. Double pointer allows you to modify the pointer. So what is tree root in your case? Pointer to TREENODE
. Some operations like search will never modify it. But some operations might need to change it, so that another node becomes new root. So they have to have access to that variable that you use as root. One example why they might need it is rebalancing, see AVL trees.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With