1.和链表一样:动态存储
2.具有唯一的根
3.每个结点最多只有2个孩子,每个结点最多只有一个父亲
4.具有天然的递归结构
1.是二叉树
2.每个结点:大于其左子树结点值,小于右子树结点的值
3.每个子树也是二分搜索树
//创建结点
private class Node {
private T ele;//值
private Node left;//左孩子索引
private Node right;
public Node(T ele) {
this.ele = ele;
}
}
private Node root;//根
private int size;
public BinearySearchTree(){
this.root=null;
this.size=0;
}
public int getSize(){
return this.size;
}
//添加
public void add(T ele){
//更新根
this.root=addDG(this.root,ele);
this.size++;
}
//向以root为根的二分搜索树添加元素
private Node addDG(Node root,T ele){
//终止条件
if(root==null){
return new Node(ele);
}
//递归操作
if(root.ele.compareTo(ele)>0){
root.left= addDG(root.left,ele);
}
else{
root.right=addDG(root.right,ele);
}
return root;
}
//查询
public boolean search(T ele){
return searchDG(this.root,ele);
}
private boolean searchDG(Node root,T ele){
//终止
if(root==null){
return false;
}
if(root.ele.compareTo(ele)==0){
return true;
}else if(root.ele.compareTo(ele)>0){
return searchDG(root.left,ele);
}else{
return searchDG(root.right,ele);
}
}
public void preTravel() {
//先序
List<T> list = new ArrayList<>();
preTrvelDG(this.root, list);
String res=list.stream().map(item -> item.toString()).collect(Collectors.joining("-"));
System.out.println(res);
}
private void preTrvelDG(Node root,List<T>list){
//先序
if(root==null){
//判断
return ;
}
//操作
list.add(root.ele);
preTrvelDG(root.left,list);
preTrvelDG(root.right,list);
}
public void midTravel() {
//中序
List<T> list = new ArrayList<>();
midTrvelDG(this.root, list);
String res=list.stream().map(item -> item.toString()).collect(Collectors.joining("-"));
System.out.println(res);
}
private void midTrvelDG(Node root,List<T>list){
//中序
if(root==null){
//判断
return ;
}
//操作
midTrvelDG(root.left,list);
list.add(root.ele);
midTrvelDG(root.right,list);
}
public void suffiuxTravel() {
//后序
List<T> list = new ArrayList<>();
suffiuxTrvelDG(this.root, list);
String res=list.stream().map(item -> item.toString()).collect(Collectors.joining("-"));
System.out.println(res);
}
private void suffiuxTrvelDG(Node root,List<T>list){
//后序
if(root==null){
//判断
return ;
}
//操作
suffiuxTrvelDG(root.left,list);
suffiuxTrvelDG(root.right,list);
list.add(root.ele);
}
package com.ffyc.learn.binearysearch;
import com.ffyc.leetcodetest.sortList077;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
//二分搜索树
public class BinearySearchTree<T extends Comparable<T>> {
//创建结点
private class Node {
private T ele;//值
private Node left;//左孩子索引
private Node right;
public Node(T ele) {
this.ele = ele;
}
}
private Node root;//根
private int size;
public BinearySearchTree(){
this.root=null;
this.size=0;
}
public int getSize(){
return this.size;
}
//添加
public void add(T ele){
//更新根
this.root=addDG(this.root,ele);
this.size++;
}
//向以root为根的二分搜索树添加元素
private Node addDG(Node root,T ele){
//终止条件
if(root==null){
return new Node(ele);
}
//递归操作
if(root.ele.compareTo(ele)>0){
root.left= addDG(root.left,ele);
}
else{
root.right=addDG(root.right,ele);
}
return root;
}
//查询
public boolean search(T ele){
return searchDG(this.root,ele);
}
private boolean searchDG(Node root,T ele){
//终止
if(root==null){
return false;
}
if(root.ele.compareTo(ele)==0){
return true;
}else if(root.ele.compareTo(ele)>0){
return searchDG(root.left,ele);
}else{
return searchDG(root.right,ele);
}
}
//遍历
public void preTravel() {
//先序
List<T> list = new ArrayList<>();
preTrvelDG(this.root, list);
String res=list.stream().map(item -> item.toString()).collect(Collectors.joining("-"));
System.out.println(res);
}
private void preTrvelDG(Node root,List<T>list){
//先序
if(root==null){
//判断
return ;
}
//操作
list.add(root.ele);
preTrvelDG(root.left,list);
preTrvelDG(root.right,list);
}
public void midTravel() {
//中序
List<T> list = new ArrayList<>();
midTrvelDG(this.root, list);
String res=list.stream().map(item -> item.toString()).collect(Collectors.joining("-"));
System.out.println(res);
}
private void midTrvelDG(Node root,List<T>list){
//中序
if(root==null){
//判断
return ;
}
//操作
midTrvelDG(root.left,list);
list.add(root.ele);
midTrvelDG(root.right,list);
}
public void suffiuxTravel() {
//后序
List<T> list = new ArrayList<>();
suffiuxTrvelDG(this.root, list);
String res=list.stream().map(item -> item.toString()).collect(Collectors.joining("-"));
System.out.println(res);
}
private void suffiuxTrvelDG(Node root,List<T>list){
//后序
if(root==null){
//判断
return ;
}
//操作
suffiuxTrvelDG(root.left,list);
suffiuxTrvelDG(root.right,list);
list.add(root.ele);
}
public static void main(String[] args) {
BinearySearchTree<Integer>bst=new BinearySearchTree<>();
Random random=new Random();
for(int i=0;i<10;i++){
bst.add(random.nextInt(100));
}
System.out.println("先");
bst.preTravel();
System.out.println("中");
bst.midTravel();
System.out.println("后");
bst.suffiuxTravel();
}
}