Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带方法的二叉树数据结构_Java_Binary Tree - Fatal编程技术网

Java 带方法的二叉树数据结构

Java 带方法的二叉树数据结构,java,binary-tree,Java,Binary Tree,我实现了一个二叉树数据结构。数据结构允许从列表中构建二叉树(元素从左到右插入)。如何优化插入元素?目前,它是递归的,所以如果树很深,它将耗尽内存。如何使其尾部递归,甚至结束递归 public class Node { private int value; private boolean isLeaf; public Node (int value, boolean isLeaf){ this.value = value; this.is

我实现了一个二叉树数据结构。数据结构允许从列表中构建二叉树(元素从左到右插入)。如何优化插入元素?目前,它是递归的,所以如果树很深,它将耗尽内存。如何使其尾部递归,甚至结束递归

public class Node {

    private int value;
    private boolean isLeaf;

    public Node (int value, boolean isLeaf){
        this.value = value;
        this.isLeaf = isLeaf;
    }

    public int getValue(){
        return value;
    }

    public void setLeaf(boolean value){
        this.isLeaf = value;
    }
    public boolean isLeaf(){
        return isLeaf;
    }

}



public class BinaryTree {

    Node root;
    BinaryTree left_child;
    BinaryTree right_child;

    public BinaryTree(){

    }
    public BinaryTree(Node root, BinaryTree left_child, BinaryTree right_child){
        this.root = root;
        this.left_child = left_child;
        this.right_child = right_child;
    }

    public BinaryTree insertElement(int element){
        if (root==null)
            return new BinaryTree(new Node(element, true), null, null);
        else {
            if (root.isLeaf()){
                root.setLeaf(false);
                if (element < root.getValue())
                    return new BinaryTree(root, new BinaryTree(new Node(element, true), null, null), null);
                else
                    return new BinaryTree(root, null, new BinaryTree(new Node(element, true), null, null));
            } else {
                if (element < root.getValue())
                    if (left_child!=null)
                        return new BinaryTree(root, left_child.insertElement(element), right_child);
                    else
                        return new BinaryTree(root, new BinaryTree(new Node(element, true), null, null), right_child);
                else
                    if (right_child!=null)
                        return new BinaryTree(root, left_child, right_child.insertElement(element));
                    else
                        return new BinaryTree(root, left_child, new BinaryTree(new Node(element, true), null, null));
            }
        }
    }

    public BinaryTree getLeftChild(){
        return left_child;
    }

    public BinaryTree getRightChild(){
        return right_child;
    }

    public void setLeftChild(BinaryTree tree){
        this.left_child = tree;
    }

    public void setRightChild(BinaryTree tree){
        this.right_child = tree;
    }

    public BinaryTree buildBinaryTree(int[] elements){
        if (elements.length==0)
            return null;
        else{
            BinaryTree tree = new BinaryTree(new Node(elements[0], true), left_child, right_child);
            for (int i=1;i<elements.length;i++){
                tree = tree.insertElement(elements[i]);
            }
            return tree;
        }
    }

    public void traversePreOrder(){
        if (root!=null)
            System.out.print(root.getValue() + " ");
        if (left_child!=null)
            left_child.traversePreOrder();
        if (right_child!=null)
            right_child.traversePreOrder();
    }

    public void traverseInOrder(){
        if (left_child!=null)
            left_child.traverseInOrder();
        if (root!=null)
            System.out.print(root.getValue() + " ");
        if (right_child!=null)
            right_child.traverseInOrder();
    }
    public void traversePostOrder(){
        if (left_child!=null)
            left_child.traversePostOrder();
        if (right_child!=null)
            right_child.traversePostOrder();
        if (root!=null)
            System.out.print(root.getValue() + " ");
    }

    public static void main(String[] args){
        int[] elements = new int[]{5,7,2,1,4,6,8};
        BinaryTree tree = new BinaryTree();
        tree = tree.buildBinaryTree(elements);
        tree.traversePreOrder();
        System.out.println();
        tree.traverseInOrder();
        System.out.println();
        tree.traversePostOrder();
        System.out.println();
    }

}
公共类节点{
私有int值;
私有布尔isLeaf;
公共节点(int值,布尔isLeaf){
这个值=值;
this.isLeaf=isLeaf;
}
public int getValue(){
返回值;
}
公共void setLeaf(布尔值){
this.isLeaf=值;
}
公共布尔isLeaf(){
返回岛;
}
}
公共类二叉树{
节根;
左二叉树;
二叉树右_子;
公共二叉树(){
}
公共二进制树(节点根、二进制树左\u子级、二进制树右\u子级){
this.root=根;
this.left_child=left_child;
this.right\u child=right\u child;
}
公共二进制树插入元素(int元素){
if(root==null)
返回新的二进制树(新节点(element,true),null,null);
否则{
if(root.isLeaf()){
根.刚毛叶(假);
if(元素对于(inti=1;i,在编码中,创建

仅包含值和布尔变量isLeaf的节点类

每当创建一个元素时,都会基于插入的元素创建一个新的二叉树,并附加到主树

实现二叉树的另一种方法是

节点类包含自己的值、左节点和右节点


插入元素仍然是递归的,但每当插入元素时,您不需要创建新的二叉树,只需创建一个新节点,就像编码中的

仅包含值和布尔变量isLeaf的节点类

每当创建一个元素时,都会基于插入的元素创建一个新的二叉树,并附加到主树

实现二叉树的另一种方法是

节点类包含自己的值、左节点和右节点


插入元素仍然是递归的,但每当插入元素时,您不需要创建新的二叉树,只需创建一个新节点,如如果您认为树太深且内存不足,最好使用循环实现逻辑,而不是使用递归。 temproot=根; while(已插入!=真){

if(root==null)
//在根上添加。
else if(itemitem)//它应该转到左侧。
{
if(temproot->left==null)
//将您的节点添加到这里并插入=true或put break;否则只需将指针向左移动即可。
temproo=temproot->left;//取左边的地址。
}
else if(应该向右)
//检查上述if子句的逻辑。
}//while循环结束
若你们发现它应该在左边,并且左边并没有子节点,那个就把你们的节点添加到那个里。
无需将所有访问的节点放入系统堆栈中,因为无论如何您都没有使用这些节点。

如果您认为树太深并且内存不足,最好使用循环实现逻辑,而不是使用递归。 temproot=根; while(已插入!=真){

if(root==null)
//在根上添加。
else if(itemitem)//它应该转到左侧。
{
if(temproot->left==null)
//将您的节点添加到这里并插入=true或put break;否则只需将指针向左移动即可。
temproo=temproot->left;//取左边的地址。
}
else if(应该向右)
//检查上述if子句的逻辑。
}//while循环结束
若你们发现它应该在左边,并且左边并没有子节点,那个就把你们的节点添加到那个里。
不需要将所有访问的节点都放在系统堆栈中,因为无论如何您都没有使用这些节点。

如果节点类包含左节点和右节点信息,那么它已经完全表示了二叉树。不需要二叉树类。您不认为吗?我不明白这是什么意思“不需要二叉树类。“您认为如果我只使用一个类实现二叉树,在存储方面会有很大的不同吗?如果我有一个节点类,其中包含表示二叉树所需的所有信息,为什么我要定义另一个类?这就是我的意思。@Bob您看过我的代码示例了吗?我的意思是节点类只包含关于no的所有信息。”
if(root==null)
    //add at the root.
else if(item<temproot->item )//it should go to left.
{
    if(temproot->left==null)
        //add your node here and inserted=true or put break; else just move pointer to   left.
 temproo=temproot->left; //take left address.

}
else if(it should go to right)
    //check the logic of above if clause.
}//end of while loop