二叉树的实现

**

二叉树的实现

**

节点类Node的声明

//声明节点类
public class Node {
    // 声明 value值
    private int value 
    //左子节点
    private Node leftChild;
    //右节点
    private  Node rightChidl;
    public Node() {
    }
    public Node(int value) {
        this.value = value;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public Node getLeftChild() {
        return leftChild;
    }
    public void setLeftChild(Node leftChild) {
        this.leftChild = leftChild;
    }
    public Node getRightChidl() {
        return rightChidl;
    }
    public void setRightChidl(Node rightChidl) {
        this.rightChidl = rightChidl;
    }
    @Override
    public String toString() {
        return String.valueOf(value);
    }
}

二叉树的实现类

/**
 * 二叉树类
 */
public class BinaryTree {
    private Node root;
    public BinaryTree(int value) {
        // 根据value创建节点对象
        Node node = new Node(value);
        //设置根节点对象
        this.root = node;
    }
    public BinaryTree() {
    }
    public Node getRoot() {
        return root;
    }
    public void setRoot(Node root) {
        this.root = root;
    }
    //    添加数据
    public boolean add(int value) {
	//        根据value创建node对象
        Node node = new Node(value);
	//        判断是否有根节点
        if (root == null) {
            //将此node复值给此根节点
            root = node;
            return true;
        } else {//有根节点
	//            用来记录当前遍历的节点对象
            Node current = root;
            while (true) {
                // 判断此node的value的值是否小于current节点的value的值
                if (value < current.getValue()) {
                    //说明node应该出现在此current节点的左边
                    //判断current是否还有左节点对象
                    if (current.getLeftChild() == null){
				// 如果是null把node设置为此current的左节点
                           current.setLeftChild(node);
                           return true;
                    }else{//说明此current有左节点 则需要拿到此左节点的值 再与node进行比较value值
                        current = current.getLeftChild();
                    }
                }else  if (value > current.getValue()){
                    //来到这里说明此node节点需要出现在此current节点的右边
                    //判断此节点是否有右节点
                    if (current.getRightChidl() == null){
                        //设置此node节点为此current节点的右节点
                        current.setRightChidl(node);
                        return true;
                    }else {
			//来到这里说明此节点有右节点
			// 则获取该节点的右节点并且复制给current
                      current =   current.getRightChidl();
                    }
                }else {
			//说明此节点的value值与当前current的value值相同 直接终止循环
                    return false;
                }
            }
        }
    }
    /*
    根据节点内容获得节点对象
    value 内容
    节点对象,如果没有找到则返回null
     */
    public Node get(int value){
        //定义一个节点对象的变量,用来记录当前比较的节点对象
        Node currentNode = root;
        //定义一个死循环 进行循环查询比较
        while (true){
            //比较value值和currentNode的value值是否相同
            if(value == currentNode.getValue()){
			// 如果相同则返回此currentNode对象
                return currentNode;
            }else  if( value > currentNode.getValue()){
			//  这说明此value值得节点对象应该在此currentNode的右边
                currentNode = currentNode.getRightChidl();
            }else if( value < currentNode.getValue()){
			//  来到这里 说明value值的Node节点对象应该存在于currentNode的左边
                currentNode = currentNode.getLeftChild();
            }
            if(currentNode ==null){
                return  currentNode;
            }
        }
    }

    /*
    遍历node下的所有子节点的内容  按顺序输出
     */
    public  void order( Node node ){
        if(node == null){
            //递归的终止条件
            return;
        }
        //先遍历该node的左节点
        order(node.getLeftChild());
        //打印
        System.out.println(node);
        //再遍历node的右节点
        order(node.getRightChidl());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值