Hello,
So, I wrote a code last week using pointer to a pointer to add a node to a BST
But now as I try to do this with just using single pointersCode: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 it doesn't seem to work.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;
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!



LinkBack URL
About LinkBacks


