美文网首页
BinarySearch

BinarySearch

作者: 无远弗届_90 | 来源:发表于2020-04-19 16:49 被阅读0次

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

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);
    }
}

相关文章

网友评论

      本文标题:BinarySearch

      本文链接:https://www.haomeiwen.com/subject/vudvbhtx.html