this code is a binary search tree I'm doing for my school assignment.
#include "binary_tree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 10
Node* create_tree() {
return NULL;
}
void insert_tree(Node** root, char* nname, char* number) {
if (*root==NULL){
Node* node = (Node *)malloc(sizeof(Node));
strncpy(node->name , nname, MAXLEN);
strncpy(node->number , number, MAXLEN);
node->left = node->right = NULL;
*root = node;
return;
}
if (strncmp(*root->name, nname, MAXLEN)) {
insert_tree(&(*root)->left, nname, number);
} else {
insert_tree(&(*root)->right, nname, number);
}
}
void preorder(Node* node, int level){
int i;
if (node==NULL){
//printf("null\n");
return;
}
for (i = 0; i<level; i++) {
printf(" ");
}
printf("%s %d\n", node->name, level);
preorder(node->left, level + 1);
preorder(node->right, level + 1);
}
This is my Full code. Well I deleted not necessary bits of codes, but thats all I need right know..
typedef struct t_node {
char name[MAXLEN + 1];
char number[MAXLEN + 1];
struct t_node *left;
struct t_node *right;
} Node;
and this is the Node type I'm using.
at the insert_tree, inserting one node is OK.
but when I use strncmp, it get errors.
"Request for member in something not a structure or union"
isn't root a Node Struct?
I don't get why I get errors.
See operator precedence, where ->
is having precedence over *
. You should use (*root)->name
as argument to your strncmp()
call.