【问题标题】:Java parse binary tree structureJava解析二叉树结构
【发布时间】:2013-08-07 03:53:51
【问题描述】:

我目前正在研究一些数据结构,并且遇到了一些存储在二叉树中的数据,但我并不完全确定解析它的最佳方式。

基本上数据是这样存储的:

Structure 1: 
  LeftChild: 0xaddress
    Structure 2: 
      LeftChild: 0xaddress
        Structure 3:
         LeftChild: 0xaddress
          ........
         RightChild: 0xaddress
          Structure 4:
            LeftChild: 0xaddress
            RightChild: 0xaddress
      RightChild: 0xaddress
  RightChild: 0xaddress

现在显然很难对二叉树进行文本解释,所以希望我上面的糟糕尝试能解释一下。本质上,这一切都是从一个结构开始的,它有一个左右树条目,每个条目都有左右,最终其中一个会用完节点,然后树的下一个分支继续。

我不完全确定解决这个问题的最佳方法。

我的第一个方法是通过使用 while 循环来继续追逐树节点,但这似乎有点让人头疼。

我知道 Java 有二叉树实现,但我不知道是否可以将它们用于此类工作。我从来没有尝试过使用它们,所以我可能错了。

如果有人对如何解决这个问题有任何意见或建议,我将不胜感激。

谢谢!

【问题讨论】:

  • 顺序对您的解析方式有影响吗?就像订购与预购等一样。
  • 不,顺序无关紧要,因为节点包含足够的信息,可以在需要时将它们重新组合在一起。

标签: java recursion tree binary-tree


【解决方案1】:

一个建议:如果你的树太深,你应该避免基于递归的解决方案。那是因为您可能会遇到堆栈溢出问题。

要处理这种情况,您可以使用堆栈。

下面的伪代码不递归地遍历所有二叉树节点。

visitNodes(root) 
    add the root on the stack

    while the stack is not empty
        nodeToBeProcessed <- pop the top node from the stack

        process nodeToBeProcessed

        if nodeToBeProcessed has a left child
            add the left child on the stack

        if nodeToBeprocessed has a right child
            add the right child on the stack

注意:pop 操作返回并移除栈顶节点。

注意 2:如果深度不是问题,基于递归的解决方案通常更简单。

【讨论】:

  • 看起来堆栈只是门票,而且它很好并且易于实现。不需要可怕的递归!谢谢
【解决方案2】:

递归是以某种伪代码形式执行此操作的传统方式:

Node { // data
    Node leftChild;
    Node rightChild;
}

handleNode(Node node) {
    if (node is missing or null or blank)
        return;
    ... handle anything on the node
    handleNode(node.leftChild);
    handleNode(node.rightChild);
}

【讨论】:

    【解决方案3】:

    如果我理解正确,您的问题是从具有该格式的文件中读取信息,而不是在解析后遍历树。您应该创建节点并跟踪谁是使用堆栈的兄弟姐妹,您可以获得构建节点的方法。

    Stack<String> stack = new Stack<String>();
    try {
        BufferedReader in = new BufferedReader(new FileReader("yourfile.txt"));
        String str;
        while ((str = in.readLine()) != null)
        {
    
            if(str.contains("Structure"))
            {
                stack.push(str);
            }else
            {
                if(str.contains("Left"))
                {
                    stack.push(str);
                }
                if(str.contains("Right"))
                {
                    System.out.println(stack.pop());    
                    System.out.println(str);
                    System.out.println(stack.pop());
                }
            }
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    使用您的示例打印此代码:

             LeftChild: 0xaddress
             RightChild: 0xaddress
            Structure 3:
                LeftChild: 0xaddress
                RightChild: 0xaddress
              Structure 4:
          LeftChild: 0xaddress
          RightChild: 0xaddress
        Structure 2: 
      LeftChild: 0xaddress
      RightChild: 0xaddress
    Structure 1: 
    

    您可以使用它来构建节点。希望对您有所帮助。

    【讨论】:

      【解决方案4】:

      我不确定你是如何实现 yoru 的,但试试这个(你需要做一些明显的名称更改):

      //list to put all data into
      private static ArrayList<String> list = new ArrayList<String>();
      
      //recursive way to search binary tree
      public void binaryTreeSearchAndParse(Node root)
      {
          //does it exist?
          if(root != null)
          {
              list.add(root.getData());
              binaryTreeSearchAndParse(root.getLeft());
              binaryTreeSearchAndParse(root.getRight());
              //you may or may not need this depending on how you implemented your tree
              //binaryTreeSearchAndParse(root.getNextStructure());
          }    
      }
      
      public static void main(String[] args)
      {
      
         //get tree
         //pass in root of tree
         binaryTreeSearchAndParse(tree.getRoot());
      
          //now all data should be added in the global ArrayList
          for(int i = 0; i<list.size(); i++)
          {
               System.out.println(list.get(i));
          }
      }
      

      如果我知道您是如何实现树的,我可以为您提供更多帮助。

      这是一个递归解决方案。如果你想要一个迭代解决方案,你应该使用队列:

      Binary search tree level order with parent node

      【讨论】:

        猜你喜欢
        • 2014-10-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-18
        • 2021-02-07
        • 1970-01-01
        • 2011-11-28
        • 2011-12-22
        • 1970-01-01
        相关资源
        最近更新 更多