繁体   English   中英

Java解析二叉树结构

[英]Java parse binary tree structure

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

本质上,数据是这样存储的:

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具有二叉树实现,但是我不知道是否可以将它们用于此类工作。 我从未尝试过使用它们,所以我可能是错的。

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

谢谢!

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

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

以下伪代码遍历所有二叉树节点,而无需递归。

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:如果深度不是问题,则基于递归的解决方案通常更简单。

递归是采用某种伪代码形式的传统方式:

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

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

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: 

可以用来构建节点。 希望能帮助到你。

我不确定您是如何实现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));
    }
}

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

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

父节点的二叉搜索树级别顺序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM