So, I need to make a binary tree, except each node needs to hold multiple data types...
My instructor told me to use structures, but I am still lost as to how to put all this data into the tree.

struct record{
        long int student_id; char lastname[12]; char firstname[12]; 
        char home_address[40]; long int tele_num;
        };
        

int main(int argc, char *argv[]){
 
 
 ifstream infile("lab7_input.txt");
 ofstream outfile("lab7_output.txt");
 

 BST<long int> myTree;
 
record students[10];

for (int i = 0; i <= 10; i++ ){
 infile>>students[i].student_id>>students[i].lastname>>students[i].firstname>>students[i].home_address>>students[i].tele_num; 
 myTree.insert(students[i].student_id); 
}
 
 long int* a=myTree.search(students[1].student_id);
 cout << *a; 
...
}

Any ideas on how to do this? I omitted the classes and their functions but I think you get the idea.

Recommended Answers

All 4 Replies

I'm familiar with all of this stuff... I've just never done it with more than one item of data per node... trust me I've googled and looked around forums and no one seems to know how to do this. Of course there is a way to do it, I just don't know it.

Thanks for the help but while that tutorial is great, I looked through it and it doesn't say anything about multiple pieces of data.

Anyone else have an idea?

If you are trying to write code for a tree then I assume you are familiar with writing code for a list. You can think of a list as a tree that has only 1 child per node and they all rights or lefts or nexts or whatever else you want to call them.

Nodes that make up lists and trees can be thought of having a data member and one (for singly linked lists) or more (2 for doubly linked lists and binary trees but it could be 3 or more for other trees) ponters to type. For a binary tree the nodes typically are declared something like this:

class Node
{
    data member
     Node * right;
     Node * left;
};

data member can be any valid type. It could be a plain old data type like an int or a double or a string or an array or a pointer to some valid type, etc. Or, it could be a user defined type like a struct or a class. So say you have a declared a struct that looks something like this:

struct People
{
   string name;
   int age;
   double bodyTemperature;
   string * siblingNames;
   //etc
};

You could then create a BinaryFamilyTree containing multiple People sorted on their age using a code that looked like this:

struct Node:
{
    People p;
    Node * right;
    Node * left;
};

class BinaryFamilyTree
{
   People * root;
   //etc
};

In your case the user defined type (class or struct) that you create can be of your own making and include whatever data types you want within the user defined type. You can insert as many individual nodes into the tree using whatever criteria you wish, as long as you are consistent throughout the code. Good luck

Thanks a lot! I'll give it a shot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.