Java:在二叉搜索树中查找学生GPA的等级

时间:2017-03-31 03:17:31

标签: java binary-search-tree

这让我很头疼。我已经阅读了与此问题相关的所有帖子,但我仍然很难理解如何实现该算法。我花了好几个小时努力学习......

我有一个完全编写的BST,其中包含递归添加,删除ect Student对象的方法(但是BST类是为泛型编写的)。我需要编写一个方法,根据他们的GPA返回二元搜索树中学生的等级,但我不明白我应该如何实现它。 Student类有一个getGPA()方法,所以我知道不需要以某种方式使用它。

我应该将元素存储到数组中,然后搜索索引吗?如果是这样,最好的算法是什么来完成这个?有一个更好的方法吗?如果有人能帮我解决这个问题,我会非常感激。

以下是我的BST课程的重要部分:

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

我知道之前已经问过这类问题,但我不明白,而且我感到很沮丧。感谢任何可以帮助我的人:)

1 个答案:

答案 0 :(得分:0)

经过多次失败,似乎我找到了一个可行的解决方案。我决定尝试使用ArrayList,而不是存储在固定大小的通用数组中。我使用递归方法将每个节点添加到数组列表中:

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

最后在课堂上使用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;
    }

我不喜欢使用==来比较引用,所以我正在努力改变它,但是现在这似乎正常运行。 Ty的评论,它激励我走另一条路。