I want to write a function to add a node to a tree which takes as parameters the root, the value of the node, the value of the node below which I want to add the new node and a number according to which I will add the node to be left or right child (even = right child, odd = left child). I don't understand where I wrong , its the same concept of BST just other condition. I would appreciate any help.
for example :
6
/ \
3 20
Tnode* insert (Tnode* root , int data , int father , int leftOrRight) ; insert (root , 50 , 6 , 2) the tree will be :
6
/ \
3 50
\
20
Tnode* insert(Tnode* root, int data , int father , int leftOrRight)
{
/* If the tree is empty, return a new node */
if (root == NULL)
return newNode(data);
if (root->data == father)
if (leftOrRight % 2 == 0)
root->right = insert(root->right , data , father , leftOrRight);
else
root->left = insert(root->left , data , father , leftOrRight) ;
return root ;
}
Tnode* newNode(int data)
{
Tnode* node = (Tnode*)malloc(sizeof(Tnode));
node->data = data;
node->left = node->right = NULL;
return node;
}