BinarySearch

2020-04-19  本文已影响0人  无远弗届_90

看完教学视频,记录一波。。。

package com.example.learning.binary;

/**
 * @DATE: 2020/4/19
 * @AUTHOR:ramboo
 * @Description 二叉树
 **/
public class BinarySearch {


    private int data;
    private BinarySearch left;
    private BinarySearch right;

    public BinarySearch(int data){
        this.data = data;
        this.left = null;
        this.right = null;
    }

    public void insert(BinarySearch root,int data){
        if(root.data < data){
            if(root.right == null){
                root.right = new BinarySearch(data);
            }else{
                insert(root.right,data);
            }
        }else{
            if(root.left == null){
                root.left = new BinarySearch(data);
            }else{
                insert(root.left,data);
            }
        }
    }


    public void middleScan(BinarySearch root){
        if(root != null){
            middleScan(root.left);
            System.out.println(root.data);
            middleScan(root.right);
        }
    }


    public static void main(String[] args) {

        int[] array = new int[]{42,8,4,98,76,86,18,75};
        BinarySearch root = new BinarySearch(42);
        for(int i = 1; i < array.length; i++){
            root.insert(root,array[i]);
        }
        root.middleScan(root);
    }
}

上一篇 下一篇

猜你喜欢

热点阅读