【问题标题】:How to input a binary tree in Java如何在Java中输入二叉树
【发布时间】:2016-10-24 19:05:47
【问题描述】:

我需要帮助在 Java 中实现二叉树。

我不是说喜欢从文本文件中读取它。我的意思是用户使用扫描仪输入树,然后它创建并输出树值。此外,“-”符号表示树中的空值。

到目前为止,我有一个节点类正在工作,但我需要它能够从所述节点中生成一棵树。它还需要知道何时停止接受输入。

这是我的代码的开始。我需要一种能够输入节点列表并使用这些类将其变成树的方法。

import java.util.Scanner;  
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.Queue;
import java.util.LinkedList;


/**
 *
 * @author phirstprince
 */

class TreeNode {
    int data;
    TreeNode LC, RC;

    public TreeNode(int x) {
        data = x;
        LC = null;
        RC = null;
    }

}

  public class TreeDeserialize {

      /**
       * @param args the command line arguments
       */
        public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          int x = input.nextInt(); 
      }

  }

例如,如果一个人要输入这个( - 是空节点)

1
2
3
-
4
5
6
-
-
-
-
-
-

它会像这样将它组织成一棵树。

         1
       /   \
      2     3
       \   / \
        4 5   6

有人认为他们可以提供帮助吗?我相信这需要排队。还有如何确保它知道何时停止接受输入?

【问题讨论】:

  • 您是否尝试实现堆结构?您显示的树与二叉搜索树不匹配(2、4 和 5 位于错误的位置),但它与堆匹配。

标签: java input tree binary-tree stdio


【解决方案1】:

您应该开发一种检查数字并将其添加到的方法,例如,如果数字大于父级,则应将其添加到右侧,如果小于父级,则应将其添加到左侧。我认为您可以借助递归来开发此功能。

【讨论】:

  • 查看此链接,它会对您有用cs.cmu.edu/~adamchik/15-121/lectures/Trees/trees.html
  • OP提出的结构不遵循右大左小的规则。在我看来它像一个堆,但它可能只是基于插入顺序。
  • 并不是按顺序添加的,更像是“广度优先”的添加方式。就像它一次将它们添加到树上一样。
  • 是的,这也是我所看到的,它是“广度优先”插入。结果树是错误的,但如果 OP 只提出一种数据格式而不考虑验证,那么他们的示例是可以的。
【解决方案2】:

这是一个尝试从左到右逐级插入新节点的函数。它基本上只是进行广度优先遍历,直到找到可以插入节点的位置。

public static void insert(TreeNode root, int x) {
    Queue<TreeNode> q = new LinkedList<TreeNode>();
    q.offer(root);

    while(!q.isEmpty()) {
        TreeNode u = q.poll();

        if(u.LC == null) {   
            u.LC = new TreeNode(x);
            break;
        }

        if(u.RC == null) {
            u.RC = new TreeNode(x);
            break;
        }

        if(!u.LC.isNull)
            q.offer(u.LC);

        if(!u.RC.isNull)
            q.offer(u.RC);
    }
}

请注意,要使其正常工作,您需要在用户输入“-”时插入一个特殊的“null”节点。为此,您可以在 TreeNode 类中使用布尔标志:

class TreeNode {
    int data;
    boolean isNull;
    TreeNode LC, RC;

    public TreeNode(int x) {
        data = x;
        LC = null;
        RC = null;
    }

    public TreeNode() {
        isNull = true;
    }
}

最后,您可以重载 insert 方法以在用户询问时插入 Null 节点。重载函数是相同的,只是它使用默认构造函数创建新的 TreeNodes:

public static void insert(TreeNode root) {
    Queue<TreeNode> q = new LinkedList<TreeNode>();
    q.offer(root);

    while(!q.isEmpty()) {
        TreeNode u = q.poll();

        if(u.LC == null) {   
            u.LC = new TreeNode();
            break;
        }

        if(u.RC == null) {
            u.RC = new TreeNode();
            break;
        }

        if(!u.LC.isNull)
            q.offer(u.LC);

        if(!u.RC.isNull)
            q.offer(u.RC);
    }
}

您的主要方法将如下所示:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int x = input.nextInt(); 
    TreeNode root = new TreeNode(x);

    while(true) {
        String str = input.next();
        if(str.equals("-"))
            insert(root);
        else
            insert(root, Integer.parseInt(str));
    }    
}

【讨论】:

  • 很奇怪。它仍然不让我输入 - 现在。
  • 我想我修正了输入。但现在它并没有让我停止添加输入。我如何确保它知道何时停止?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 1970-01-01
  • 2022-10-14
  • 2015-11-20
  • 2020-07-12
  • 1970-01-01
  • 2011-04-06
相关资源
最近更新 更多