# errors in delete function of binary search tree

#include #include typedef struct treenode { int data; struct treenode *left; struct treenode *right; }tnode; tnode * insert(tnode *node,int data) { if(node==NULL) { node=(tnode *)malloc(sizeof(tnode)); node->data=data; node->left=node->right=NULL; return node; } if(data>node->data) { node->right=insert(node->right,data); } else if(datadata) { node->left=insert(node->left,data); } return node; } tnode * find(tnode *node,int data) { if(node==NULL) return NULL; if(node->dataright,data); else if(node->data>data) return find(node->left,data); else return node; } tnode* maximal(tnode *node) { if(node==NULL) { return NULL; } if(node->right) return maximal(node->right); else return node; } tnode* minimal(tnode *node) { if(node==NULL) { return NULL; } if(node->left) return minimal(node->left); else return node; } tnode * delete(tnode *node,int data) { tnode *temp; if(node==NULL) { printf("Element Not Found"); } else if(data < node->data) { node->left = delete(node->left, data); } else if(data > node->data) { node->right = delete(node->right, data); } else { /* Now We can delete this node and replace with either minimum element in the right sub tree or maximum element in the left subtree */ if(node->right && node->left) { /* Here we will replace with minimum element in the right sub tree */ temp = minimal(node->right); node -> data = temp->data; /* As we replaced it with some other node, we have to delete that node */ node -> right = delete(node->right,temp->data); } else { /* If there is only one or zero children then we can directly remove it from the tree and connect its parent to its child */ temp = node; if(node->left == NULL) node = node->right; else if(node->right == NULL) node = node->left; free(temp); /* temp is longer required */ } } return node; } int main() { tnode *root=NULL,*temp; //int maximum,minimum; int op,n; do { printf("\n1.insertion"); printf("\n2.deletion"); printf("\n3.preorder"); printf("\n4.inorder"); printf("\n5.postorder"); printf("\n6.minimal node"); printf("\n7.maximum node"); printf("\n8.find node"); printf("\n9.quit"); printf("\nenter your option:"); scanf("%d",&op); switch(op) { case 1: printf("enter the element to be inserted:"); scanf("%d",&n); root=insert(root,n); break; case 2: printf("enter the element to be deleted:"); scanf("%d",&n); root=delete(root,n); break; case 6: temp=minimal(root); if(temp==NULL) printf("no elements in the tree"); else printf("the minimum element found is:%d",temp->data); break; case 7: temp=maximal(root); if(temp==NULL) printf("no elements in the tree"); else printf("the maximal element found is:%d",temp->data); break; case 8: printf("enter the element to be found:"); scanf("%d",&n); temp=find(root,n); if(temp == NULL) printf("element not found"); else printf("element found"); break; default : exit(0); } }while(op<9); return 0; } **when I compile this lot of errors are hitting me back and all the errors are related to delete function and how to free the pointers(two errors were related to freeing the pointers). I used gcc on widows platform to execute the program. ** the errors are as follows: g++.exe -x c++ -c C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C -o C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.o -Wall -fpermissive C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:97: parse error before `delete' C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:120: syntax error before `->' C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:122: syntax error before `->' C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:133: `temp' was not declared in this scope C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:133: warning: ANSI C++ forbids declaration `free' with no type C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:133: `int free' redeclared as different kind of symbol .\..\lib\gcc-lib\i386-mingw32\2.95.2\..\..\..\..\include\stdlib.h:271: previous declaration of `void free(void *)' C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:134: parse error before `}' C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C: In function `int main()': C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:174: warning: left-hand operand of comma expression has no effect C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:174: type `int' argument given to `delete', expected pointer Failure