【问题标题】:Getting NullPointerException whille inserting element in BST在 BST 中插入元素时获取 NullPointerException
【发布时间】:2019-06-18 07:30:24
【问题描述】:

我正在尝试使用迭代方法在二叉搜索树中插入元素,但我得到 NullPointerException 并且我无法弄清楚为什么会出现此错误。我尝试更改循环并检查温度,但我没有得到那里发生的错误。 编辑:- 我已经添加了整个代码。

     import java.util.*;
  import java.io.*;

  class Node {
      Node left;
      Node right;
      int data;

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

  class Solution {

      public static void preOrder(Node root) {

          if (root == null)
              return;

          System.out.print(root.data + " ");
          preOrder(root.left);
          preOrder(root.right);

      }

      /* Node is defined as :
      class Node 
         int data;
         Node left;
         Node right;

         */

      public static Node insert(Node root, int data) {

          Node inserter = new Node(data);
          Node temp = root;
          while (true) {
              if (inserter.data <= temp.data) {
                  if (temp.left == null) {
                      temp.left = inserter;
                      break;
                  } else
                      temp = temp.left;

              } else {
                  if (temp.right == null) {
                      temp.right = inserter;
                      break;
                  } else
                      temp = temp.right;

              }
          }

          return root;
      }

      public static void main(String[] args) {
          Scanner scan = new Scanner(System.in);
          int t = scan.nextInt();
          Node root = null;
          while (t-- > 0) {
              int data = scan.nextInt();
              root = insert(root, data);
          }
          scan.close();
          preOrder(root);
      }
  }

【问题讨论】:

  • 根节点可以为 Null。你检查了吗?
  • 你从哪里得到空指针?在您的代码中的哪一行?
  • @TheWhiteRabbit ``` if (temp.left == null) ``` 在这一行我收到 NullPointerException 错误。
  • 您似乎将 null 传递给第一个参数 - root。您没有发布调用此方法的代码,因此我们无法为您提供更多帮助。

标签: java nullpointerexception binary-search-tree


【解决方案1】:
public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      int t = scan.nextInt();
      Node root = null;
      while (t-- > 0) {
          int data = scan.nextInt();
          root = insert(root, data);
      }
      scan.close();
      preOrder(root);
  }

在 main 方法中你传递了root == null

你需要重写你的insert 方法然后它才能工作

public static Node insert(Node root, int data) {

          // check if root == null then initialize root and return it
          if(root == null){ 
               return new Node(data);
          }
          //--------

          Node inserter = new Node(data);
          Node temp = root;
          while (true) {
              if (inserter.data <= temp.data) {
                  if (temp.left == null) {
                      temp.left = inserter;
                      break;
                  } else
                      temp = temp.left;

              } else {
                  if (temp.right == null) {
                      temp.right = inserter;
                      break;
                  } else
                      temp = temp.right;

              }
          }

          return root;
      }

【讨论】:

  • 谢谢 知道了。我添加了根为空的条件。 if(root == null) { root = inserter ; return root ; }
【解决方案2】:

您的问题在于您的主要方法:

  public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      int t = scan.nextInt();
      Node root = null;
      while (t-- > 0) {
          int data = scan.nextInt();
          root = insert(root, data);
      }
      scan.close();
      preOrder(root);
  }

你初始化Node root = null,当你第一次进入你的while循环时,root在传递给insert方法时仍然为null:root = insert(root, data);

public static Node insert(Node root, int data) {
  Node inserter = new Node(data);
  Node temp = root;
  while (true) {
    if (inserter.data <= temp.data) {
    ...

所以当你第一次进入insert方法时,temp会被设置为null,在if (inserter.data &lt;= temp.data)行的temp.data上给出一个空指针。

【讨论】:

  • 我该如何解决?我试过但无法得到它。抱歉,我是 Java 初学者,试图解决一些问题。
  • 只需使用新的 Node(int data) 构造函数来创建根节点,而不是为其分配 null 并继续您正在做的事情。
  • 谢谢我明白了,我添加了根为空的条件。 if(root == null) { root = inserter ; return root ; }
猜你喜欢
  • 1970-01-01
  • 2021-10-03
  • 2021-12-11
  • 2016-08-16
  • 1970-01-01
  • 2014-05-08
  • 1970-01-01
  • 2020-07-30
  • 2015-10-22
相关资源
最近更新 更多