Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary Search Trees

Status
Not open for further replies.

paulsowden

Programmer
Nov 4, 2002
10
US
How do you create a binary search tree with more than one data element in it? I can create a tree with single data items (ints or chars) but soon as i come to add more variables it breaks. Any suggestions?

Paul
 
Please post some code and whatever error messages you're getting. It's hard to give specific advice with just a general description of the problem.

 
If it's an ordered tree, I'm going to guess you're trying to compare structs with each other. If so, think about why that won't work.
 
here is the code i've written

#include "tree.h"

BTREE new_node(void)
{
return(malloc(sizeof(NODE)));
}

BTREE init_node(DATA d1, BTREE p1, BTREE p2)
{
BTREE t;

t = new_node();
t -> d = d1;
t -> left = p1;
t -> right = p2;

return t;
}

BTREE create_tree(DATA a[], int i, int size)
{
if(i >=size)
{
return NULL;
}
else
{
return(init_node(a, create_tree(a, 2 * i + 1, size),
create_tree(a, 2 * i + 2, size)));
}
}

main(){}

//////////////////////////////////////////////////////

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

typedef char DATA;

struct node{
DATA d;
struct node *left;
struct node *right;
};

typedef struct node NODE;
typedef NODE *BTREE;

I'm completely new to C and i want to add string variables to the node struture but i cant seem to make anything work.
Any suggestions?

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top