排序二叉树的的添加,遍历

/**
 * 二叉树结点
 */
public class Node {
    private int data;
    private Node leftChild;
    private Node rigthChild;

    public Node(){

    }

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

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public Node getLeftChild() {
        return leftChild;
    }

    public void setLeftChild(Node leftChild) {
        this.leftChild = leftChild;
    }

    public Node getRigthChild() {
        return rigthChild;
    }

    public void setRigthChild(Node rigthChild) {
        this.rigthChild = rigthChild;
    }
}

package mr.yang.testBtree;

/**
 * 二叉树
 */
public class BinaryTree {
    private Node root;

    public BinaryTree() {

    }

    public BinaryTree(int val) {
        Node node = new Node(val);
        this.root = node;
    }

    public Node getRoot() {
        return root;
    }

    public void setRoot(Node root) {
        this.root = root;
    }

    /**
     * 二叉排序树添加
     */

    public void add(int val) {
        Node node = new Node(val);

        if (root == null) {
            this.root = new Node(val);
        } else {
            Node current = root;
            while (true) {
                if (val < current.getData()){//当前结点与根比较
                    //左边
                    if(current.getLeftChild()!=null){
                        //非空
                        current = current.getLeftChild();
                    }else {
                        //空
                        current.setLeftChild(node);
                        break;
                    }
                }else{
                    //右边
                    if(current.getRigthChild()!=null){
                        //非空
                        current = current.getRigthChild();
                    }else {
                        //空
                        current.setRigthChild(node);
                        break;
                    }
                }
            }
        }
    }


    /**
     * 查找值并反返回结点
     * @param val
     * @return
     */
    public Node get(int val){
        Node currentNode = root;
        while (true){
            if(val == currentNode.getData()) {
                return currentNode;
            }else if(val>currentNode.getData()){
                //右边
                if(currentNode.getRigthChild() == null) return null;
                currentNode = currentNode.getRigthChild();
            }else {
                //左边
                if(currentNode.getLeftChild() == null) return null;
                currentNode = currentNode.getLeftChild();
            }
        }


    }



    /**
     * 中序遍历  左根右
     */
    public void oder(Node node){

        if(node == null) return;
        oder(node.getLeftChild());
        System.out.println(node.getData());
        oder(node.getRigthChild());
    }
}

测试代码:

package mr.yang.testBtree;

public class TestBinaryTree {

    public static void main(String[] args) {

        int a[] = {3,1,0,2,7,5,8,9};

        BinaryTree bt = new BinaryTree();

        for (int i = 0; i < a.length; i++) {
            int val = a[i];
            bt.add(val);
        }

        Node node = bt.get(8);
        System.out.println(node.getData());
        System.out.println(node.getLeftChild());
        System.out.println(node.getRigthChild().getData());

       /* Node node1 = bt.get(5);
        System.out.println(node1.getData());
        System.out.println(node1.getLeftChild());
        System.out.println(node1.getRigthChild());*/

       bt.oder(bt.getRoot());




    }
}

分析:

在这里插入图片描述
每一个结点,有左孩子,数据,,右孩子

树由结点组成。将比根结点大的放右边,将比根结点小的放在边。

遍历:左根右。。。.

1.先遍历左孩子,若为空,则返回。否则递归遍历左孩子
2.输出中序
3遍历右孩子,若为空,则返回。否则递归遍历右孩子

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值