简体   繁体   English

通过降低二叉搜索树中的字数来打印字计数器

[英]Printing a word counter by descending number of counts of words within binary search tree

I've created a word counter using a binary search tree. 我使用二叉搜索树创建了一个单词计数器。 When a word is added more than once, the count of that word increases. 当一个单词被多次添加时,该单词的计数会增加。 My problem lies in the fact that I want to have two toString() functions: one that prints the word and its count in alphabetical order (which I've already figured out), and one that prints the word and its count in descending count order, and if the counts for words are the same, sorted by ascending alphabetical order. 我的问题在于我想要有两个toString()函数:一个按字母顺序打印单词及其计数(我已经想到了),还有一个用降序计数打印单词及其计数如果单词的计数相同,则按升序字母顺序排序。

I'm struggling with the second toString() function. 我正在努力使用第二个toString()函数。 I set up a way to compare a node's count values to the count values of the left and right nodes, and to return the count of the largest one. 我设置了一种方法来比较节点的计数值与左右节点的计数值,并返回最大节点的计数。 However, I'm getting a few errors, and I'm not sure I'm on the right track here. 但是,我收到了一些错误,我不确定我是否在这里正确的方向。

Here's my code which includes my Node container class, the Add function, and my working basic toString() function. 这是我的代码,其中包括我的Node容器类,Add函数和我的工作基本toString()函数。

public class WordCounter extends BinarySearchTree<String, Integer>
{
  public String word;
  public int size;

  // node container class

  public class Node<String>
  {
    public String data;
    public int count = 1;

    public Node<String> left, right;

    public Node(String data)
    {
      this.data = data;
      left = null;
      right = null;
    }
  }

  private Node<String> root;

  public WordCounter()
  {
    root = null;
    size = 0;
  }

  // add function

  public void Add(String word)
  {
    root = recAdd(root, new Node<String>(word));
  }

  private Node<String> recAdd(Node<String> node, Node<String> newNode)
  {
    // base case - found open slot
    if (node == null)
    {
      return newNode;
    }

    // general case - add to right

    final int comparison = newNode.data.compareTo(node.data);

    if (comparison > 0)
    {
      node.right = recAdd(node.right, newNode);
    }

    // general case - add to left

    else if (comparison < 0)
    {
      node.left = recAdd(node.left, newNode);
    }

    // if word values are the same, increment word count

    else
    {
      node.count++;
    }
    return node;
  }

  // toString() function

  public String toString()
  {
    return recInOrderToString(root);
  }

  private String recInOrderToString(Node<String> node)
  {
    if (node == null)
    {
      return "";
    }

    return recInOrderToString(node.left) + "\n word: " + node.data.toString() + "\t count: " + node.count + recInOrderToString(node.right);
  }

Here's the function I'm struggling with. 这是我正在努力的功能。

   public String toStringByCount()
  {
    return recToStringByCount(root);
  }

  private String recToStringByCount(Node<String, Integer> node)
  {
    // to be thorough in comparisons, I went through every possible case
    // counts are compared first; if any are the same, their data must be compared
    // larger counts are printed first, then ascending alphabetical order for those with the same counts

    // node is null, return empty string
    if (node == null)
      return "";

    // both nodes are null, return current node
    if (node.left == null && node.right == null)
    {
      return "\n word: " + node.data.toString() + "\t count: " + node.count;
    }

    // right node is null, left is not null
    if (node.right == null && node.left != null)
    {
      if (node.count > node.left.count)
      {
        return "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.left);
      } else if (node.count < node.left.count)
      {
        return recToStringByCount(node.left) + "\n word: " + node.data.toString() + "\t count: " + node.count;
      } else if (node.count == node.left.count)
      {
        if (node.data.compareTo(node.left.data) < 0)
        {
          return recToStringByCount(node.left) + "\n word: " + node.data.toString() + "\t count: " + node.count;
        } else if (node.data.compareTo(node.left.data) > 0) {
          return "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.left);
        }
      }
    }

    //left node is null, right is not null
    if (node.left == null && node.right != null)
    {
      if (node.count > node.right.count)
      {
        return "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.right);
      } else if (node.count < node.right.count)
      {
        return recToStringByCount(node.right) + "\n word: " + node.data.toString() + "\t count: " + node.count;
      } else if (node.count == node.right.count)
      {
        if (node.data.compareTo(node.right.data) < 0)
        {
          return recToStringByCount(node.right) + "\n word: " + node.data.toString() + "\t count: " + node.count;
        } else if (node.data.compareTo(node.right.data) > 0) {
          return "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.right);
        }
      }
    }

    // both nodes are valid
    if ((node.left != null) && (node.right != null))
    {
      // counts are the same
      if ((node.count == node.left.count) && (node.count == node.right.count))
      {
        // must compare data

        // data is greater than left and right
        if ((node.data.compareTo(node.left.data) > 0) && (node.data.compareTo(node.right.data) > 0))
        {
          if (node.left.data.compareTo(node.right.data) > 0)
          {
            return "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.left) + recToStringByCount(node.right);
          } else if (node.left.data.compareTo(node.right.data) < 0) {
            return "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.right) + recToStringByCount(node.left);
          }
        }

        // data is less than left and right
        else if ((node.data.compareTo(node.left.data) < 0) && (node.data.compareTo(node.right.data) < 0))
        {
          if (node.left.data.compareTo(node.right.data) > 0)
          {
            return recToStringByCount(node.left) + recToStringByCount(node.right) + "\n word: " + node.data.toString() + "\t count: " + node.count;
          } else if (node.left.data.compareTo(node.right.data) < 0) {
            return recToStringByCount(node.right) + recToStringByCount(node.left) + "\n word: " + node.data.toString() + "\t count: " + node.count;
          }
        }

        // data is less than left, greater than right
        else if ((node.data.compareTo(node.left.data) < 0) && (node.data.compareTo(node.right.data) > 0))
        {
          return recToStringByCount(node.left) + "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.right);
        }

        // data is greater than left, less than right
        else if ((node.data.compareTo(node.left.data) > 0) && (node.data.compareTo(node.right.data) < 0))
        {
          return recToStringByCount(node.right) + "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.left);
        }
      }

      // left is same, smaller than right
      else if ((node.count == node.left.count) && (node.count < node.right.count))
      {
        // must compare data

        // data greater than left
        if (node.data.compareTo(node.left.data) > 0)
        {
          return recToStringByCount(node.right) + "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.left);
        }

        // data smaller than left
        else if (node.data.compareTo(node.left.data) < 0)
        {
          return recToStringByCount(node.right) + recToStringByCount(node.left) + "\n word: " + node.data.toString() + "\t count: " + node.count;
        }
      }

      // left is same, greater than right
      else if ((node.count == node.left.count) && (node.count > node.right.count))
      {
        // must compare data

        // data greater than left
        if (node.data.compareTo(node.left.data) > 0)
        {
          return "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.left) + recToStringByCount(node.right);
        }

        // data smaller than left
        else if (node.data.compareTo(node.left.data) < 0)
        {
          return recToStringByCount(node.left) + "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.right);
        }
      }

      // right is same, smaller than left
      else if ((node.count == node.right.count) && (node.count < node.left.count))
      {
        // must check data

        // data greater than right
        if (node.data.compareTo(node.right.data) > 0)
        {
          return recToStringByCount(node.left) + "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.right);
        }

        // data smaller than right
        else if (node.data.compareTo(node.right.data) < 0)
        {
          return recToStringByCount(node.left) + recToStringByCount(node.right) + "\n word: " + node.data.toString() + "\t count: " + node.count;
        }
      }

      // right is same, greater than left
      else if ((node.count == node.right.count) && (node.count > node.right.count))
      {
        // must compare data

        // data greater than right
        if (node.data.compareTo(node.right.data) > 0)
        {
          return "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.right) + recToStringByCount(node.left);
        }

        // data smaller than right
        else if (node.data.compareTo(node.right.data) < 0)
        {
          return recToStringByCount(node.right) + "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.left);
        }
      }

      // greater than right & left
      else if ((node.count > node.left.count) && (node.count > node.right.count))
      {
        // must compare right & left count

        // right greater than left
        if (node.right.count > node.left.count)
        {
          return "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.right) + recToStringByCount(node.left);
        }

        // right smaller than left
        else if (node.right.count < node.left.count)
        {
          return "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.left) + recToStringByCount(node.right);
        }

        // right equal to left
        else if (node.right.count == node.left.count)
        {
          // must compare data

          if (node.right.data.compareTo(node.left.data) > 0)
          {
            return "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.right) + recToStringByCount(node.left);
          } else if (node.right.data.compareTo(node.left.data) < 0) {
            return "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.left) + recToStringByCount(node.right);
          }
        }
      }

      // less than right & left
      else if ((node.count < node.left.count) && (node.count < node.right.count))
      {
        // must compare right & left counts

        // right greater than left
        if (node.right.count > node.left.count)
        {
          return recToStringByCount(node.right) + recToStringByCount(node.left) + "\n word: " + node.data.toString() + "\t count: " + node.count;
        }

        // right less than left
        if (node.right.count < node.left.count)
        {
          return  recToStringByCount(node.left) + recToStringByCount(node.right) + "\n word: " + node.data.toString() + "\t count: " + node.count;
        }

        // right equal to left
        if (node.right.count == node.left.count)
        {
          // must compare data

          if (node.right.data.compareTo(node.left.data) > 0)
          {
            return recToStringByCount(node.right) + recToStringByCount(node.left) + "\n word: " + node.data.toString() + "\t count: " + node.count;
          } else if (node.right.data.compareTo(node.left.data) < 0) {
            return recToStringByCount(node.left) + recToStringByCount(node.right) + "\n word: " + node.data.toString() + "\t count: " + node.count;
          }
        }
      }

      // greater than left, less than right
      else if ((node.count > node.left.count) && (node.count < node.right.count))
      {
        return recToStringByCount(node.right) + "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.left);
      }

      // smaller than left, greater than right
      else if ((node.count < node.left.count) && (node.count > node.right.count))
      {
        return recToStringByCount(node.left) + "\n word: " + node.data.toString() + "\t count: " + node.count + recToStringByCount(node.right);
      }
    }
    return "";
  }

Like I said, not sure I'm approaching this correctly. 就像我说的,不确定我是否正确接近这一点。 I'm also getting errors for "int cannot be dereferenced" on compareCountLeft and compareCountRight, and "int cannot be converted to String" on my return statements. 我在compareCountLeft和compareCountRight上也遇到“int not beedferenced”的错误,并且在我的return语句中“int不能转换为String”。

Some problems: 一些问题:
1. you need to test node.left and node.right aren't null before using the compareTo function 1.在使用compareTo函数之前,您需要测试node.leftnode.right不为null
2. You are returning integer instead of string. 2.您将返回整数而不是字符串。 you are missing the return of the data itself: 你错过了数据本身的返回:

private String recToStringByCount(Node<String> node) {
  if (node == null)
    return "";
  if (node.left == null && node.right == null) 
    return "\n word: " + node.data.toString() + "\t count: " + node.count;
  1. your if statements doesn't cover all the cases, so you don't have a return statement for each possible outcome, therefore the function won't run if语句不包括所有情况,因此您没有针对每个可能结果的return语句,因此该函数将不会运行
  2. you are missing the recursive call to itself. 你错过了对自身的递归调用。 in the if statements instead of returning count, you should do something like your first toString: if语句中而不是返回count,你应该做一些像你的第一个toString:
return recToStringByCount(node.left) + recToStringByCount(node) + recToStringByCount(node.right);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM