CMP2006 LabWorksheet 9 Trees
CMP2006 LabWorksheet 9 Trees
Table of Contents
Scenario........................................................................................................................................... 1
C++ Implementation Code.............................................................................................................. 2
//Data Class – Student.h .............................................................................................................. 2
//Node.h Class ............................................................................................................................. 3
//BSTree.h Class ......................................................................................................................... 4
//Main or Driver.cpp ................................................................................................................... 5
Java Implementation Code .............................................................................................................. 6
//Data Class – Student.java ......................................................................................................... 6
//Node.java Class ........................................................................................................................ 7
//BSTree.java Class ..................................................................................................................... 8
// Driver.java ............................................................................................................................. 10
Exercises ....................................................................................................................................... 11
Scenario
Your lab tutor has asked you to simulate a program that would manage students in a non-linear
data structure. Each student has an id, first name, last name and faculty.
Page 1 of 11
Updated January 2019 – Philip Smith
C++ Implementation Code
#include <iostream>
#include <string>
class Student
{
private:
//Attributes
int StudentId;
string FirstName;
string LastName;
string Faculty;
public:
//Default Constructor
Student()
{
StudentId = 0;
FirstName = "";
LastName = "";
Faculty = "";
}
//Primary Constructor
Student(int s, string fn, string ln, string f)
{
StudentId = s;
FirstName = fn;
LastName = ln;
Faculty = f;
}
//Copy Constructor
Student(Student &s)
{
StudentId = s.GetStudentId();
FirstName = s.GetFirstName();
LastName = s.GetLastName();
Faculty = s.GetFaculty();
}
//Setters
void SetStudentId(int val)
{
StudentId = val;
}
void SetFirstName(string val)
{
FirstName = val;
}
void SetLastName(string val)
{
LastName = val;
}
Page 2 of 11
Updated January 2019 – Philip Smith
void SetFaculty(string val)
{
Faculty = val;
}
//Getters
int GetStudentId()
{
return StudentId;
}
string GetFirstName()
{
return FirstName;
}
string GetLastName()
{
return LastName;
}
string GetFaculty()
{
return Faculty;
}
//Utility
void Display()
{
cout << "Student Id:\t" << StudentId << endl;
cout << "First Name:\t" << FirstName << endl;
cout << "Last Name:\t" << LastName << endl;
cout << "Faculty:\t" << Faculty << endl;
}
};
#endif
//Node.h Class
#include "Student.h"
class Node{
private:
Student Data;
Node * LeftSubTree;
Node * RightSubTree;
public:
Node() : Data()
{
LeftSubTree = NULL;
RightSubTree = NULL;
}
Node(int sid, string fn, string ln, string f) : Data(sid, fn, ln, f)
{
LeftSubTree = NULL;
RightSubTree = NULL;
}
Page 3 of 11
Updated January 2019 – Philip Smith
Node(Student s) : Data(s)
{
LeftSubTree = NULL;
RightSubTree = NULL;
}
Student GetData()
{
return Data;
}
void SetData(Student s)
{
Data = s;
}
Node * GetLeftSubTree()
{
return LeftSubTree;
}
Node * GetRightSubTree()
{
return RightSubTree;
}
};
//BSTree.h Class
#include "Node.h"
class Tree
{
private:
Node *Root;
public:
Tree()
{
Root = NULL;
}
Node * GetRoot()
{
return Root;
}
void InsertNode(Student s)
{
Node * temp = new Node(s);
if (temp != NULL)
{
Page 4 of 11
Updated January 2019 – Philip Smith
if (Root == NULL)
{
Root = temp;
}
else
{
Node * tempRoot = Root;
while (true)
{
if (temp->GetData().GetStudentId() < tempRoot-
>GetData().GetStudentId())
{
if (tempRoot->GetLeftSubTree() == NULL)
{
tempRoot->SetLeftSubTree(temp);
break;
}
else
{
tempRoot = tempRoot->GetLeftSubTree();
}
}
else
{
if (tempRoot->GetRightSubTree() == NULL)
{
tempRoot->SetRightSubTree(temp);
break;
}
else
{
tempRoot = tempRoot->GetRightSubTree();
}
}
}
}
}
else
{
cerr << "Tree is full. Cannot add another node!" << endl;
}
}
//Main or Driver.cpp
#include "Tree.h"
int main()
{
Tree * MyTree = new Tree();
Page 5 of 11
Updated January 2019 – Philip Smith
MyTree->InsertNode(Student(1234, "John", "Brown", "FENC"));
MyTree->InsertNode(Student(43, "Jamie", "Simms", "FOLW"));
MyTree->InsertNode(Student(1, "Susan", "Blake", "FOSS"));
MyTree->InsertNode(Student(7000, "Sam", "Mason", "FENC"));
MyTree->PreOrderTraversal(MyTree->GetRoot());
system("pause");
return 0;
}
//Default Constructor
public Student()
{
StudentId = 0;
FirstName = "";
LastName = "";
Faculty = "";
}
//Primary Constructor
public Student(int s, string fn, string ln, string f)
{
StudentId = s;
FirstName = fn;
LastName = ln;
Faculty = f;
}
//Copy Constructor
public Student(Student s)
{
StudentId = s.GetStudentId();
FirstName = s.GetFirstName();
LastName = s.GetLastName();
Faculty = s.GetFaculty();
}
//Setters
public void SetStudentId(int val)
{
StudentId = val;
}
public void SetFirstName(string val)
{
FirstName = val;
}
Page 6 of 11
Updated January 2019 – Philip Smith
public void SetLastName(string val)
{
LastName = val;
}
//Getters
public int GetStudentId()
{
return StudentId;
}
public string GetFirstName()
{
return FirstName;
}
public string GetLastName()
{
return LastName;
}
public string GetFaculty()
{
return Faculty;
}
//Utility
public void Display()
{
System.out.println("Student Id:\t" + StudentId);
System.out.println("First Name:\t" + FirstName);
System.out.println("Last Name:\t" + LastName);
System.out.println("Faculty:\t" + Faculty);
}
//Node.java Class
public class Node{
public Node()
{
Data = new Student();
LeftSubTree = null;
RightSubTree = null;
}
Page 7 of 11
Updated January 2019 – Philip Smith
public Node(Student s)
{
Data = new Student(s);
LeftSubTree = null;
RightSubTree = null;
}
//BSTree.java Class
public class Tree
{
private Node Root;
public Tree()
{
Root = null;
}
Page 8 of 11
Updated January 2019 – Philip Smith
public void InsertNode(Student s)
{
Node temp = new Node(s);
if (temp != null)
{
if (Root == null)
{
Root = temp;
}
else
{
Node tempRoot = Root;
while (true)
{
if (temp.GetData().GetStudentId() <
tempRoot.GetData().GetStudentId())
{
if (tempRoot.GetLeftSubTree() == null)
{
tempRoot.SetLeftSubTree(temp);
break;
}
else
{
tempRoot = tempRoot.GetLeftSubTree();
}
}
else
{
if (tempRoot.GetRightSubTree() == null)
{
tempRoot.SetRightSubTree(temp);
break;
}
else
{
tempRoot = tempRoot.GetRightSubTree();
}
}
}
}
}
else
{
System.err.println(“Tree is full. Cannot add another node!”);
}
}
Page 9 of 11
Updated January 2019 – Philip Smith
// Driver.java
public class Main{
public static void main(String []args)
{
Tree MyTree = new Tree();
MyTree.InsertNode(new Student(1234, "John", "Brown", "FENC"));
MyTree.InsertNode(new Student(43, "Jamie", "Simms", "FOLW"));
MyTree.InsertNode(new Student(1, "Susan", "Blake", "FOSS"));
MyTree.InsertNode(new Student(7000, "Sam", "Mason", "FENC"));
MyTree.PreOrderTraversal(MyTree.GetRoot());
}
Page 10 of 11
Updated January 2019 – Philip Smith
Exercise
1. A DNA Analysis Company has asked you to create an application that will visualize a
family tree. A Family tree is made up of people who are related. Each person should have
an Id and a Name. Your program should have a menu that will prompt users with the
following options:
a. AddPerson – Adds a new person to the Tree
b. CheckIfEmpty – Check if the list is empty
c. CheckIfFull – Check if the list is full
d. PreorderTraversal – Displays the family members using preorder
e. InorderTraversal – Displays the family members in order
f. CountPeopleInFamily – Displays the number of people in list
Page 11 of 11
Updated January 2019 – Philip Smith