二叉树(一)

package cn.jbit.nodetree.test;

public class Node {

    private int value;      //介值

    private Node left;      //二叉树左

    private Node rigth;     //二叉树右

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

    public void add(int value){
        if(value > this.value){
            if(rigth != null){
                rigth.add(value);
            }else{
                Node node = new Node(value);
                rigth = node;
            }
        }else{
            if(left != null){
                left.add(value);
            }else{
                Node node = new Node(value);
                left = node;
            }
        }
    }


    public boolean find(int value){
        if(value == this.value) {
            return true;
        }else if(value > this.value){
            if(rigth == null) return false;
            return rigth.find(value);
        }else{
            if(left == null) return false;
            return left.find(value);
        }
    }

}

你可能感兴趣的:(二叉树)