0% found this document useful (0 votes)
4 views11 pages

CMP2006 LabWorksheet 9 Trees

The document is a lab worksheet for a Data Structures course, focusing on the implementation of binary search trees (BST) in C++ and Java. It includes code for a Student class, Node class, and Tree class, along with a main driver program to demonstrate tree operations. Additionally, it presents an exercise to create an application for visualizing a family tree with various functionalities.

Uploaded by

divinephoenix65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

CMP2006 LabWorksheet 9 Trees

The document is a lab worksheet for a Data Structures course, focusing on the implementation of binary search trees (BST) in C++ and Java. It includes code for a Student class, Node class, and Tree class, along with a main driver program to demonstrate tree operations. Additionally, it presents an exercise to create an application for visualizing a family tree with various functionalities.

Uploaded by

divinephoenix65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIVERSITY OF TECHNOLOGY JAMAICA

FACULTY OF COMPUTING AND ENGINEERING


SCHOOL OF COMPUTING AND INFORMATION TECHNOLOGY

Data Structures (CMP2006)

Lab Worksheet #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

//Data Class – Student.h


#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>
#include <string>

using namespace std;

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;
}

void SetLeftSubTree(Node *obj)


{
LeftSubTree = obj;
}

Node * GetRightSubTree()
{
return RightSubTree;
}

void SetRightSubTree(Node *obj)


{
RightSubTree = obj;
}

};

//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;
}
}

void PreOrderTraversal(Node *MyRoot)


{
if (MyRoot != NULL)
{
cout << "[ID: " << MyRoot->GetData().GetStudentId() << ", " << MyRoot-
>GetData().GetLastName() << "] -> ";
PreOrderTraversal(MyRoot->GetLeftSubTree());
PreOrderTraversal(MyRoot->GetRightSubTree());
}
}
};

//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;
}

Java Implementation Code

//Data Class – Student.java

public class Student


{
private int StudentId;
private string FirstName;
private string LastName;
private string Faculty;

//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;
}

public void SetFaculty(string val)


{
Faculty = 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{

private Student Data;


private Node LeftSubTree;
private Node RightSubTree;

public Node()
{
Data = new Student();
LeftSubTree = null;
RightSubTree = null;
}

public Node(int sid, string fn, string ln, string f)


{
Data = new Student(sid, fn, ln, f);
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;
}

public Student GetData()


{
return Data;
}

public void SetData(Student s)


{
Data = s;
}

public Node GetLeftSubTree()


{
return LeftSubTree;
}

public void SetLeftSubTree(Node obj)


{
LeftSubTree = obj;
}

public Node GetRightSubTree()


{
return RightSubTree;
}

public void SetRightSubTree(Node obj)


{
RightSubTree = obj;
}

//BSTree.java Class
public class Tree
{
private Node Root;

public Tree()
{
Root = null;
}

public Node GetRoot()


{
return Root;
}

public void SetRoot(Node r)


{
Root = r;
}

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!”);
}
}

public void PreOrderTraversal(Node MyRoot)


{
if (MyRoot != null)
{
System.err.println(“[ID: “ + MyRoot.GetData().GetStudentId() + “, “ +
MyRoot.GetData().GetLastName() + “] -> “;
PreOrderTraversal(MyRoot.GetLeftSubTree());
PreOrderTraversal(MyRoot.GetRightSubTree());
}
}
};

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

You might also like