在BST中获取NullPointerException Whille插入元素

时间:2019-06-18 07:30:24

标签: java nullpointerexception binary-search-tree

我正在尝试使用迭代方法在Binary Search Tree中插入元素,但是我遇到了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);
      }
  }

2 个答案:

答案 0 :(得分: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);
  }

在主要方法中,您正在传递root == null

您需要重写插入 方法,这样就可以了

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

答案 1 :(得分:0)

您的问题出在您的主要方法中:

  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方法时,root仍然为空: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,在temp.data行的if (inserter.data <= temp.data)上提供一个空指针。