0% found this document useful (0 votes)
21 views

CSI247: Lab 7 - Tree Introduction: Folder Structure (5 Marks)

This document provides instructions for a lab assignment to implement a generic tree data structure in Java. Students are asked to: 1. Create a project folder structure with src and classes subdirectories. 2. Define a TreeNode interface with methods like size(), isRoot(), contains(), and addChild(). 3. Implement the TreeNodeImpl class to represent tree nodes, storing data, children nodes, and a parent node. This class must also implement the Comparable interface. 4. Create a Tester class with a main method to test the tree implementation by creating some sample nodes and structures.

Uploaded by

Offie Mphale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

CSI247: Lab 7 - Tree Introduction: Folder Structure (5 Marks)

This document provides instructions for a lab assignment to implement a generic tree data structure in Java. Students are asked to: 1. Create a project folder structure with src and classes subdirectories. 2. Define a TreeNode interface with methods like size(), isRoot(), contains(), and addChild(). 3. Implement the TreeNodeImpl class to represent tree nodes, storing data, children nodes, and a parent node. This class must also implement the Comparable interface. 4. Create a Tester class with a main method to test the tree implementation by creating some sample nodes and structures.

Uploaded by

Offie Mphale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CSI247: Lab 7 – Tree Introduction

At the end of this lab you should be able to:


• Implement generic class
• Compare objects
• Follow proper java project folder structure
• Compile and run from the command line.
• Pass command line arguments to the program.
• Implement of tree nodes

Folder Structure (5 marks)


Create a project structure that you will use for this lab. The main folder will be called lab7, with 2
sub-directories, src, and classes. Copy the csi247.list package from the solution of lab6 to the src
folder.

TreeNode Interface (5 marks)


In a package called tree.node, create a generic interface called TreeNode. The generic formal
parameter should be bounded to the Comparable interface. The interface should define the
following methods.
1. int size()
2. boolean isRoot()
3. TreeNode<E> contains(E data)
4. TreeNode<E> addChild(E data)

TreeNodeImpl class (20 marks)


Create a generic class in the tree.node called TreeNodeImpl that extends the TreeNode interface.
The class should also implement the Comparable interface. Add all the appropriate generic
parameters. The class should have the following properties:
1. data: This is a generic property that keeps the data held by this node. You should have the
customary getter and setter for this property.
2. children: This is an ArrayList of TreeNodeImpl objects for holding all the children of this
node. This should have the proper generic templates. This property should only have a getter
method, but no setter.
3. parent: This is a TreeNode property for keeping track of this node’s parent. . You should
have the customary getter and setter for this property.

In addition, this class should have a constructor that takes parameters for all the instance variables.
The methods from the TreeNode and Comparable should be implemented as follows:
1. size(): This will return the number of children in the node.
2. isRoot(): Check if the parent has a null value.
3. contains(): This method should go through the children in the node and check if one of the
children has the data, if so, return the reference of the child node. If no child contains the
data, return null.
4. addChild(): Create a new TreeNodeImpl object and add it to the children then return the
node.
5. compareTo(): Compare the tree nodes by the comparing the data contained in the nodes.
NOTE: An extension would also be to make sure that the children are also the same. But for
this lab, we are not going to do that.

Tester class (10 marks)


Create another package in the src directory called lab7. In that package, create a class called Tester,
that will contain the main method. Create a set of nodes that mimic the structure in the figure below.
Can you draw the memory diagram of the structure?

You might also like