Please consider the following C code in VC++2010 for creating BST in C language. By creating win32 console application in VC++ project.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
typedef struct BSTNode
{
char *data;
struct BSTNode *left,*right;
}Node;
Node *createNode(char *str)
{
int strLength=strlen(str);
char *data=(char *)malloc(strLength+1);
strcpy(data,str);
data[strLength+1]='\0';
Node *temp=(Node *)malloc(sizeof(Node));
temp->left=0;
temp->right=0;
temp->data=data;
return temp;
}
int main()
{
Node *root=createNode("Ravinder");
printf("%s\n",root->data);
return 0;
}
It give errors in VC++2010:
warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c 17
warning C4047: 'return' : 'Node *' differs in levels of indirection from 'int' c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c 25
error C2275: 'Node' : illegal use of this type as an expression c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c 20
error C2223: left of '->right' must point to struct/union c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c 22
error C2223: left of '->left' must point to struct/union c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c 21
error C2223: left of '->data' must point to struct/union c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c 23
error C2065: 'temp' : undeclared identifier c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c 20
error C2065: 'temp' : undeclared identifier c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c 21
error C2065: 'temp' : undeclared identifier c:\users\radha krishna\documents\visual studio 2010\projects\binarysearchtree\binarysearchtree\main.c 22
But if i replace above function createnode by this it work fine:
Node *createNode(char *str)
{
Node *temp=(Node *)malloc(sizeof(Node));
temp->left=0;
temp->right=0;
temp->data=str;
return temp;
}