Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.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中的二进制树ADP实现-插入如何工作?_Java_Data Structures_Tree_Binary Tree_Abstract Data Type - Fatal编程技术网

Java中的二进制树ADP实现-插入如何工作?

Java中的二进制树ADP实现-插入如何工作?,java,data-structures,tree,binary-tree,abstract-data-type,Java,Data Structures,Tree,Binary Tree,Abstract Data Type,我在理解二进制树ADT时遇到问题,尤其是插入方法 我在internet上找到的大多数实现都有两个参数,第一个是现有节点,第二个是要插入的数据,其工作原理类似于“如果数据小于节点数据,请将其添加为左节点,或者为左节点递归调用insert,否则在右侧执行相同的操作 对于普通的二叉树(而不是搜索二叉树),这种比较真的有必要吗?因为我想实现一个泛型树,这些值可能不太容易比较 树接口 public interface IBinaryTree<E> { public IBinaryTr

我在理解二进制树ADT时遇到问题,尤其是插入方法

我在internet上找到的大多数实现都有两个参数,第一个是现有节点,第二个是要插入的数据,其工作原理类似于“如果数据小于节点数据,请将其添加为左节点,或者为左节点递归调用insert,否则在右侧执行相同的操作

对于普通的二叉树(而不是搜索二叉树),这种比较真的有必要吗?因为我想实现一个泛型树,这些值可能不太容易比较

树接口

public interface IBinaryTree<E> {

    public IBinaryTreeNode<E> getRoot();

    public Boolean isEmpty();

    public void insert(E data);
}
public interface IBinaryTreeNode<E> {       
    public IBinaryTreeNode<E> getLeftNode();
    public IBinaryTreeNode<E> getRightNode();
    public E getData();

    public void setData(E data);
    public void setLeftNode(IBinaryTreeNode<E> node);
    public void setRightNode(IBinaryTreeNode<E> node);

    public IBinaryTreeNode<E> inorderFind(E data);
    public IBinaryTreeNode<E> preorderFind(E data);
    public IBinaryTreeNode<E> postorderFind(E data);

}
公共接口IBinaryTree{
公共IBinaryTreeNode getRoot();
公共布尔值为空();
公共空白插入(E数据);
}
树状界面

public interface IBinaryTree<E> {

    public IBinaryTreeNode<E> getRoot();

    public Boolean isEmpty();

    public void insert(E data);
}
public interface IBinaryTreeNode<E> {       
    public IBinaryTreeNode<E> getLeftNode();
    public IBinaryTreeNode<E> getRightNode();
    public E getData();

    public void setData(E data);
    public void setLeftNode(IBinaryTreeNode<E> node);
    public void setRightNode(IBinaryTreeNode<E> node);

    public IBinaryTreeNode<E> inorderFind(E data);
    public IBinaryTreeNode<E> preorderFind(E data);
    public IBinaryTreeNode<E> postorderFind(E data);

}
公共接口IBinaryTreeNode{
公共IBinaryTreeNode getLeftNode();
公共IBinaryTreeNode getRightNode();
公共E getData();
公共数据(E数据);
公共void setLeftNode(IBinaryTreeNode节点);
公共void setRightNode(IBinaryTreeNode节点);
公共IBinaryTreeNode索引查找(E数据);
公共IBinaryTreeNode预订单查找(E数据);
公共IBinaryTreeNode postorderFind(E数据);
}
树节点实现

public class BinaryTreeNode<E> implements IBinaryTreeNode<E> {

    private IBinaryTreeNode<E> left;
    private IBinaryTreeNode<E> right;
    E data;


    @Override
    public IBinaryTreeNode<E> getLeftNode() {
        return left;
    }

    @Override
    public IBinaryTreeNode<E> getRightNode() {
        return right;
    }

    @Override
    public E getData() {
        return data;
    }

    @Override
    public void setData(E data) {
        this.data = data;
    }

    @Override
    public void setLeftNode(IBinaryTreeNode<E> node) {
        left = node;
    }

    @Override
    public void setRightNode(IBinaryTreeNode<E> node) {
        right = node;

    }


    // Inorder search (Left tree --> Node --> Right tree)
    @Override
    public IBinaryTreeNode<E> inorderFind(E data) {
        if(left != null){
            IBinaryTreeNode<E> leftResult = this.left.inorderFind(data);
            if(leftResult != null)
                return leftResult;
        }
        if(this.data.equals(data))
            return this;

        if(right != null){
            IBinaryTreeNode<E> rightResult = this.right.inorderFind(data);
            if(rightResult != null)
                return rightResult;
        }

        return null;
    }

    // Preorder search (Node --> Left tree --> Right tree)
    @Override
    public IBinaryTreeNode<E> preorderFind(E data) {
        if(this.data.equals(data))
            return this;

        if(left != null){
            IBinaryTreeNode<E> leftResult = left.preorderFind(data);
            if(leftResult != null)
                return leftResult;
        }

        if(right != null){
            IBinaryTreeNode<E> rightResult = right.preorderFind(data);
            if(rightResult != null)
                return rightResult;
        }       

        return null;
    }

    // Postorder search (Left tree --> Right tree --> Node)
    @Override
    public IBinaryTreeNode<E> postorderFind(E data) {   

        if(left != null){
            IBinaryTreeNode<E> leftResult = left.preorderFind(data);
            if(leftResult != null)
                return leftResult;
        }

        if(right != null){
            IBinaryTreeNode<E> rightResult = right.preorderFind(data);
            if(rightResult != null)
                return rightResult;
        }       

        if(this.data.equals(data))
            return this;

        return null;
    }

}
public class BinaryTree<E extends Object> implements IBinaryTree<E> {

    IBinaryTreeNode<E> root;
    int size;

    public BinaryTree(IBinaryTreeNode<E> node){

        this.root = node;
        size++;
    }   

    @Override
    public IBinaryTreeNode<E> getRoot() {
        return root;
    }


    @Override
    public Boolean isEmpty() {
        return (size == 0);
    }

    @Override
    public void insert(E data) {

    }

}
公共类二进制TreeNode实现IBinaryTreeNode{
私有IBinaryTreeNode左;
私有IBinaryTreeNode权限;
E数据;
@凌驾
公共IBinaryTreeNode getLeftNode(){
左转;
}
@凌驾
公共IBinaryTreeNode getRightNode(){
返还权;
}
@凌驾
公共E getData(){
返回数据;
}
@凌驾
公共无效设置数据(E数据){
这个数据=数据;
}
@凌驾
公共void setLeftNode(IBinaryTreeNode节点){
左=节点;
}
@凌驾
公共void setRightNode(IBinaryTreeNode节点){
右=节点;
}
//顺序搜索(左树-->节点-->右树)
@凌驾
公共IBinaryTreeNode索引查找(E数据){
if(左!=null){
ibinarytreenodeleftresult=this.left.inorderFind(数据);
if(leftResult!=null)
返回结果;
}
if(this.data.equals(data))
归还这个;
if(右!=null){
IBinaryTreeNode rightResult=this.right.inorderFind(数据);
如果(rightResult!=null)
返回正确的结果;
}
返回null;
}
//预订单搜索(节点-->左树-->右树)
@凌驾
公共IBinaryTreeNode预订单查找(E数据){
if(this.data.equals(data))
归还这个;
if(左!=null){
IBinaryTreeNode leftResult=left.preorderFind(数据);
if(leftResult!=null)
返回结果;
}
if(右!=null){
IBinaryTreeNode rightResult=right.preorderFind(数据);
如果(rightResult!=null)
返回正确的结果;
}       
返回null;
}
//后序搜索(左树-->右树-->节点)
@凌驾
公共IBinaryTreeNode postorderFind(E数据){
if(左!=null){
IBinaryTreeNode leftResult=left.preorderFind(数据);
if(leftResult!=null)
返回结果;
}
if(右!=null){
IBinaryTreeNode rightResult=right.preorderFind(数据);
如果(rightResult!=null)
返回正确的结果;
}       
if(this.data.equals(data))
归还这个;
返回null;
}
}
最后是树的实现

public class BinaryTreeNode<E> implements IBinaryTreeNode<E> {

    private IBinaryTreeNode<E> left;
    private IBinaryTreeNode<E> right;
    E data;


    @Override
    public IBinaryTreeNode<E> getLeftNode() {
        return left;
    }

    @Override
    public IBinaryTreeNode<E> getRightNode() {
        return right;
    }

    @Override
    public E getData() {
        return data;
    }

    @Override
    public void setData(E data) {
        this.data = data;
    }

    @Override
    public void setLeftNode(IBinaryTreeNode<E> node) {
        left = node;
    }

    @Override
    public void setRightNode(IBinaryTreeNode<E> node) {
        right = node;

    }


    // Inorder search (Left tree --> Node --> Right tree)
    @Override
    public IBinaryTreeNode<E> inorderFind(E data) {
        if(left != null){
            IBinaryTreeNode<E> leftResult = this.left.inorderFind(data);
            if(leftResult != null)
                return leftResult;
        }
        if(this.data.equals(data))
            return this;

        if(right != null){
            IBinaryTreeNode<E> rightResult = this.right.inorderFind(data);
            if(rightResult != null)
                return rightResult;
        }

        return null;
    }

    // Preorder search (Node --> Left tree --> Right tree)
    @Override
    public IBinaryTreeNode<E> preorderFind(E data) {
        if(this.data.equals(data))
            return this;

        if(left != null){
            IBinaryTreeNode<E> leftResult = left.preorderFind(data);
            if(leftResult != null)
                return leftResult;
        }

        if(right != null){
            IBinaryTreeNode<E> rightResult = right.preorderFind(data);
            if(rightResult != null)
                return rightResult;
        }       

        return null;
    }

    // Postorder search (Left tree --> Right tree --> Node)
    @Override
    public IBinaryTreeNode<E> postorderFind(E data) {   

        if(left != null){
            IBinaryTreeNode<E> leftResult = left.preorderFind(data);
            if(leftResult != null)
                return leftResult;
        }

        if(right != null){
            IBinaryTreeNode<E> rightResult = right.preorderFind(data);
            if(rightResult != null)
                return rightResult;
        }       

        if(this.data.equals(data))
            return this;

        return null;
    }

}
public class BinaryTree<E extends Object> implements IBinaryTree<E> {

    IBinaryTreeNode<E> root;
    int size;

    public BinaryTree(IBinaryTreeNode<E> node){

        this.root = node;
        size++;
    }   

    @Override
    public IBinaryTreeNode<E> getRoot() {
        return root;
    }


    @Override
    public Boolean isEmpty() {
        return (size == 0);
    }

    @Override
    public void insert(E data) {

    }

}
公共类二进制树实现IBinaryTree{
朱鹮根;
整数大小;
公共二进制树(IBinaryTreeNode节点){
this.root=节点;
大小++;
}   
@凌驾
公共IBinaryTreeNode getRoot(){
返回根;
}
@凌驾
公共布尔值为空(){
返回(大小==0);
}
@凌驾
公共空白插入(电子数据){
}
}

如果要比较泛型类型的对象,只需实现Comparable compareTo(to)方法即可。