Why pointer is not zeroing node of BST after remove function call? - c

Have a recursive remove function for BST that isn't zeroing a pointer to a leaf node.
bool removeNode(Node* tree, int key)
{
bool removed = false;
if (tree)
{
if (key < tree->key)
{
removeNode(tree->left, key);
}
else if (key > tree->key)
{
removeNode(tree->right, key);
}
else // this node is the key
{
if (!tree->left && !tree->right) // leaf
{
free(tree);
tree = 0;
}
else if (!tree->left)
{
*tree = *tree->right;
}
else if (!tree->right)
{
*tree = *tree->left;
}
else // this node has 2 children
{
Node* paux = tree->left;
if (paux->right)
{
while (paux->right)
{
paux = paux->right;
}
}
tree->key = paux->key;
tree->left = paux->left;
}
removed = true;
}
}
return removed;
}
I use
Node* node = (Node*)malloc(sizeof(Node));
to allocate memory. My leaf's address is correctly zeroed, but when it returns to the previous call, the address remains still the same. If the address was zeroed, why does it return to the previous value? The change should have affected the pointer... shouldn't it?
Data structures and related functions:
typedef struct Node
{
int key;
struct Node* left;
struct Node* right;
} Node;
// init a binary tree
void init(Node** tree, int key)
{
*tree = (Node*) malloc(sizeof(Node));
(*tree)->key = key;
(*tree)->left = 0;
(*tree)->right = 0;
}
// insert at binary tree
bool insert(Node** tree, int key)
{
bool inserted = false;
if (!*tree)
{
init(&*tree, key);
}
else
{
Node* node = (Node*)malloc(sizeof(Node));
node->key = key;
node->left = 0;
node->right = 0;
Node* paux = *tree;
Node* root = paux;
while (paux != 0)
{
root = paux;
if (key < paux->key)
{
paux = paux->left;
}
else
{
paux = paux->right;
}
}
paux = node;
if (key < root->key)
{
root->left = paux;
}
else
{
root->right = paux;
}
inserted = true;
}
return inserted;
}
void print(Node* tree)
{
if (tree != 0)
{
printf("%d ", tree->key);
print(tree->left);
print(tree->right);
}

The problem you encounter happens because the function is not able to modify the input pointer. The pointer itself is passed by value.
Think of a function that increments an integer. The trivial implementation will not work as the argument is passed "by value".
void inc(int x)
{
x++;
}
you can fix this sample by passing it by reference (a pointer)
void inc(int *x)
{
(*x)++;
}
so, if you want to be able to nullify the pointer from within the function pass a pointer to the pointer:
bool removeNode(Node **tree, int key)

As you want to be able to change tree inside the function you need a double pointer.
Instead of
bool removeNode(Node* tree, int key)
you need
bool removeNode(Node** tree, int key)
^^
That also means that you need to change the way you access tree inside the function.
Also see: https://stackoverflow.com/a/39436538/4386427 - it is the same problem

removeNode() has its own copy of tree, so changes to the actual value of tree itself are not visible outside removeNode(). One option is to make tree a Node **, as others have suggested. Another option is to refactor so that you have the parent's pointer available at the point where you want to free the node. For example, the parent could be passed as another parameter to removeNode(), or your code could test one level deeper while processing. Then you could do free(parent->left); parent->left=0; or some such.

Related

Assign struct pointer to another struct pointer does not work?

I implemented an iterative version of preorder tree traversal like following:
...
void push(struct TreeNode* node)
{
if(node!=NULL)
{
top = top + 1;
stack[top].node = node;
stack[top].val = node->val;
}
}
void pop(struct TreeNode* node, int* retSz)
{
if(stack[top].node!=NULL)
{
*retSz = *retSz + 1;
OutputArray = (int*)(realloc(OutputArray, (*retSz)*sizeof(int)));
OutputArray[*retSz-1] = stack[top].val;
node = stack[top].node;
top = top - 1;
}
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
...
//start
push(root);
//process
while(!isEmpty())
{
pop(root, returnSize);
push(root->right);
push(root->left);
}
return OutputArray;
}
I found in push(),
stack[top].node = node;
it do work, but in pop()
node = stack[top].node;
This line does not work,
the node still points to the same one.
But after I changed it to
node->val = stack[top].node->val;
node->left = stack[top].node->left;
node->right = stack[top].node->right;
or
*node= *(stack[top].node);
Then the program works normally.
Can anyone point out the problem ?

exc bad access C

I am making a simple BST and, in the add_to_bst() function, it is throwing an error in the first line when referencing the object's value.
CODE
typedef struct node {
int value;
struct node* leftChild;
struct node* rightChild;
} BSTNode;
BSTNode *new_BSTNode(int val) {
BSTNode *this = (BSTNode *) malloc(sizeof(BSTNode));
this->value = val;
this->leftChild = (BSTNode * ) malloc(sizeof(BSTNode));
this->rightChild = (BSTNode * ) malloc(sizeof(BSTNode));
this->leftChild = NULL;
this->rightChild = NULL;
return this;
}
typedef struct bst {
BSTNode * root;
} BST;
BST *new_BST(int root_val) {
BST *this = (BST *) malloc(sizeof(BST));
this->root = (BST * ) malloc(sizeof(BSTNode));
this->root->value = root_val;
// this->root->value = (int *) malloc(sizeof(int));
return this;
}
int node_get(BSTNode *n, int i) {
if (n == NULL) return -1;
if (i == n-> value) return 1;
if (i > n-> value) return node_get(n->rightChild, i);
else return node_get(n->leftChild, i);
}
int bst_get(BST *bst, int i) {
return node_get(bst->root, i);
}
void add_to_bst_node(int i, BSTNode *to) {
int n = to->value; // <--- ERR
printf("\nBST VAL: %d", n);
if (i > n) {
if (to->rightChild == NULL)
to->rightChild = new_BSTNode(i);
else
add_to_bst_node(i, to->rightChild);
} else {
if (to->leftChild == NULL)
to->leftChild = new_BSTNode(i);
else
add_to_bst_node(i, to->leftChild);
}
}
void add_to_bst(BST *tree, int i) {
if (tree->root != NULL) {
add_to_bst_node(i, tree->root);
} else {
tree->root = new_BSTNode(i);
}
}
int main() {
BST *bst = new_BST(10);
add_to_bst(bst, 10);
}
RUN MSG:
0x7fa64fc00690
0x7fa64fc00640
First Val: 10
Process finished with exit code 11
BUILD ERR:
BSTNode *new_BSTNode(int val) {
BSTNode *this = (BSTNode *) malloc(sizeof(BSTNode));
this -> value = val;
this -> leftChild = (BSTNode * ) malloc(sizeof(BSTNode));
this -> leftChild = (BSTNode * ) malloc(sizeof(BSTNode));
return this;
}
This leaves this->rightChild uninitialized and leaves this->leftChild pointing to uninitialized garbage. Neither of these issues is fixed in the code that calls new_BSTnode.
void add_to_bst_node(int i, BSTNode *to) {
int n = to -> value; // <------ ERROR
Not surprising, since to comes from leftChild and rightChild, both of which are broken by the logic of new_BSTNode.
Also:
BST *new_BST(int root_val) {
BST *this = (BST *) malloc(sizeof(BST));
this -> root = (BST * ) malloc(sizeof(BSTNode));
this -> root -> value = root_val;
// this -> root -> value = (int *) malloc(sizeof(int));
return this;
}
This doesn't set this->root->leftChild or this->root->rightChild either, so again, they're garbage that gets passed to add_to_bst_node as to.
The creation of the new node, and insertion into the tree seems incorrect.
A new node should not allocate space for the left and right subtrees. Since new nodes are always added to the extremities, they never have subtrees when new anyway.
BSTNode *new_BSTNode( int val )
{
BSTNode *this = ( BSTNode * ) malloc( sizeof( BSTNode ) );
if ( this != NULL )
{
this->value = val;
this->leftChild = NULL;
this->rightChild = NULL;
}
return this;
}
Using a recursive algorithm when inserting new data allows the code to "walk" the tree, finding the correct place for insertion.
void add_to_bst_node( int value, BSTNode *to )
{
if (to != NULL)
{
if (value > to->value)
{
// Add to child-right subtree
if (to->rightChild == NULL)
{
// right-tree is empty, create it
to->rightChild = new_BSTNode( value );
}
else
{
// add it somewhere on the right-side (recursively)
add_to_bst_node( value, to->rightChild );
}
}
else // if (i <= to->value)
{
// Add to child-left subtree
if (to->leftChild == NULL)
{
// left-tree is empty, create it
to->leftChild = new_BSTNode( value );
}
else
{
// add it somewhere on the right-side (recursively)
add_to_bst_node( value, to->leftChild );
}
}
}
}
A tree is just a node. Making a separate structure for a "tree" is just extra work.
typedef BSTNode BST;
So the creation of a tree, is just the creation of a node:
BST *new_BST( int value )
{
return new_BSTNode( value );
}
The branch in add_to_BST() always chooses the tree->root != NULL if it was initialised error-free. Then the add_to_BST_node() dereferences garbage, (as the other answers have pointed out); here is a graphical representation of the memory allocating functions,
And,
I recommend thinking about what the states are in ones system and drawing them out first so one doesn't fall into an invalid state. Also, if one is doing a constructor, it's a good idea to initialise the entire structure.

searching in binary tree in c when data gets larger

void *node_search(node_t *root, void *key) {
node_t** curr = &root;
int outcome;
static int comparison = 0;
while (*curr){
outcome = strcmp(key, (*curr)->name);
comparison++;
printf("%d", outcome);
if(outcome<0) {
curr = &(*curr)->left;
} else {
if(outcome == 0){
printf("%s---> ", key);
printf("%d number of comparisions\n", comparison);
comparison=0;
return (*curr)->movie;
}
curr = &(*curr)->right;
}
}
printf("%s---> ", key);
printf("%d number of comparisions but NOT FOUND\n", comparison);
comparison = 0;
return (*curr);
}
when the number of data is small it finds what i need to find pefectly
but when the same data set but larger in size gets used it prints out not found
why is this??
here's my insertion to tree
node_t *insert_node(node_t *root, node_t *new)
{
node_t** curr = &root;
while (*curr)
{
if (strcmp(new->name, (*curr)->name) < 0) {
curr = &(*curr)->left;
} else {
curr = &(*curr)->right;
}
}
*curr = new;
return root;
}
In insert_node, you should be passing in root as a node_t ** instead of a node_t *. Otherwise, if you have an empty tree the root node won't get created. Also, be sure to initialize root to NULL in your main.
Since you'll be passing in the address of your root pointer, insert_node doesn't need to return anything.
void insert_node(node_t **root, node_t *new)
{
node_t** curr = root;
while (*curr)
{
if (strcmp(new->name, (*curr)->name) < 0) {
curr = &(*curr)->left;
} else {
curr = &(*curr)->right;
}
}
*curr = new;
}
int main()
{
node_t *root = NULL;
...
insert_node(&root, node1);
insert_node(&root, node2);
...
}

Insert of a new node into a binary tree and return its head pointer

Hello i need to make a function that inserts a new node into a binary search tree and returns a pointer to the head/root of that tree.
My problem is with the returned value, i cant seem to figure out how to return the head of the tree in a recursive way as seen below.
tree_type insertNode (tree_type tree, int data) {
tree_type temp = NULL;
if(!tree)
{
temp = (tree_type)malloc(3*sizeof(tree_type));
temp->left = temp->right = NULL;
temp->data = data;
tree = temp;
return ;
}
if(data < tree->data)
{
insertNode(tree->left, data);
}
else if(data > tree->data)
{
insertNode(tree->right, data);
}
}
Firstly, the assignment tree = temp is useless, because tree is a local variable which disappears when the function returns.
Secondly, a return; in a function which is declared as returning a type other than void requires a diagnostic; it is not valid C or C++.
Instead of
tree = temp;
return;
consider returning the new tree:
return temp;
(There is no need for the variable temp; you could just use the variable tree in that case and then return tree).
The problem of how to return the root node is simple:
if(data < tree->data)
{
tree->left = insertNode(tree->left, data);
return tree;
}
and so forth. If you eliminate the variable temp and use tree in the malloc case, your function can just have a single return point which consists of return tree;.
If tree->left is null, then insertNode(tree->left, data) receives a null left argument and so receives a new node. We must capture this return value and assign it to tree->left. If tree->left is not null, then insertNode will just return tree->left and so the assignment just writes the same value back into tree->left.
Is that what you need?
if(!tree)
{
temp = (tree_type) malloc(sizeof(*temp));
temp->left = temp->right = NULL;
temp->data = data;
return temp;
}
if(data < tree->data)
{
tree->left = insertNode(tree->left, data);
return tree->left;
}
else if(data > tree->data)
{
tree->right = insertNode(tree->right, data);
return tree->right;
}

Iteratively inserting into a binary tree

I came across some threads on StackOverflow but none of them quite cleared my doubts.
So the problem is simple. I need to iteratively insert elements into a binary tree. And this is my code.
BST newNode(int x)
{
BSTNodePtr node = (BSTNodePtr) malloc(sizeof(struct TreeNode));
node->Element = x;
node->Left = NULL;
node->Right = NULL;
return node;
}
BST Insert(int x, BST T)
{
BST temp_node = T;
while( T != NULL) {
if (x < T->Element)
T = T->Left;
else if (x >= T->Element)
T = T->Right;
}
T = newNode(x);
return temp_node;
}
However, when I'm finding the height of this tree I am always getting 0. The height code is
int Height(BST T)
{
if (T == NULL)
return 0;
return 1+(max(Height(T->Left), Height(T->Right)));
}
and this works perfectly fine when I do insertion recursively (using a function with the exact same signature)
What am I missing?
Here:
BST Insert(int x, BST T)
{
BST temp_node = T;
while( T != NULL) {
if (x < T->Element)
T = T->Left;
else if (x >= T->Element)
T = T->Right;
}
T = newNode(x);
return temp_node;
}
You navigate the tree until you hit T == NULL. Then you create a node and assign the pointer to it to T. Then you return the original value of T. You don't modify your tree at all. No node in it is made to point to the newly created node. T is just a local variable.
Couldn't solve the problem that way. This code, however, seems to work.
BST Insert(int x, BST T)
{
BST temp=T;
BST node=(BST)malloc(sizeof(struct TreeNode));
node->Element=x;
node->Left=NULL;
node->Right=NULL;
if (T==NULL)
{
T=node;
return(T);
//printf("%d\n",T->Element);
}
else
{
while(1)
{
if (temp->Element>=node->Element && temp->Left==NULL)
{
temp->Left=node;
break;
}
else if (temp->Element>=node->Element && temp->Left!=NULL)
{
temp=temp->Left;
}
else if (temp->Element<node->Element && temp->Right==NULL)
{
temp->Right=node;
break;
}
else
{
temp=temp->Right;
}
}
return(T);
}
}
Here's My implementation of the aforementioned problem:
bst* newNode(int x)
{
bst* T = new bst;
T->value = x;
T->left_child = T->right_child = NULL;
return T;
}
bst* bst_insert_iter(bst* T,int val)
{
if (T == NULL)
T = newNode(val);
else
{
bst *temp_node = T;
bool flag = true;
while(flag)
{
if (val <= temp_node->value)
{
if (temp_node->left_child == NULL)
{
temp_node->left_child=newNode(val);
flag = false;
}
else
temp_node = temp_node->left_child;
}
else
{
if (temp_node->right_child == NULL)
{
temp_node->right_child=newNode(val);
flag = false;
}
else
temp_node = temp_node->right_child;
}
}
}
return T;
}
You have the bug in your insert function. As I may assume, initially your tree is empty. so the first time you insert a node, the second argument is NULL, right? Then this function always returns NULL to you as you always pass a NULL value.
template <class T>
class TreeNode{
private:
T data;
TreeNode<T>* right,*left;
public:
void setData(T d){
this->data =d;
}
T getData(){
return this->data;
}
void setRight(TreeNode<T>* r){
this->right =r;
}
TreeNode<T>* getRight(){
return this->right;
}
void setLeft(TreeNode<T>* r){
this->left =r;
}
TreeNode<T>* getLeft(){
return this->left;
}
static TreeNode<T>* newNode(T data){
TreeNode<T>* n = new TreeNode<T>();
n->setData(data);
n->setRight(NULL);
n->setLeft(NULL);
return n;
}
};
template <class T>
class BinaryTree{
private:
TreeNode<T>* root;
public:
void insert(T data){
TreeNode<T>* n = TreeNode<T>::newNode(data);
if(root==NULL)
root = n;
else{
TreeNode<T>* t = root;
while(t!=NULL){
if(n->getData() >= t->getData()){
if(t->getRight()==NULL){
t->setRight(n); //newnode attached as right child in tree
t = NULL;
}
else
t = t->getRight();
}
else{
if(t->getLeft()==NULL){
t->setLeft(n); //newnode attached as left child in tree
t=NULL;
}
else
t = t->getLeft();
}
}
}
}
void preorder(){
TreeNode<T>* t = root;
preorderUtil(t);
}
void preorderUtil(TreeNode<T>* node){
if(node==NULL)
return;
preorderUtil(node->getLeft());
cout<<node->getData()<<" ";
preorderUtil(node->getRight());
}
};
Your changes are not reflected in the tree. I followed this way to insert data iteratively and it works fine. The point is making a node inside your binarytree to point the newly created node such that it gets attached to the tree.
Here is my version , it seems to be working.
struct tree{
tree *left;
tree *right;
int key;
};
void insertBst(int k,tree *t)
{
tree *newK = new tree[sizeof(tree)];
newK->key = k;
newK->left = NULL;
newK->right = NULL;
if((t)->key == NULL)
{
t=newK;
return;
}
else{
bool found = false;
tree *root = t;
tree *parent = NULL;
while(root != NULL)
{
parent = root;
if(root->key < newK->key)
{
root=root->right;
}
else if(root->key > newK->key)
{
root=root->left;
}
else{
//Here we have duplicates!! so do nothing
root = root;
}
}
if(parent->key > newK->key) parent->left = newK;
else parent->right = newK;
}
}

Resources