简体   繁体   English

Java中的二叉树插入

[英]Binary Tree Insertion in Java

I have a list like the following. 我有一个如下列表。 I would like to know how to create a binary tree with this type of list in Java. 我想知道如何在Java中使用这种类型的列表创建一个二叉树。 Can anyone provide me with some binary tree insertion code in Java for this type of list? 谁能为我提供一些此类Java列表中的二进制树插入代码?

For example: 例如:

List 1:  AND AND AND G M S T

The binary tree will be: 二进制树将是:

       AND
   AND                AND 
 G     M            S     T

And for this list: 对于此列表:

List 2: AND AND G M S

The binary tree will be: 二进制树将是:

              AND
  AND          S
 G   M          

I tried following method for insertion: 我尝试了以下插入方法:

public void insert(RDFQuery node,  RDF leafValue) { 
    flag++;
    if ((flag%2)!=0) {
        if (node.left  != null) {
            flag--;
            nodeStore=node.left;
            leftFlag=1;
            insert(node.left, leafValue);
        } 
        else { 
              node.left = new RDFQuery(leafValue);
        }

    }       
    if ((flag%2)==0) {
        if (leftFlag==1) {
            node=nodeStore; 
            leftFlag=0;
        }

        if (node.right  != null) {
              flag--;   
              insert(node.right, leafValue);
        } 
        else { 
              node.right = new RDFQuery(leafValue);
              rightFlag=0;
        }
    }
}

I'm unsure how you want to turn your lists into trees, as I cannot reproduce both of your examples using any of the obvious algorithms I can think of. 我不确定您要如何将列表变成树,因为我无法使用我能想到的任何显而易见的算法来重现您的两个示例。

Fill by row, breadth first 逐行填充,广度优先

Usually, I'd fill in a level from left to right, before continuing in the next level. 通常,我会先从左到右填写一个级别,然后再继续下一个级别。 For your example input, this would result in the following tree: 对于您的示例输入,这将导致以下树:

List 2: AND AND G M S

        AND
       /   \
    AND     G
  /    \ 
 M      S

There are various ways you could implement this. 您可以通过多种方式实现此目的。 One would be maintaining two numbers: the index of the row you're currently inserting into (ie the depth) and the index of the new leaf within that row. 一种方法是维护两个数字:您当前要插入的行的索引(即深度)和该行中新叶子的索引。 The bit pattern of that index within the row would tell you the order of left and right children, starting at the root with the most significant bit. 行中该索引的位模式将告诉您左右子级的顺序,从具有最高有效位的根开始。 The depth would tell you how many bits you have to consider, ie which bit is the most significant of those you have to handle. 深度将告诉您必须考虑多少位,即哪一位是您必须处理的最高位。

Prefix notation 前缀表示法

On the other hand, the names of your elements suggest that some of them have a fixed arity. 另一方面,您的元素名称表明其中一些元素具有固定的统一性。 I imagine AND to be a binary operator in all cases, whereas the simple symbols are probably nullary. 我想象AND在所有情况下都是二进制运算符,而简单符号可能是无效的。 However, with this interpretation, your first example would result in a different list: 但是,按照这种解释,您的第一个示例将得出不同的列表:

       AND
     /     \
  AND       AND 
 /   \     /   \         
G     M  S      T

AND ( AND ( G, M ), AND ( S, T ) )
List 1: AND AND G M AND S T

If thsi were your interpretation, then you'd probably best maintain a reference to the current node, and after the last expected child has been added to it, switch from parent to parent until you reach a node which still expects children. 如果这是您的解释,那么您可能最好维护对当前节点的引用,并在最后一个预期的子项添加到它之后,从父项切换到父项,直到到达仍希望子项的节点。

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

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