如何随机地向二叉搜索树中插入节点?

huangapple go评论110阅读模式
英文:

How do I randomly insert nodes into a binary search tree?

问题

  1. public void Insert(Comparable key, Object data)
  2. {
  3. // Create node
  4. Node node = new Node(key, data);
  5. // Walk down the tree
  6. Node parent = null;
  7. Node child = root;
  8. while (child != null)
  9. {
  10. // Parent goes down
  11. parent = child;
  12. // Child goes down
  13. if (key.compareTo(child.GetKey()) == 0)
  14. throw new RuntimeException("Duplicate key.");
  15. else if (key.compareTo(child.GetKey()) < 0)
  16. child = child.left;
  17. else
  18. child = child.right;
  19. }
  20. // Hang new node from parent or make it the root
  21. node.parent = parent;
  22. if (parent == null)
  23. root = node;
  24. else if (key.compareTo(parent.GetKey()) < 0)
  25. parent.left = node;
  26. else
  27. parent.right = node;
  28. }
  29. class Test
  30. {
  31. public static void main(String args[])
  32. {
  33. Tree tree = new Tree();
  34. // Create keys
  35. Node n1 = new Node(null, "1");
  36. Node n2 = new Node(null, "2");
  37. Node n3 = new Node(null, "3");
  38. Node n4 = new Node(null, "4");
  39. Node n5 = new Node(null, "5");
  40. Node n6 = new Node(null, "6");
  41. Node n7 = new Node(null, "7");
  42. // Create Scanner
  43. Scanner scanner = new Scanner(System.in);
  44. System.out.print("Enter 'sorted' or 'random': ");
  45. String s = scanner.nextLine();
  46. if (s.equals("sorted"))
  47. {
  48. tree.Insert("1", n1.GetData());
  49. tree.Insert("2", n2.GetData());
  50. tree.Insert("3", n3.GetData());
  51. tree.Insert("4", n4.GetData());
  52. tree.Insert("5", n5.GetData());
  53. tree.Insert("6", n6.GetData());
  54. tree.Insert("7", n7.GetData());
  55. }
  56. if (s.equals("random"))
  57. {
  58. tree.Insert("5", n5.GetData());
  59. tree.Insert("2", n2.GetData());
  60. tree.Insert("1", n1.GetData());
  61. tree.Insert("3", n3.GetData());
  62. tree.Insert("6", n6.GetData());
  63. tree.Insert("7", n7.GetData());
  64. tree.Insert("4", n4.GetData());
  65. }
  66. if (!s.equals("random") && !s.equals("sorted"))
  67. throw new RuntimeException("Invalid input.");
  68. }
  69. }
英文:

I'm a student learning about data structures in Java. I'm less than a beginner so please don't criticize my code too hardly :).

I want to randomly insert nodes into a binary search tree, but don't know how to do this if not manually.

I'll paste the Insert() method here and how I was previously inserting. Also I'm getting a compilation error because of the scanner. Any idea why?

  1. {
  2. // Create node
  3. Node node = new Node(key, data);
  4. // Walk down the tree
  5. Node parent = null;
  6. Node child = root;
  7. while (child != null)
  8. {
  9. // Parent goes down
  10. parent = child;
  11. // Child goes down
  12. if (key.compareTo(child.GetKey()) == 0)
  13. throw new RuntimeException(&quot;Duplicate key.&quot;);
  14. else if (key.compareTo(child.GetKey()) &lt; 0)
  15. child = child.left;
  16. else
  17. child = child.right;
  18. }
  19. // Hang new node from parent or make it the root
  20. node.parent = parent;
  21. if (parent == null)
  22. root = node;
  23. else if (key.compareTo(parent.GetKey()) &lt; 0)
  24. parent.left = node;
  25. else
  26. parent.right = node;
  27. }
  28. import java.util.Scanner;
  29. class Test
  30. {
  31. public static void main(String args[])
  32. {
  33. Tree tree = new Tree();
  34. // Create keys
  35. Node n1 = new Node(null, &quot;1&quot;);
  36. Node n2 = new Node(null, &quot;2&quot;);
  37. Node n3 = new Node(null, &quot;3&quot;);
  38. Node n4 = new Node(null, &quot;4&quot;);
  39. Node n5 = new Node(null, &quot;5&quot;);
  40. Node n6 = new Node(null, &quot;6&quot;);
  41. Node n7 = new Node(null, &quot;7&quot;);
  42. // Create Scanner
  43. Scanner scanner = new Scanner();
  44. System.out.print(&quot;Enter &#39;sorted&#39; or &#39;random&#39;: &quot;);
  45. String s = scanner.nextLine();
  46. if (s == &quot;sorted&quot;)
  47. tree.Insert(&quot;1&quot;, n1.GetData());
  48. tree.Insert(&quot;2&quot;, n2.GetData());
  49. tree.Insert(&quot;3&quot;, n3.GetData());
  50. tree.Insert(&quot;4&quot;, n4.GetData());
  51. tree.Insert(&quot;5&quot;, n5.GetData());
  52. tree.Insert(&quot;6&quot;, n6.GetData());
  53. tree.Insert(&quot;7&quot;, n7.GetData());
  54. if (s == &quot;random&quot;)
  55. tree.Insert(&quot;5&quot;, n5.GetData());
  56. tree.Insert(&quot;2&quot;, n2.GetData());
  57. tree.Insert(&quot;1&quot;, n1.GetData());
  58. tree.Insert(&quot;3&quot;, n3.GetData());
  59. tree.Insert(&quot;6&quot;, n6.GetData());
  60. tree.Insert(&quot;7&quot;, n7.GetData());
  61. tree.Insert(&quot;4&quot;, n4.GetData());
  62. if (s != &quot;random&quot; &amp;&amp; s != &quot;sorted&quot;)
  63. throw new RuntimeException(&quot;Invalid input.&quot;);
  64. }
  65. }
  66. </details>
  67. # 答案1
  68. **得分**: 0
  69. 以下是翻译好的内容:
  70. `Collections`类中有一个标准库方法可以对列表进行随机排序。要使用它,我们必须创建一个包含数据的列表,然后调用`Collections.shuffle`方法:
  71. ```java
  72. List<String> data = new ArrayList();
  73. for (int i = 1; i <= 7; i++) {
  74. data.add(String.valueOf(i));
  75. }
  76. Collections.shuffle(data);

现在您可以将数据插入到树中:

  1. for (String item: data) {
  2. tree.Insert(item, item);
  3. }

关于编译错误,没有一个不带参数的Scanner构造函数。您可能想要传入系统的标准输入流。

  1. // 创建Scanner
  2. Scanner scanner = new Scanner(System.in);

我还注意到另一件事:您不能使用==来比较字符串,比如if (s == "sorted"),您必须使用equals方法:if (s.equals("sorted"))

还有一件事,您需要在if语句中使用大括号{ }。否则,您希望有条件地运行的内容将始终运行。

  1. if (s.equals("sorted")) {
  2. tree.Insert("1", n1.GetData());
  3. tree.Insert("2", n2.GetData()); // <-- 无论s是什么都会运行
  4. }
英文:

There is a standard library method in the Collections class to shuffle a list. To use it, we must create a List with the data, and then call Collections.shuffle:

  1. List&lt;String&gt; data = new ArrayList();
  2. for (int i = 1; i &lt;= 7; i++) {
  3. data.add(String.valueOf(i));
  4. }
  5. Collections.shuffle(data);

Now you can just insert the data into the tree:

  1. for (String item: data) {
  2. tree.Insert(item, item);
  3. }

Regarding the compilation error, there is no Scanner constructor that takes no arguments. You probably wanted to pass in the system standard input stream.

  1. // Create Scanner
  2. Scanner scanner = new Scanner(System.in);

Another thing I noticed: you can't compare strings with == like in if (s == &quot;sorted&quot;) you must use the equals method instead: if (s.equals(&quot;sorted&quot;))

Yet another thing, you need to use brackets { } with if-statements. Otherwise things that you meant to run conditionally will run always.

  1. if (s == &quot;sorted&quot;)
  2. tree.Insert(&quot;1&quot;, n1.GetData());
  3. tree.Insert(&quot;2&quot;, n2.GetData()); // &lt;-- runs no matter what s is

huangapple
  • 本文由 发表于 2020年4月9日 04:12:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61109231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定