'Finding the rank of a student GPA in Binary Search Tree
I've read all the posts related to this question, yet I am still having a very hard time understanding how to implement the algorithm.
I have a fully written BST with methods that recursively add, delete etc Student
objects(the BST class is written for generics, though). I need to write a method that returns the rank of a student in the binary search tree based off their GPA, yet I do not understand how I should implement this. The Student
class has a getGPA() method, so I know ill need to use that somehow.
Should I store the elements into an array then search for the index? If so, what's the best algorithm to accomplish this? Is there a better way to do this?
Here is the important parts of my BST class:
public int size()
// Returns the number of elements in this BST.
{
return recSize(root);
}
private int recSize(BSTNode<T> tree)
// Returns the number of elements in tree.
{
if (tree == null)
return 0;
else
return recSize(tree.getLeft()) + recSize(tree.getRight()) + 1;
}
private boolean recContains(T element, BSTNode<T> tree)
// Returns true if tree contains an element e such that
// e.compareTo(element) == 0; otherwise, returns false.
{
if (tree == null)
return false; // element is not found
else if (element.compareTo(tree.getInfo()) < 0)
return recContains(element, tree.getLeft()); // Search left subtree
else if (element.compareTo(tree.getInfo()) > 0)
return recContains(element, tree.getRight()); // Search right subtree
else
return true; // element is found
}
public boolean contains (T element)
// Returns true if this BST contains an element e such that
// e.compareTo(element) == 0; otherwise, returns false.
{
return recContains(element, root);
}
private T recGet(T element, BSTNode<T> tree)
// Returns an element e from tree such that e.compareTo(element) == 0;
// if no such element exists, returns null.
{
if (tree == null)
return null; // element is not found
else if (element.compareTo(tree.getInfo()) < 0)
return recGet(element, tree.getLeft()); // get from left subtree
else
if (element.compareTo(tree.getInfo()) > 0)
return recGet(element, tree.getRight()); // get from right subtree
else
return tree.getInfo(); // element is found
}
public T get(T element)
// Returns an element e from this BST such that e.compareTo(element) == 0;
// if no such element exists, returns null.
{
return recGet(element, root);
}
private BSTNode<T> recAdd(T element, BSTNode<T> tree)
// Adds element to tree; tree retains its BST property.
{
if (tree == null)
// Addition place found
tree = new BSTNode<T>(element);
else if (element.compareTo(tree.getInfo()) <= 0)
tree.setLeft(recAdd(element, tree.getLeft())); // Add in left subtree
else
tree.setRight(recAdd(element, tree.getRight())); // Add in right subtree
return tree;
}
public void add (T element)
// Adds element to this BST. The tree retains its BST property.
{
root = recAdd(element, root);
}
private void inOrder(BSTNode<T> tree)
// Initializes inOrderQueue with tree elements in inOrder order.
{
if (tree != null)
{
inOrder(tree.getLeft());
inOrderQueue.enqueue(tree.getInfo());
inOrder(tree.getRight());
}
}
private void preOrder(BSTNode<T> tree)
// Initializes preOrderQueue with tree elements in preOrder order.
{
if (tree != null)
{
preOrderQueue.enqueue(tree.getInfo());
preOrder(tree.getLeft());
preOrder(tree.getRight());
}
}
private void postOrder(BSTNode<T> tree)
// Initializes postOrderQueue with tree elements in postOrder order.
{
if (tree != null)
{
postOrder(tree.getLeft());
postOrder(tree.getRight());
postOrderQueue.enqueue(tree.getInfo());
}
}
public int reset(int orderType)
// Initializes current position for an iteration through this BST
// in orderType order. Returns current number of nodes in the BST.
{
int numNodes = size();
if (orderType == INORDER)
{
inOrderQueue = new LinkedUnbndQueue<T>();
inOrder(root);
}
else
if (orderType == PREORDER)
{
preOrderQueue = new LinkedUnbndQueue<T>();
preOrder(root);
}
if (orderType == POSTORDER)
{
postOrderQueue = new LinkedUnbndQueue<T>();
postOrder(root);
}
return numNodes;
}
Solution 1:[1]
After several failures, it seems I have found a workable solution. Instead of storing in a generic array with a fixed size, I decided to try an ArrayList instead. I use a recursive method to add each node inorder to the array list:
//recursive method used to copy BST to array list
public void recArrayList(BSTNode<T> tree, ArrayList<T> list)
{
if (tree == null)
{
return;
}
recArrayList(tree.getLeft(), list);
list.add(tree.getInfo());
recArrayList(tree.getRight(), list);
}
//converts BST to arraylist used for finding GPA rank
public ArrayList<T> toArrayList()
{
stuList = new ArrayList<T>();
recArrayList(root, stuList);
return stuList;
}
And finally in the class using the BST:
//returns the rank of student in BST based off GPA
public int rank(Student stu)
{
ArrayList<Student> stuList = stuGPA.toArrayList();
for (i = 0; i < stuList.size(); i++)
{
if (stuList.get(i) == stu)
{
return i + 1;
}
}
return 0;
}
I dont like using ==
to compare references so i'm working on changing that, but for now this seems to be functioning correctly. Ty for the comments, it inspired me to go a different route.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | FuegoJohnson |