BST node deletion in c - c

i'm trying to delete the nodes of a BST of strings that don't have a certain char in a certain position, but the code seg-faults and i can't trace the source of the error.
the inserting phase does the job, it's the deleting part that gives me headaches.
the delete function finds the key in the tree then deletes the node containing that key.
any help?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct BST {
char *value;
struct BST *p_left;
struct BST *p_right;
};
typedef int (*Compare)(const char *, const char *);
int CmpStr(const char *a, const char *b)
{return (strcmp (a, b));}
//function to insert words into string
void insertInTree(char* key, struct BST** leaf, Compare cmp) {
int res;
if( *leaf == NULL ) {
*leaf = (struct BST*) malloc(sizeof( struct BST ) );
(*leaf)->value = malloc( strlen (key) +1 );
strcpy ((*leaf)->value, key);
(*leaf)->p_left = NULL;
(*leaf)->p_right = NULL;
} else {
res = cmp (key, (*leaf)->value);
if( res < 0)
insertInTree(key, &(*leaf)->p_left, cmp);
else if( res > 0)
insertInTree(key, &(*leaf)->p_right, cmp);
}
}
//helper function to find the min value string in alphabetical order
char* minValueNode(struct BST* node) {
struct BST* current = node;
while (current->p_left != NULL)
current = current->p_left;
return current->value;
}
//function to delete the node in the BST
struct BST* deleteNode(struct BST* root, char *key) {
if (root == NULL)
return NULL;
int cmp_result = strcmp(key, root->value);
if (cmp_result < 0)
root->p_right= deleteNode(root->p_right, key);
else if (cmp_result>0)
root->p_left= deleteNode(root->p_left, key);
else{
if (root->p_left == NULL && root->p_right == NULL){
free(root);
return NULL;
}
else if (root->p_left==NULL) {
struct BST *temp = root->p_right;
free(root);
return temp;
} else if(root->p_right==NULL){
struct BST *temp = root->p_left;
free(root);
return temp;
}
else {
char *newMin = minValueNode(root->p_right);
strcpy(root->value, newMin);
root->p_right= deleteNode(root->p_right, newMin);
}
}
return root;
}
//function to check if there is the exactchar char in the n position in the string
void charInExactPosition(struct BST *head, char exactchar, int n)
{
if( head != NULL ) {
charInExactPosition(head->p_left, exactchar, n);
if (head->value[n]!=exactchar){
deleteNode(head, head->value);}
charInExactPosition(head->p_right, exactchar, n);
}
}
void printTree(struct BST *root)
{
if( root != NULL ) {
printTree(root->p_left);
printf("%s\n", root->value);
printTree(root->p_right);
}
}
int main(){
struct BST* tree = NULL;
insertInTree("AAAAA", &tree, (Compare) CmpStr);
insertInTree("AkdAA", &tree, (Compare) CmpStr);
insertInTree("AAfAA", &tree, (Compare) CmpStr);
insertInTree("AdgAA", &tree, (Compare) CmpStr);
insertInTree("AhAAA", &tree, (Compare) CmpStr);
insertInTree("AAAiA", &tree, (Compare) CmpStr);
insertInTree("2AAAA", &tree, (Compare) CmpStr);
charInExactPosition(tree, 'A', 1);
printTree(tree);
}

Related

function to delete node in a BST of strings

i'm having a problem in this code. its function is to go through a BST of strings and delete the nodes that satisfy some conditions (if in the word in the node there's an instance of the banned character, the node needs to be deleted). the problem is that it seg-faults in scrematurabanned2 because in the last recursive call it tries to access a node that is not existant and seg-faults. how can i fix this?
sorry in advance if there's not a lot of code in this question but this is a huge program and these are the parts that i think are responsible for the problem
struct node {
char *value;
struct node *p_left;
struct node *p_right;
};
int CmpStr(const char *a, const char *b)
{
return (strcmp (a, b));
}
void inserisciInAlbero2(char* key, struct node** leaf, Compare cmp) {
int res;
if( *leaf == NULL ) {
*leaf = (struct node*) malloc(sizeof( struct node ) );
(*leaf)->value = malloc( strlen (key) +1 );
strcpy ((*leaf)->value, key);
(*leaf)->p_left = NULL;
(*leaf)->p_right = NULL;
} else {
res = cmp (key, (*leaf)->value);
if( res < 0)
inserisciInAlbero2( key, &(*leaf)->p_left, cmp);
else if( res > 0)
inserisciInAlbero2( key, &(*leaf)->p_right, cmp);
}
}
void scrematurabanned2(struct node *head, const char *bannedchar, int n){
if( head != NULL ) {
scrematurabanned2(head->p_left, bannedchar, n);
if(strpbrk(head->value,bannedchar)!=NULL)
{
head = deleteNode(head, head->value);
}
scrematurabanned2(head->p_right, bannedchar, n);
}
}
struct node * minValueNode(struct node* node) {
struct node* current = node;
while (current->p_left != NULL)
current = current->p_left;
return current;
}
struct node* deleteNode(struct node* root, char *key) {
if (root == NULL)
return root;
int cmp_result = strcmp(key, root->value);
if (cmp_result < 0)
root->p_left= deleteNode(root->p_left, key);
else if (cmp_result>0)
root->p_right= deleteNode(root->p_right, key);
else{
if (root->p_left==NULL) {
struct node *temp = root->p_right;
free(root);
return temp;
} else if(root->p_right==NULL){
struct node *temp = root->p_left;
free(root);
return temp;
}
struct node* temp = minValueNode(root->p_right);
strcpy(root->value, temp->value);
}
return root;
}
int main() {
struct node *BST = NULL;
inserisciInAlbero2("2rj9R", &BST, (Compare) CmpStr);
inserisciInAlbero2("2rF9d", &BST, (Compare) CmpStr);
inserisciInAlbero2("2rq9R", &BST, (Compare) CmpStr);
inserisciInAlbero2("2ft9R", &BST, (Compare) CmpStr);
scrematurabanned2(BST, 'r', 5));

Searching for a string in BST fails

I am searching a string in a BST. If I found the string, I just return it. If not found, I print 3 suggestions:
the last traversed node before declaring that the node is not found
the inorder successsor
the inorder predecessor
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
void line() {
printf("\n-------------------------\n");
}
typedef struct node node;
struct node {
node* left, *right;
char *key;
};
node *newnode(char* key) {
node *n = malloc(sizeof(node));
n->key = malloc(strlen(key) + 1);
strcpy(n->key, key);
n->right = n->left = NULL;
return n;
}
node *insirtNode(node *root, char *key) {
if (!root) return newnode(key);
if (strcmp(key, root->key) < 0) {
root->left = insirtNode(root->left, key);
} else if (strcmp(key, root->key) > 0) {
root->right = insirtNode(root->right, key);
}
return root;
}
node* search(node *root, char *key, node **pred, node **succ, node **last) {
if (root) {
//printf("%s:%s=%d.\n",root->key,key,strcasecmp(key,root->key));
if (strcasecmp(key, root->key) == 0) {
printf("%s - is correct", root->key);
line();
return root;
}
if (strcasecmp(key, root->key) < 0) {
if (!root->left) {
*last = root;
}
*succ = root;
return search(root->left, key, pred, succ, last);
} else {
if (!root->right) {
*last = root;
}
*pred = root;
return search(root->right, key, pred, succ, last);
}
} else return NULL;
}
int main() {
char input[60];
printf("enter a word:\n");
gets(input);
line();
node *pred = NULL, *succ = NULL, *temp = NULL, *last = NULL;
node *root = NULL;
root = insirtNode(root, "anas");
root = insirtNode(root, "anad");
root = insirtNode(root, "anay");
root = insirtNode(root, "anaz");
temp = search(root, input, &pred, &succ, &last);
if (!temp) {
printf("%s incorrect, suggestions: %s %s %s", input, succ->key, pred->key, last->key);
}
printf("\n");
return 0;
}
The functions sometimes work when the string
exist in the BST. If not, it returns garbage without printing anything.
There are these issues:
in the main function where succ->key, pred->key and last->key are accessed when any of these pointers could still be NULL.
Not a problem, but last is likely to be equal to either succ or pred, so probably you want to avoid printing the same key twice.
Suggested correction:
if (temp == NULL) {
printf("%s incorrect, suggestion(s):", input);
if (pred != NULL) printf(" %s", pred->key);
if (succ != NULL) printf(" %s", succ->key);
if (last != NULL && last != pred && last != succ) printf(" %s", last->key);
}
The call to malloc assumes that a char is a byte. This is wrong. So multiply the number of characters by sizeof(char):
n->key = malloc((strlen(key) + 1) * sizeof(char));

Pointer set to NULL but not in debugger

So I am working o AVL tree, however I cant seem to either get the delete function working nor freeing the tree right. The delete function segfaults everytime, but the free function segfaults when in the debugger.
Here is gdb's stack trace:
#0 0x00007fffd935a87a in msvcrt!_memicmp_l () from C:\WINDOWS\System32\msvcrt.dll
#1 0x0000000000402811 in isPresentRecurs (root=0xb756d0, searchedValue=0xb795b0 "aaa", found=0x61fcec) at ../.source/binTree.c:206
#2 0x00000000004027d6 in isPresent (root=0xb756d0, searchKey=0xb795b0 "aaa") at ../.source/binTree.c:200
#3 0x0000000000401c3d in main () at test.c:110
In my tests I check if the root has been set to NULL, which running it normally does finish however running it inside the debugger does not and instead goes into the else statement:
Minimal Example (test.c):
#include "binTree.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TRACE 0
#define MAX_SEARCH_ITEMS 20
void fillTree(binTree **tree);
void fillSearchValues( char **valArray);
void fillTree(binTree** tree){
printf( "Constructing tree\n\n" );
char key[200] ="";
for(int j=1 ;j<4;j++ ){
memset(&key,0,199);
for(int i=0; i<26; i++){
for(int k = 0;k<j;k++) key[k]= i+'a';
Var value;
value.data = malloc(sizeof(varData));
value.data->iData = j;
value.type =INTEGER;
(*tree)->root= insert((*tree)->root,key,value);
if(TRACE) printf("key: %s, value: %d\n",(*tree)->root->key,(*tree)->root->value.data->iData);
}
}
(*tree)->nodeCount = getSizeBinaryTree((*tree)->root);
printf( "\n\nTree constructed\n\n" );
}
void fillSearchValues( char **valArray){
char key[200]="";
for(int j=1 ;j<4;j++ ){
memset(&key,0,199);
for(int i=0; i<26; i++){
if(i*j>MAX_SEARCH_ITEMS) break;
for(int k = 0;k<j;k++) key[k]= i+'a';
*(valArray+i*j) = strdup(key);
if (TRACE)printf ("%s read; %s inserted\n", key, valArray[i*j] );
}
}
}
int main(){
binTree *tree = createNewTree();
fillTree(&tree);
printTree(tree->root);
/* //Fails at delete
for(int i=0;i<26;i++){
char string = i+'a';
tree->root = Delete(tree->root,&string);
}*/
printf("\nFreeing Tree: \n=================================\n");
freeTree(tree->root);
if(tree->root==NULL) printf("Tree has been freed successfully\n");
else printf("Failed to free tree \n");
// searching after freeing
int found =0; int lost =0;
char *values[MAX_SEARCH_ITEMS];
fillSearchValues(values);
for(int i=0;i<MAX_SEARCH_ITEMS;i++){
if(isPresent(tree->root,values[i])){
if (TRACE)printf("found search value %s\n",values[i]);
found++;
}else{
lost++;
if(TRACE)printf("didnot find search value %s\n",values[i]);
}
}
printf("found %d of %d while cleared %d\n", found,MAX_SEARCH_ITEMS,lost);
free(tree);
return 0;
}
binTree.h:
#ifndef BINTREE_H
#define BINTREE_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define COUNT 10
typedef enum TYPE {INTEGER, FLOAT, CHARACTER} TYPE;
typedef union {
float fData;
int iData;
char cData;
} varData;
typedef struct Var{
varData * data;
TYPE type;
} Var;
typedef struct Node{
char* key;
Var value;
int height;
struct Node *left;
struct Node *right;
}Node;
typedef struct binTree{
Node *root;
unsigned int nodeCount;
}binTree;
int max(int a,int b);
binTree *createNewTree();
Node *newNode(char *key,Var value);
void freeTree(Node *node);
void freeNode(Node *node);
Node *insert(Node *node,char *key,Var value);
Node *rightRotate(Node *n);
Node *leftRotate(Node *n);
int height(Node *node);
int getBalance(Node *N);
void printTree(Node *root);
void printTreeS(Node *root,int space);
int isPresent(Node *root,char *searchKey);
void isPresentRecurs(Node *root,char *searchedValue,int *found);
Node *minValueNode(Node *node);
Node *search(Node *node,char *key);
Node *Delete(Node *root,char *key);
int getSizeBinaryTree(Node* root);
#endif
binTree.c
#include "binTree.h"
int max(int a, int b){
return (a > b)? a : b;
}
binTree* createNewTree(){
binTree *t = (binTree *) malloc(sizeof(binTree));
if(!t){
printf("Failed at allocationg tree\n");
exit(-1);
}
t->root = NULL;
return t;
}
Node* newNode(char * key,Var value){
Node *p = (Node*)malloc(sizeof(Node));
if(!p){
printf("Failed at allocationg node\n");
exit(-1);
}
p->key = strdup(key);
p->value = value;
p->left=p->right=NULL;
p->height = 1;
return p;
}
void freeTree(Node* node){
if (node==NULL) return;
freeTree(node->left);
freeTree(node->right);
freeNode(node);
node=NULL;
}
void freeNode(Node *node){
free(node->value.data);
node->value.data = NULL;
free(node->key);
node->key = NULL;
free(node);
node = NULL;
}
Node* insert(Node *node, char *key,Var value){
if (node == NULL) return newNode(key,value);
if ( strcasecmp(key ,node->key)<0) node->left = insert(node->left, key,value);
else if (strcasecmp(key ,node->key)>0) node->right = insert(node->right, key,value);
else if(strcasecmp(key,node->key)==0){
if(memcmp(&value.data,&node->value,sizeof(Var))!=0){
memcpy(&node->value,&value,sizeof(Var));
}
return node;
};
node->height = max(height(node->left),height(node->right))+1;
int balance = getBalance(node);
// Left Left Case
if (balance > 1 && strcasecmp(key, node->left->key)<0)
return rightRotate(node);
// Right Right Case
if (balance < -1 && strcasecmp(key, node->right->key)>0)
return leftRotate(node);
// Left Right Case
if (balance > 1 && strcasecmp(key, node->left->key)>0){
node->left = leftRotate(node->left);
return rightRotate(node);
}
// Right Left Case
if (balance < -1 && strcasecmp(key,node->right->key)<0){
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
Node *rightRotate(Node *n){
Node *leftNode =n->left;
if(!leftNode) return n;
Node *rightOfLeft =leftNode->right;
leftNode->right = n;
n->left = rightOfLeft;
n->height = max(height(n->left), height(n->right)) + 1;
leftNode->height = max(height(leftNode->left), height(leftNode->right)) + 1;
return leftNode;
}
Node *leftRotate(Node *n){
Node *rightNode = n->right;
if(!rightNode) return n;
Node *leftOfright = rightNode->left;
rightNode->left = n;
n->right = leftOfright;
n->height = max(height(n->left), height(n->right)) + 1;
rightNode->height = max(height(rightNode->left), height(rightNode->right)) + 1;
return rightNode;
}
int height(Node *node){
if (!node) return 0;
return node->height;
}
int getBalance(Node *N){
if (N == NULL) return 0;
return height(N->left) - height(N->right);
}
void printTree(Node *root){
printTreeS(root, 0);
}
void printTreeS( Node *root, int space){
if (root == NULL)
return;
space += COUNT;
printTreeS(root->right, space);
printf("\n");
for (int i = COUNT; i < space; i++) printf(" ");
if (root->value.type == CHARACTER)printf("type: CHAR key: %s value: %s\n", root->key, root->value.data->cData);
if (root->value.type == INTEGER)printf("type: INT key: %s value: %d\n", root->key, root->value.data->iData);
if (root->value.type == FLOAT)printf("type: FLOAT key: %s value: %f\n", root->key, root->value.data->fData);
printTreeS(root->left, space);
}
int isPresent(Node* root, char* searchKey){
int found = 0;
isPresentRecurs( root, searchKey, &found );
return found;
}
void isPresentRecurs( Node *root,char *searchedValue,int* found ){
if (root) {
if (strcasecmp(root->key,searchedValue)==0)
*found = 1;
else {
isPresentRecurs(root->left, searchedValue, found);
if (!(*found))
isPresentRecurs( root->right, searchedValue, found);
}
}
}
Node * minValueNode(Node* node){
if(!node) return NULL;
if(node->left )return minValueNode(node->left);
return node;
}
Node *search(Node *node, char *key){
if (node == NULL || strcmp(node->key, key)==0)return node;
if (strcmp(node->key, key)<0) return search(node->right, key);
return search(node->left, key);
}
int getSizeBinaryTree(Node* root){
if (root) return 1 +getSizeBinaryTree( root->left ) + getSizeBinaryTree( root->right );
else return 0;
}
Node* Delete(Node* root,char *key) {
if (root==NULL) return root;
else if (strcasecmp(key ,root->key)>0) root->left =Delete(root->left,key);
else if (strcasecmp(key ,root->key)<0) root->right = Delete(root->right,key);
else {
if(root->right==NULL && root->left==NULL) {
free(root);
root = NULL;
}
else if(root->left!=NULL && root->right==NULL) {
Node* temp = root->left;
root = root->left;
freeNode(temp);
}
else if(root->right!=NULL && root->left==NULL) {
Node* temp = root->right;
root = root->right;
freeNode(temp);
}
else {
Node* temp = minValueNode(root->right);
root->key= temp->key;
root->value = temp->value;
root->right = Delete(root->right,temp->key);
}
}
if(root==NULL) return root;
root->height = 1 + max(height(root->left),height(root->right));
int balance = getBalance(root);
//Left Left Case
if(balance > 1 && getBalance(root->left) >=0) return rightRotate(root);
// Right Right Case
if(balance < -1 && getBalance(root->right) <=0) return leftRotate(root);
// Left Right Case
if(balance > 1 && getBalance(root->left) < 0) {
root->left = leftRotate(root->left);
return rightRotate(root);
}
//Right Left Case
if(balance < -1 && getBalance(root->right) > 0) {
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
This is a mistake:
freeTree(tree->root);
if(tree->root==NULL) printf("Tree has been freed successfully\n");
else printf("Failed to free tree \n");
C uses pass-by-value, so it is not possible for freeTree to set tree->root to NULL.
The line node = NULL; inside the freeTree function sets the function parameter (which is a copy of the argument), it does not modify the argument in the calling context.
The function does free the pointed-to memory, which renders all pointers to that memory indeterminate, so the test tree->root == NULL actually causes undefined behaviour by using an indeterminate value.
Your compiler should warn about a dead-store for node=NULL; , if you do not see a warning then try turning up the warning and/or optimization level in your compiler, or running a static analyzer such as clang-tidy. freeNode has a similar issue.
To fix the problem, either change the calling code, e.g. freeTree(tree->root); tree->root = NULL;, or you will have to use pass-by-pointer, i.e. pass the address of the node you want to free.

Need help creating maxHeap function

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct listNode {
int id;
struct listNode *next;
} ListNode;
typedef struct treeNode {
char *word;
char *key;
int freq;
ListNode *head;
struct treeNode *left;
struct treeNode *right;
} TreeNode;
TreeNode *insertItem(TreeNode *root, char *gword);
void printTreeInorder(TreeNode *v);
void searchforexist(TreeNode *root, char *key);
int searchItem(TreeNode *root, char *word);
void Heap(TreeNode *arr[],TreeNode *root, int i);
void maxheap(TreeNode *arr[],int k);
void freeNodes(TreeNode *root);
#define MAX 25
int main() {
char in[MAX];
char word[MAX];
TreeNode *root = NULL;
int comp;
int f=0;
int t=0;
FILE *fp = fopen("input.txt", "r");
memset(word, 0, MAX);
if (fp != NULL) {
while (fscanf(fp, "%24s \n", word) != EOF) {
root = insertItem(root, word);//insert items
t++;
if (strcmp(word, "eof") == 0) break;
}
fclose(fp);
}
// User inputs word
printf("Give word");
printf("\n");
scanf("%s",in);
comp=strcmp(in,"#");
while(comp!=0)
{
printf("Give word");
printf("\n");
scanf("%s",in);
comp=strcmp(in,"#");
if(comp==1)
break;
f=searchItem(root,in);
printf("%d",f);
f=0;
printf("\n");
}
TreeNode *arr[t];
Heap(arr,root,t);// HEAPPPPPPPPPPPPPPPPPPPPPPPPPPPP
printTreeInorder(root);
printf("\n");
freeNodes(root);
return 0;
}
TreeNode *insertItem(TreeNode *root, char *gword) {
TreeNode *v = root;
TreeNode *pv = NULL;
while (v != NULL) {
pv = v;
int comp = strcmp(gword, v->word);
if (comp < 0) {
v = v->left;
} else if (comp > 0) {
v = v->right;
} else {
char *word = v->word;
searchforexist(root,v->word);
return root;
}
}
TreeNode *tmp = (TreeNode *)malloc(sizeof(TreeNode));
tmp->word = strdup(gword);
tmp->left = tmp->right = NULL;
tmp->freq = 1;
if (root != NULL) {
if (strcmp(gword, pv->word) < 0) {
pv->left = tmp;
} else {
pv->right = tmp;
}
} else
root = tmp;
return root;
}
void searchforexist(TreeNode *root, char *word) {
if(root == NULL) {
return;
}
int comp = strcmp(word, root->word);
if(comp == 0) {
root->freq++;
} else {
searchforexist(comp < 0 ? root->left : root->right , word);
}
}
int searchItem(TreeNode *root, char *word)
{
if(root == NULL) {
return 0;
}
int comp = strcmp(word, root->word);
if(comp == 0) {
return root->freq;
} else {
searchItem(comp < 0 ? root->left : root->right , word);
}
}
void Heap(TreeNode *arr[],TreeNode *root, int i)
{
int k=0;
while(k<i)
{
if(root==NULL){
maxheap(arr,k);
}
arr[k]=root;
k++;
if (k=i){
maxheap(arr,k);
break;
}
Heap(arr,root->left,k);
Heap(arr,root->right,k);
}
}
void maxheap(TreeNode *arr[],int k)
{
int i;
int j;
for (i = 0; i < k; i++)
{
for (j = 0; j < k; j++)
{
if(arr[i]->freq>arr[j]->freq)
{
TreeNode *tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
for (i = 0; i < k; i++)
{
printf("%s %d",arr[i]->word,arr[i]->freq);
}
}
void printTreeInorder(TreeNode *v)
{
if (v==NULL) return;
printf("(");
printTreeInorder(v->left);
printf(")");
printf(" %.4s ", v->word);
printf("(");
printTreeInorder(v->right);
printf(")");
}
void freeNodes(TreeNode *root) {
if (root == NULL) {
return;
}
freeNodes(root->left);
freeNodes(root->right);
if(root->word != NULL) free(root->word);
if(root->key != NULL) free(root->key);
free(root);
return;
}
This program reads a file and puts all strings to a binary search tree. Repeating words are not added but the frequency counter increases (searchforexist). Then the user types a word and the program displays the frequency of the word typed.
The above works fine using any input file.
However im having trouble with the next steps of the assignment:
After that the hole tree is suppose to be copied in a maxheap based on the frequency of each word. The heap must be created using array with size equal to the elements of a tree. Every cell of said array must contain a pointer that points to a node in the binary search tree so we can still access the word inside and its frequency.
The user then inputs an int number and the programm prints all words that have frequency less that the number inputed by the user.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct listNode {
int id;
struct listNode *next;
} ListNode;
typedef struct treeNode {
char *word;
char *key;
int freq;
ListNode *head;
struct treeNode *left;
struct treeNode *right;
} TreeNode;
TreeNode *insertItem(TreeNode *root, char *gword);
void printTreeInorder(TreeNode *v);
void searchforexist(TreeNode *root, char *key);
int searchItem(TreeNode *root, char *word);
void freeNodes(TreeNode *root);
#define MAX 25
int main() {
char in[MAX];
char word[MAX];
TreeNode *root = NULL;
int comp;
int f=0;
FILE *fp = fopen("input.txt", "r");
memset(word, 0, MAX);
if (fp != NULL) {
while (fscanf(fp, "%24s \n", word) != EOF) {
root = insertItem(root, word);//insert items
if (strcmp(word, "eof") == 0) break;
}
fclose(fp);
}
// User inputs word
printf("Give word");
printf("\n");
scanf("%s",in);
comp=strcmp(in,"#");
while(comp!=0)
{
printf("Give word");
printf("\n");
scanf("%s",in);
comp=strcmp(in,"#");
if(comp==1)
break;
f=searchItem(root,in);
printf("%d",f);
f=0;
printf("\n");
}
//heapcreating here
printTreeInorder(root);
printf("\n");
freeNodes(root);
return 0;
}
TreeNode *insertItem(TreeNode *root, char *gword) {
TreeNode *v = root;
TreeNode *pv = NULL;
while (v != NULL) {
pv = v;
int comp = strcmp(gword, v->word);
if (comp < 0) {
v = v->left;
} else if (comp > 0) {
v = v->right;
} else {
char *word = v->word;
searchforexist(root,v->word);
return root;
}
}
TreeNode *tmp = (TreeNode *)malloc(sizeof(TreeNode));
tmp->word = strdup(gword);
tmp->left = tmp->right = NULL;
tmp->freq = 1;
if (root != NULL) {
if (strcmp(gword, pv->word) < 0) {
pv->left = tmp;
} else {
pv->right = tmp;
}
} else
root = tmp;
return root;
}
void searchforexist(TreeNode *root, char *word) {
if(root == NULL) {
return;
}
int comp = strcmp(word, root->word);
if(comp == 0) {
root->freq++;
} else {
searchforexist(comp < 0 ? root->left : root->right , word);
}
}
int searchItem(TreeNode *root, char *word)
{
if(root == NULL) {
return 0;
}
int comp = strcmp(word, root->word);
if(comp == 0) {
return root->freq;
} else {
searchItem(comp < 0 ? root->left : root->right , word);
}
}
void printTreeInorder(TreeNode *v)
{
if (v==NULL) return;
printf("(");
printTreeInorder(v->left);
printf(")");
printf(" %.4s ", v->word);
printf("(");
printTreeInorder(v->right);
printf(")");
}
void freeNodes(TreeNode *root) {
if (root == NULL) {
return;
}
freeNodes(root->left);
freeNodes(root->right);
if(root->word != NULL) free(root->word);
if(root->key != NULL) free(root->key);
free(root);
return;
}
I and many of us make simple mistakes. Use the automated tools you have to increase your coding efficiency.
Save time, enable all compiler warnings - or use a better compiler.
warning: control reaches end of non-void function [-Wreturn-type]
searchItem() needs to return a value.
int searchItem(TreeNode *root, char *word) {
if (root == NULL) {
return 0;
}
int comp = strcmp(word, root->word);
if (comp == 0) {
return root->freq;
} else {
// searchItem(comp < 0 ? root->left : root->right, word);
return searchItem(comp < 0 ? root->left : root->right, word);
}
}
Other problems may exist.

BST build tree double pointers

I am unsure how to set a pointer to a pointer to build a tree. Like once I have traveled to a leaf and call insert, how should I insert another element calling insert with the root node or the address of the root pointer? I think the problem with this function is the name root where that should be the double pointer right?
#include "bst.h"
#include <stdio.h>
#include <stdlib.h>
//arbitrary list of temp nodes
TreeNode *new_node, *root, *tmp, *parent;
int elemArray[100], i1, i2, i0;
int main( int argc, char *argv[] ) {
//Throw error is *Argv[] is not an integer
//assuming it was an integer
int cnt = atoi( argv[1] );
printf( "number is %d\n", cnt );
//
printf("Enter %i integer values to place in tree:\n", cnt);
for ( i1 = 0; i1 < cnt; ++i1) {
scanf( "%d", &elemArray[i1] );
}
//first ouput "INput Values"
printf( " Input Values:\n " );
for ( i2 = 0; i2 < cnt; ++i2) {
printf( "%d\n", elemArray[i2] );
}
TreeNode** root = (TreeNode*)malloc(sizeof(TreeNode*));
buildTree(root, elemArray, cnt );
printf( "Preorder:\n ");
//traverse
//TreeNode* tempN = malloc(sizeof(TreeNode*));
//tempN->data= 5;
traverse( root, PREORDER);
//traverse a single node
printf( "Inorder:\n ");
printf( "Postorder:\n ");
//Build tree with each element
return 0;
}
This is the .h file
/// The definition of the tree structure
typedef struct TreeNode {
int data ; // the data stored in the node
struct TreeNode* left ; // node's left child
struct TreeNode* right ; // node's right child
} TreeNode;
/// The three supported traversals
typedef enum {
PREORDER, // parent -> left -> right
INORDER, // left -> parent -> right
POSTORDER // left -> right -> parent
} TraversalType;
and lastly the traverse function so far as it failed the first test.
void traverse( const TreeNode* root, const TraversalType type ) {
if ( type == PREORDER) {
if (root != NULL)
{
printf("%d", root->data);
traverse( root->left, PREORDER);
traverse( root-> right, PREORDER);
}
}
}
void build_tree(TreeNode** root, const int elements[], const int count) {
TreeNode* node = malloc(sizeof(TreeNode*));
node->left = node ->right = NULL;
*root = node;
for ( int i0 = 0; i0 < count; ++i0 ){
TreeNode* node = malloc(sizeof(TreeNode*));
*root = node;
node->data = elements[cnt];
insertNode( &(*root), &node );
}
}
Why is the insertNode getting the errors (multiple) and I don't know which is the pointer and which is the struct. GUYS ANY HINT WILL BE HELPFUL PLEASE?
bst.c:94:20: error: request for member 'left' in something not a structure or union
insert(root->left, new_node);
void insertNode(TreeNode** root, TreeNode* new_node) {
if (new_node-> data < &root-> data) {
if (&root-> left == NULL)
&root-> left == new_node;
else
insert(root->left, new_node);
}
if (new_node->data > &root->data) {
if(&root-> right ==NULL)
&root->right = new_node;
else
insert(&root->right, new_node);
}
}
SO for Edit 2: I do have a header file which has a build_Tree(**root, elems[], sizeofElem[]), which means i need a helper function insert. Yes is would be easier to add by input starting*.
Edit 1
#include "bst.h"
#include <stdio.h>
#include <stdlib.h>
//arbitrary list of temp nodes
TreeNode *new_node, *root, *tmp, *parent, *current;
int elemArray[5], i1, i2, i0;
/*
Insert a new node into the tree by referencing the root and using recursion
*/
TreeNode* getN(int dataElem) {
TreeNode *newNode = malloc(sizeof(*newNode));
if (newNode != NULL)
{
newNode->data = dataElem;
newNode->left = NULL;
newNode->right = NULL;
}
return newNode;
}
/** This func should just be the root of the tree in the parameter,
but I like the idea of a pointer becuase it helps to create a tempory
pointer rather than newNode
**/
TreeNode* addNodeToTree(TreeNode *root, int data) {
TreeNode *current = *root; //define the current pointer to the root always
TreeNode *parent = *root
TreeNode *newNode = getN(data);
if (*root == NULL)
{
printf("First Node");
*root = newNode;
}
else
{
while(current != NULL)
{
parent = current;
if (current->data > data)
current = current->left;
else if (current->data < data)
current = current->right;
}
if (parent->data > data)
parent->left = newNode;
else if (parent->data < data)
parent->right = newNode;
}
}
void build_Tree(TreeNode** root, const int elements[], const int count) {
*root = malloc(sizeof(TreeNode));
for ( i0 = 0; i0 < count; ++i0 ){
printf("%d\n", elements[count]);
addNodeToTree(&root, elements[count]);
}
}
int main( int argc, char *argv[] ) {
//Throw error is *Argv[] is not an integer
//assuming it was an integer
int cnt = atoi( argv[1] );
printf( "number is %d\n", cnt );
//
printf("Enter %i integer values to place in tree:\n", cnt);
for ( i1 = 0; i1 < cnt; ++i1) {
scanf( "%d", &elemArray[i1] );
}
//first ouput "INput Values"
printf( " Input Values:\n " );
for ( i2 = 0; i2 < cnt; ++i2) {
printf( "%d\n", elemArray[i2] );
printf("building tree0\n");
}
printf("building tree\n");
TreeNode** root = (TreeNode**)malloc(sizeof(TreeNode*));
TreeNode *root = NULL;
build_Tree(root, elemArray, cnt );
printf( "Preorder:\n ");
//traverse
//TreeNode* tempN = malloc(sizeof(TreeNode*));
//tempN->data= 5;
traverse( *root, PREORDER); //pass the pointer of root to traverse the tree
//traverse a single node
printf( "Inorder:\n ");
printf( "Postorder:\n ");
//Build tree with each element
return 0;
}
void traverse( const TreeNode* root, const TraversalType type ) {
if ( type == PREORDER) {
if (root != NULL)
{
printf("%d", root->data);
traverse( root->left, PREORDER);
traverse( root-> right, PREORDER);
}
}
}
/**
void insertNode(TreeNode** root, TreeNode* new_node) {
if (new_node-> data < *root-> data) {
if (*root-> left == NULL)
*root-> left == new_node;
else
insert(*root->left, new_node);
}
if (new_node->data > *root->data) {
if(*root-> right ==NULL)
*root->right = new_node;
else
insert(*root->right, new_node);
}
}
**/
//question1: what is the
Edit 2 for main and build_tree
void build_Tree(TreeNode** root, const int elements[], const int count) {
//*root = malloc(sizeof(TreeNode));
for ( i0 = 0; i0 < count; i0++ ){
//create the node
//
TreeNode *current = *root; //define the current pointer to the root always
TreeNode *parent = *root;
//dont create node
int data = elements[i0];
TreeNode *newNode = getN(data);
if (*root == NULL)
{
printf("First Node %d\n", elements[i0]);
*root = newNode;
}
else
{
printf("Next Node %d\n", elements[i0]);
while(current != NULL)
{
parent = current;
if (current->data > data)
current = current->left;
else if (current->data < data)
current = current->right;
}
if (parent->data > data)
parent->left = newNode;
else if (parent->data < data)
parent->right = newNode;
}
//return root;
}
}
TreeNode* getN(int dataElem) {
TreeNode *newNode = malloc(sizeof(*newNode));
if (newNode != NULL)
{
newNode->data = dataElem;
newNode->left = NULL;
newNode->right = NULL;
}
return newNode;
}
int main( int argc, char *argv[] ) {
//Throw error is *Argv[] is not an integer
//assuming it was an integer
int cnt = atoi( argv[1] );
printf( "number is %d\n", cnt );
//
printf("Enter %i integer values to place in tree:\n", cnt);
for ( i1 = 0; i1 < cnt; ++i1) {
scanf( "%d", &elemArray[i1] );
}
//first ouput "INput Values"
printf( " Input Values:\n " );
for ( i2 = 0; i2 < cnt; ++i2) {
printf( "%d\n", elemArray[i2] );
printf("building tree0\n");
}
printf("building tree\n");
TreeNode* root; //= malloc(sizeof(TreeNode*));
root = NULL;
build_Tree(&root, elemArray, cnt );
printf( "Preorder:\n ");
//traverse
//TreeNode* tempN = malloc(sizeof(TreeNode*));
//tempN->data= 5;
//traverse( *root, PREORDER); //pass the pointer of root to traverse the tree
//traverse a single node
printf( "Inorder:\n ");
printf( "Postorder:\n ");
//Build tree with each element
return 0;
}
Say you created a function addNodeToTree(TreeNode *root, int data), pass the root node, and data to it as argument.
Now inside this function, simply create another variable say TreeNode *current = root which will help us basically to traverse the tree and place the node at its respective position, and TreeNode *newNode = NULL(this will become the new node, which is to be inserted).
Now before moving ahead, to actually place the node, we will first check, if the root is not null, i.e. the tree is EMPTY. For that, we will test:
if (root == NULL)
{
newNode = malloc(sizeof(*newNode)); // else we can make a function for this
// thingy too. Creating a function too,
// for you to look at.
root = newNode;
}
If the tree is not EMPTY, i.e. it contains a node already, then we will traverse the tree to find the place, where to put the new node. So the else part, will be like:
else
{
parent = current = root;
// This loop will actually help us, find the `parent`,
// of the `newNode`(which is to be inserted)
while (current != NULL)
{
parent = current;
if (current->data > data)
current = current->left;
else if (current->data < data)
current = current->right;
}
// Now once, we have found, the node, under which the
// newNode will be placed, now we have to check, whether
// newNode will become a `left child/right child` of the
// parent.
newNode = getNewNode(data);
if (parent->data > data)
parent->left = newNode;
else if (parent->data < data)
parent->right = newNode;
return root;
}
TreeNode * getNewNode(int data)
{
TreeNode *newNode = malloc(sizeof(*newNode));
if (newNode != NULL)
{
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
}
return newNode;
}
Now the newNode has been inserted, and you can simply traverse in any order to see the Tree.
EDIT 1:
Here is one working example, just see if this makes sense. Else please do ask any question, that might may arise.
#include <stdio.h>
#include <stdlib.h>
typedef struct TREENODE
{
int data;
struct TREENODE *left;
struct TREENODE *right;
}TreeNode;
void display(TreeNode *node)
{
printf("*********************************\n");
printf("Address of Node: %p\n", node);
printf("Data: %d\n", node->data);
printf("Left Child: %p\n", node->left);
printf("Right Child: %p\n", node->right);
printf("*********************************\n");
}
TreeNode * getNewNode(int data)
{
TreeNode *newNode = malloc(sizeof(*newNode));
if (newNode != NULL)
{
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
}
return newNode;
}
int getIntData(char *message)
{
int value = 0;
char buffer[BUFSIZ] = {'\0'};
fputs(message, stdout);
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%d", &value);
return value;
}
TreeNode * addNodeToTree(TreeNode *root, int data)
{
TreeNode *current = root, *parent = root;
TreeNode *newNode = getNewNode(data);
if (root == NULL)
{
root = newNode;
}
else
{
while(current != NULL)
{
parent = current;
if (current->data > data)
current = current->left;
else if (current->data < data)
current = current->right;
}
if (parent->data > data)
parent->left = newNode;
else if (parent->data < data)
parent->right = newNode;
}
return root;
}
void inOrderTraversal(TreeNode *root)
{
if (root != NULL)
{
inOrderTraversal(root->left);
display(root);
inOrderTraversal(root->right);
}
}
int main(void)
{
TreeNode *root = NULL;
int data = 0;
data = getIntData("Enter Data: ");
root = addNodeToTree(root, data);
data = getIntData("Enter Data: ");
root = addNodeToTree(root, data);
data = getIntData("Enter Data: ");
root = addNodeToTree(root, data);
inOrderTraversal(root);
return EXIT_SUCCESS;
}
EDIT 2:
To make addNodeToTree(...), implement pointer to a pointer, one simply needs to change the function as:
void addNodeToTree(TreeNode **root, int data)
{
TreeNode *current = *root;
TreeNode *parent = *root;
TreeNode *newNode = getNewNode(data);
if (*root == NULL)
{
*root = newNode;
}
else
{
// same as before, just don't return anythingy, from the function.
// As the function uses pointer to a pointer, hence whatever changes
// are done, here will be reciprocated in the main function automatically
}
// no need to return anythingy
}
And the call from main will now look like:
int main(void)
{
TreeNode *root = NULL;
int data = 0;
data = getIntData("Enter Data: ");
addNodeToTree(&root, data);
// Just see the call to addNodeToTree(...), the rest is same, as before
}
EDIT 3:
In order to take elements from an array instead of adding them directly to tree, just change the main method to this form:
int main(void)
{
TreeNode *root = NULL;
int element[5] = {19, 11, 5, 28, 25};
int size = sizeof(element) / sizeof(element[0]);
int counter = 0;
for (counter = 0; counter < size; ++counter)
{
addNodeToTree(&root, element[counter]);
}
inOrderTraversal(root);
return EXIT_SUCCESS;
}

Resources