SLIDE
SLIDE
SLIDE 01: THE UNIVERSITY EXAMPLE, SEARCH TREE
2. THE SOURCE PROGRAM IN C:
#include <stdio.h>
#include <string.h>
class univ
{
friend class tree;
protected:
char LAST[15], FIRST[15];
int AGE;
long SS;
public:
univ(char *last, char *first, int age, long ss)
{
strcpy(LAST,last);
strcpy(FIRST,first);
AGE=age; SS=ss;
};
univ()
{
LAST[0]='\0';
FIRST[0]='\0';
AGE=0; SS=0;
};
void ln(char *last) {strcpy(LAST, last); };
void fn(char *first) {strcpy(FIRST,first);};
void ag(int age) {AGE=age; };
void ssno(long ss) {SS=ss; };
void print()
{
printf("%s %s\nAGE: %d\nSS#: %ld\n",LAST,FIRST,AGE,SS);
};
};
class student: public univ
{
friend class tree;
private:
float GPA;
int LEVEL;
public:
student(char *last,char *first,int age,long ss,float gpa,int level):
univ(last,first,age,ss)
{
GPA=gpa; LEVEL=level;
};
student():univ() {GPA=0.0;LEVEL=0;};
void av(float gpa) {GPA=gpa;};
void lv(int level) {LEVEL=level;};
void print()
{
univ::print();
printf("GPA: %.2f LEVEL: %d\n",GPA,LEVEL);
};
};
class prof: public univ
{
friend class tree;
private:
float SALARY;
public:
prof(char *last,char *first,int age,long ss,float salary):
univ(last,first,age,ss)
{
SALARY=salary;
};
prof():() {SALARY=0.0;};
void sa(float salary) {SALARY=salary;};
void print()
{
univ::print();
printf("SALARY: $%.2f\n",SALARY);
};
};
class staff: public univ
{
friend class tree;
private:
float WAGE;
public:
staff(char *last,char *first,int age,long ss,float wage):
univ(last,first,age,ss)
{
WAGE=wage;
};
staff():() {WAGE=0.0;};
void wg(float wage) {WAGE=wage;};
void print()
{
univ::print();
printf("WAGE: $%.2f\n",WAGE);
};
};
class dean: public univ
{
friend class tree;
private:
int YEAR, COLLEGE;
public:
dean(char *last,char *first,int age,long ss,int year,int college):
univ(last,first,age,ss)
{
YEAR=year;
COLLEGE=college;
};
dean():() {YEAR=0; COLLEGE=0;};
void yr(float year) {YEAR=year;};
void co(float college) {COLLEGE=college;};
void print()
{
univ::print();
printf("COLLEGE: %d\nYEAR: %d\n",COLLEGE,YEAR);
};
};
class node
{
friend class tree;
private:
node *left, *right;
univ *data;
};
class tree
{
private:
node *root;
node *left;
node *right;
public:
tree() { root=left=right=0; };
void insert_person(univ* a)
{
node* parent, *current;
int found=0;
parent=0;
current=root;
while (current && !found)
{
if (strcmp(current->data->LAST,a->LAST)==0)
found=1;
else
{
parent=current;
if (strcmp(a->LAST,current->data->LAST)<0)
current=current->left;
else
current=current->right;
}
}
if (!found)
{
if (!parent)
{
//first node in search_table
root=new node;
root->left=root->right=0;
root->data=new univ;
root->data=a;
}
else
{
if (strcmp(a->LAST,parent->data->LAST)<0)
{
//add new node to the left
node* new_node=new node;
new_node->data=a;
new_node->left=new_node->right=0;
parent->left=new_node;
}
else
{
//add new node to the right
node* new_node=new node;
new_node->data=a;
new_node->left=new_node->right=0;
parent->right=new_node;
}
}
}
};
void display(node* n=0, int first=1)
{
node* current;
if (first)
{
current=root;
first=0;
}
else
current=n;
if (current!=0)
{
display(current->left,first);
current->data->print();
display(current->right,first);
}
};
};
main()
{
tree T;
student S("Marshall","Law",27,111111111,1.36,4);
staff F("Work","Do",26,222222222,8.24);
prof P("Steele","Hard",66,999999999,44444);
dean D("Nose","Hard",78,777777777,1,2);
T.insert_person(&F);
T.insert_person(&P);
T.insert_person(&S);
T.insert_person(&D);
T.display();
}