简体   繁体   English

Java二叉树插入顺序

[英]Java Binary Tree Insert in Order

How do you insert items into a binary tree in java so that they are in order? 如何将项目插入Java的二叉树中以便它们井然有序? I want to use random values and sort them from smallest to largest then insert them into a binary tree in this order: 我想使用随机值并将它们从最小到最大排序,然后按以下顺序将它们插入到二叉树中:

          1
     2         3
   4   5     6   7
 8  9

When you say 'in order' you need to clarify, do you mean in sorted order or do you mean insertion order, etc. 当您说“按顺序”时,您需要澄清,按排序顺序是指插入顺序还是按插入顺序等。

There are lots of resources available on inserting into binary trees or the difference between to types of binary trees , or how to print a binary tree diagram , that I suspect this is a duplicate. 我怀疑这是重复的,有很多资源可用于插入二叉树 ,二叉树类型之间差异如何打印二叉树图

What is different about your example? 您的示例有何不同? Having '1' as the root node means you must not have a rebalancing tree since both '2' and '3' are larger than the value for your root node. 将“ 1”作为根节点意味着您必须没有平衡树,因为“ 2”和“ 3”都大于根节点的值。 Your insert rule seems inconsistent since if '1' is the first node inserted then all other values will cascade to the right branch of the tree unless you use a different rule for the root then at the other levels which would be a pain to code. 您的插入规则似乎不一致,因为如果“ 1”是插入的第一个节点,则所有其他值将级联到树的右分支,除非您对根使用其他规则,然后在其他级别使用其他规则,这将给代码带来痛苦。

Something like this?: 像这样吗?

public class BinaryTree {
    private List<Integer> list = new ArrayList<Integer>();


    public class BinaryTreeNode {
        private int p;

        public BinaryTreeNode(int p) {
            this.p = p;
        }

        private BinaryTreeNode getChild(int childP){
            BinaryTreeNode result= null;
            if (childP < list.size()){
                result = new BinaryTreeNode(childP);
            }
            return result;
        }

        public BinaryTreeNode getLeft(){
            return getChild(p*2+1);
        }

        public BinaryTreeNode getRight(){
            return getChild(p*2+2);
        }

        public int getValue(){
            return list.get(p);
        }
    }

    public void add(int item){
        list.add(item);
    }

    public BinaryTreeNode getRoot(){
        BinaryTreeNode result = null;
        if (!list.isEmpty()){
            result = new BinaryTreeNode(0);
        }
        return result;
    }
}

In Naftalin, Walder Java Collections and Generics I've faced with this implementation that I love best: Naftalin的Walder Java Collections和Generics中,我遇到了我最喜欢的实现:

public interface TreeVisitor<E, R> {
    public R visit(E leaf);

    public R visit(E value, Tree<E> left, Tree<E> right);
}

public abstract class Tree<E> {

    public abstract <R> R accept(TreeVisitor<E, R> visitor);

    public static <E> Tree<E> leaf(final E leaf) {
        return new Tree<E>() {
            @Override
            public <R> R accept(TreeVisitor<E, R> visitor) {
                return visitor.visit(leaf);
            }
        };
    }

    public static <E> Tree<E> branch(final E value, final Tree<E> left, final Tree<E> right){
        return new Tree<E>(){
            @Override
            public <R> R accept(TreeVisitor<E, R> visitor) {
                return visitor.visit(value, left, right);
            }
        };
    }
}

Now you can add any operation you want and create your tree as follows: 现在,您可以添加所需的任何操作并按如下所示创建树:

Tree<Integer> t = Tree.branch(1, 
            Tree.branch(2, 
                Tree.branch(4, Tree.leaf(8), Tree.leaf(9)), Tree.leaf(5)),
                Tree.branch(3, Tree.leaf(6), Tree.leaf(7));

I found the answer that I needed from this one. 我找到了我所需的答案。

Create a Complete Binary Tree using Linked Lists w/o comparing node values 使用链接列表创建完整的二叉树,不比较节点值

Some of the other things I was pointed to, either weren't quite what I wanted, or didn't work past like 8 or so values. 我还指出了其他一些东西,要么不是我想要的,要么没有像8个左右的值那样工作。

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

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