Reputation: 55
I'm new to C programming and this is my first time working on a complicated program. The program will put a phonebook (name and number) in a binary search tree. The question that arises is why I get an error with the Recursion and all pointers that I used in the program. My struct def is also probably wrong.. Would be happy if anyone could point me to the problem and how to solve it.
typedef struct _bstree {
char * name[60];
unsigned long phone;
struct bstree *left;
struct bstree *right;
}bstree;
typedef struct _bst_node {
int value;
struct node* next;
}bst_node;
and here are the functions (I'm not allowed to change the type of the functions or their arguments):
void bst_insert_node(bstree* bst, unsigned long phone, char *name) {
if (bst == NULL) {
bstree *newNode = (bstree *)malloc(sizeof(bstree));
newNode->phone = phone;
strcpy(newNode->name,name);
newNode->left = NULL;
newNode->right = NULL;
}
else if (bst->phone>phone) {
bst_insert_node(bst->left,phone, name); //ERROR
}
else {
bst_insert_node(bst->right, phone, name); //ERROR
}
}
bst_node* find_node(bstree* bst, unsigned long phone) {
if (bst==NULL) {
printf("Cannot find Number");
return NULL;
}
//if phone ist root
if (phone== bst->phone)
return bst; //ERROR
bstree* searching = NULL;
//left search
searching = find_node(bst->left,phone); //ERROR
if (searching)
return searching; //ERROR
// right search
searching = find_node(bst->right,phone); //ERROR
if (searching)
return searching; //ERROR
if (searching)
return searching; //ERROR
return NULL;
}
Upvotes: 1
Views: 1153
Reputation: 838
try this code buddy
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _bst_node
{
char name[60];
unsigned long phone;
} bst_node;
typedef struct _bstree
{
bst_node key;
struct _bstree * left;
struct _bstree * right;
} bstree;
static inline bstree * create_node(unsigned long phone, char * name)
{
bstree * newNode = (bstree *) malloc(sizeof(bstree));
newNode->key.phone = phone;
strcpy(newNode->key.name, name);
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void bst_insert_node(bstree * bst, unsigned long phone, char * name)
{
if (bst == NULL)
{
return;
}
if (bst->key.phone > phone)
{
if (bst->left == NULL)
{
bst->left = create_node(phone, name);
return;
}
bst_insert_node(bst->left, phone, name);
}
else
{
if (bst->right == NULL)
{
bst->right = create_node(phone, name);
return;
}
bst_insert_node(bst->right, phone, name);
}
}
bst_node * find_node(bstree* bst, unsigned long phone)
{
if (bst == NULL)
{
return NULL;
}
if(bst->key.phone > phone)
{
return find_node(bst->left, phone);
}
else if (bst->key.phone < phone)
{
return find_node(bst->right, phone);
}
return &(bst->key);
}
void print_tree(bstree * bst, int level)
{
int temp = 0;
while(temp < level)
{
printf("-");
++temp;
}
printf(" (%ld-%s)\n", bst->key.phone, bst->key.name);
if (bst->left != NULL)
{
print_tree(bst->left, level + 1);
}
if (bst->right != NULL)
{
print_tree(bst->right, level + 1);
}
}
Upvotes: 0
Reputation: 7542
typedef struct _bstree {
char * name[60];
unsigned long phone;
struct bstree *left;
struct bstree *right;
}bstree;
Why does your tree structure have left
and right
.The nodes of the tree should have left and right and not the tree itself.The tree structure should just have a root
node.
typedef struct _bst_node {
char name[60];
unsigned long phone;
struct _bst_node *left;
struct _bst_node *right;
}bst_node;
and then the tree structure
typedef struct _bstree {
bst_node *root; //this will point to the root node of the tree and will be NULL if the tree is emty.
}bstree;
Your insert()
function should take bstree
as input and insert a new bst_node
in the tree.Remeber your root is bsttree::root
and not bsttree
itself.
void bst_insert_node(bstree* bst, unsigned long phone, char *name)
{
//insert new node
}
Upvotes: 2