Basic realization of binary tree

Basic realization of binary tree

The most critical problem in the realization of binary tree processing is the preservation of data, and because the data involves object comparison, then it must be supported by a comparator, and the first choice for this comparator must be the Comparable class.

If you want to save data later, you must first have a node class. Because the node class involves data storage, it must use Comparable (size can be distinguished).

package 二叉树;

interface IBinaryTree<T>{
    public void add(T data);    //添加数据的方法
    public int size();  //获取数组长度的方法
    public Object[] toArray();  //返回所有数据
}

class BinaTreeImpl<T> implements IBinaryTree<T>{
    //该内部类只为当前的外部类提供服务
    private class Node{
        private Comparable<T> data; //要存储的数据,全部进行强制性的转换
        private Node left;  //左子树
        private Node right; //右子数
        private Node parent;    //父节点

        public Node(Comparable<T> data){    //节点创建的同时保存数据
            this.data = data;
        }

        /**
         * 添加数据
         * @param newNode   要添加的数据对象
         */
        public void addNode(Node newNode){
            if(newNode.data.compareTo((T) this.data) <= 0){  //如果新节点小于当前对象
                if(this.left == null){  //当前左节点为null
                    this.left = newNode;
                    this.parent = this;
                }else {
                    this.left.addNode(newNode);   //递归调用
                }
            }
            if(newNode.data.compareTo((T) this.data) > 0){  //新节点大于当前对象
                if(this.right == null){
                    this.right = newNod
                    this.parent = this;
                }else{
                    this.right.addNode(newNode);   //递归调用
                }
            }
        }

        /**
         * 中序遍历数据并存储全部数据
         */
        public void toArrayNode(){
            if(this.left  != null){ //此时存在左子树
                this.left.toArrayNode();
            }

            BinaTreeImpl.this.resultData[BinaTreeImpl.this.foot++] =(T)this.data; //存储当前数据

            if(this.right != null){ //此时存在右子树
                this.right.toArrayNode();
            }
        }
    }

    //----------------以下操作为二叉树结构------------------
    private Node root;  //根节点
    private int count;  //数据的个数
    private int foot;   //控制数组的脚标
    private Object[] resultData;    //存储数据的对象数组

    //添加数据
    @Override
    public void add(T data) {
        if (data == null){  //由于data需要排序,如果为null肯定无法使用
            throw new NullPointerException("存储的二叉树数据是null");
        }
        if(!(data instanceof Comparable)){  //没有实现Comparable接口
            throw new ClassCastException("要保存的数据类没有实现Comparable接口");
        }
        Node newNode = new Node((Comparable<T>) data);  // 将数据保存在节点之中
        if(root == null){   //当前没有根节点存在
            this.root = newNode;
        }else{  //有根节点存在,需要确定节点的位置,提交给Node类处理
            this.root.addNode(newNode);
        }

        this.count++;   //记录数据个数
    }

    /**
     * 获取数组个数
     * @return  数组长度
     */
    @Override
    public int size(){
        return this.count;
    }

    /**
     * 获取全部数据
     * @return  存储全部数据的对象数组
     */
    @Override
    public Object[] toArray(){
        if(this.count == 0){
            return null;
        }
        this.foot = 0;  //脚标清零
        this.resultData = new Object[this.count];   //实例化对象数组
        this.root.toArrayNode();    //提交给Node处理
        return this.resultData;  //返回数组
    }
}




public class BinaryTree类 {
    public static void main(String[] args) {
        BinaTreeImpl<Integer> tree = new BinaTreeImpl<>();
        tree.add(8);
        tree.add(1);
        tree.add(19);
        System.out.println("【元素个数】"+tree.size());
        for(Object obj:tree.toArray()) {
            System.out.print(obj + "、");
        }
    }
}

[
Number of elements] 3 1, 8, 19,

Guess you like

Origin blog.csdn.net/weixin_46245201/article/details/112647494