Hello,
I am currently attempting to complete a project for my computer science class and we have to implement a ternary tree as a sort of database of different cat objects. I know that my method to add cats to the tree is correct but I can't figure out how to do the delete method.
This is a clearer description of the project from the directions:
The database of all cats is represented by a ternary tree CatTree. This tree has a single field root which contains a reference to the root node. The nodes are defined by a private nested class CatNode. Each CatNode is associated with data stored in a CatInfo object (code provided). This CatInfo class stores data into the following fields:
• String name: the name of the cat.
• int monthHired: the time at which the cat was hired.
• int furThickness: the average thickness of the cat’s fur in millimeters
• int nextGroomingAppointment: the month in which we expect the next grooming appointment
The CatTree class contains a CatNode inner class, and a field root, which contains the root node of the tree. It also implements Iterable and includes a CatTreeIterator inner class.
The CatNode inner class contains the information about the tree structure:
• CatInfo data: the CatInfo object with the data for this cat.
• CatNode senior: this cat and all the children of its node have more seniority than the current node’s cat.
• CatNode junior: this cat and all the children of its node have less seniority than the current node’s cat.
• CatNode same: this cat and all the children of its node have exactly the same seniority than the current node’s cat. In addition, in the structure of the tree, CatNodes with equal seniority should be sorted in decreasing order of fur thickness.
Below is my code for adding:
public CatNode addCat(CatNode c) {
CatNode currentNode = this;
CatNode newNode = new CatNode(c.data);
if(currentNode != null) {
if(newNode.data.monthHired < currentNode.data.monthHired) {
if(currentNode.senior == null) {currentNode.senior=newNode;}
else {currentNode.senior=currentNode.senior.addCat(newNode);}
}
else if(newNode.data.monthHired > currentNode.data.monthHired) {
if(currentNode.junior == null) {currentNode.junior = newNode;}
else {currentNode.junior=currentNode.junior.addCat(newNode);}
}
else if(newNode.data.monthHired == currentNode.data.monthHired) {
if(newNode.data.furThickness < currentNode.data.furThickness) {
if(currentNode.same == null) {currentNode.same=newNode;}
else {currentNode.same = currentNode.same.addCat(newNode);}
}
else if(newNode.data.furThickness > currentNode.data.furThickness) {
if(currentNode.same == null) {
currentNode.same = new CatNode(currentNode.data);
currentNode.data = newNode.data;
}
else {
CatNode temp;
temp = new CatNode(currentNode.data);
currentNode.data = newNode.data;
currentNode.same = currentNode.same.addCat(temp);
}
}
}
}
return this;
}
And my attempt at deletion:
if(nodeToRemove.same != null) {
nodeToRemove.same = nodeToRemove;
root = nodeToRemove.same;
nodeToRemove = null;
}
else if(nodeToRemove.same == null && nodeToRemove.senior != null) {
nodeToRemove.senior = nodeToRemove;
root = nodeToRemove.senior;
nodeToRemove = null;
}
else {
nodeToRemove.junior = nodeToRemove;
root = nodeToRemove.junior;
nodeToRemove = null;
}
I know this is a very big question but if you can't assist with it could you at least provide some insight into ternary search trees that contain objects like this because I can not find ANYTHING online about them.
Thank you so much!
Akhil K.
A ternary search tree is a special trie data structure where the child nodes of a standard trie are ordered as a binary search tree. Representation of ternary search trees: Unlike trie(standard) data structure where each node contains 26 pointers for its children, each node in a ternary search tree contains only 3 pointers: 1. The left pointer points to the node whose value is less than the value in the current node. 2. The equal pointer points to the node whose value is equal to the value in the current node. 3. The right pointer points to the node whose value is greater than the value in the current node.04/10/20