I'm writing simple program that's simulate Binary Search Tree. I wrote functions Insert
and Show, but don't know how to implement function Delete (deletion by copying from prelude's tutorial). If you could wrote this function using this structure and using free function according to this declaration.I need to find appropriate node by comparing data first then to frr that node by keeping tree structure. If you could help me I would appreciate
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node* left;
struct Node* right;
}node;
void Insert(node**,int);
void Show(node*);
void Delete(int);
int main()
{
node *root=NULL;
Insert(&root,2);
Insert(&root,5);
Insert(&root,1);
Insert(&root,0);
Insert(&root,10);
Show(root);
}
void Insert(node** root,int data)
{
if(!*root)
{
*root=malloc(sizeof(node));
(*root)->data=data;
(*root)->left=NULL;
(*root)->right=NULL;
return;
}
else
{
if((*root)->data>data)
Insert(&(*root)->left,data);
else
Insert(&(*root)->right,data);
}
}
void Show(node* root)
{
if(root)
{
Show(root->left);
printf("%d ",root->data);
Show(root->right);
}
}
void Delete (int data)
{
//TODO
}
Thanks