概念
- 二分搜索树:对有一个树的任何结点,其右子树的值总是大于当前结点的值,
- 其左子树的值总是小于当前结点的值。
- 注意:线性表,队列,栈都可以存重复元素,树最好不要存。
- 功能实现
- 1.判断当前树的长度
- 2.判断当前树是否为空
- 3.给树添加元素,两种实现方法,一种是迭代法,一种是递归。
- 4.查找元素是否包含在树种
- 5.中序遍历
- 6.广度优先遍历(队列实现)
- 7.获取最小值
- 8.获取最大值
- 9.删除最小值结点
- 10。删除最大值结点
- 11.删除指定元素。
- 12.打印这个树。重写tostring
- 13.二叉搜索树迭代器
代码实现
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class BinarySearchTree<E extends Comparable<E>> {
private class Node {
public E e;
public Node left, right;
public Node(E e) {
this.e = e;
this.left = null;
this.right = null;
}
}
public Node root;
public int size;
public BinarySearchTree() {
this.root = null;
size = 0;
}
public int size() {
return size;
}
public boolean isempty() {
return size() == 0;
}
public void add(E e) {
root = add(root, e);
}
private BinarySearchTree<E>.Node add(BinarySearchTree<E>.Node node, E e) {
if(node==null) {
size++;
return new Node(e);
}
if (e.compareTo(node.e) > 0) {
node.right = add(node.right, e);
} else if (e.compareTo(node.e) < 0) {
node.left = add(node.left, e);
}
return node;
}
public boolean contains(E e) {
return contains(root,e);
}
private boolean contains(BinarySearchTree<E>.Node node, E e) {
if(node==null) {
return false;
}
if(e.compareTo(node.e)>0) {
return contains(node.right,e);
}else if(e.compareTo(node.e)<0){
return contains(node.left,e);
}else {
return true;
}
}
public void inOrder() {
List list=new ArrayList<E>();
inOrder(root,list);
System.out.println(list);
}
private void inOrder(BinarySearchTree<E>.Node node, List list) {
if(node==null) {
return;
}
inOrder(node.left,list);
list.add(node.e);
inOrder(node.right,list);
}
public void levelOrder() {
ArrayList<E> list = new ArrayList<E>();
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while (!queue.isEmpty()) {
Node cur = queue.poll();
list.add(cur.e);
if (cur.left != null) {
queue.add(cur.left);
}
if (cur.right != null) {
queue.add(cur.right);
}
}
System.out.println(list);
}
public E min() {
if(size==0) {
throw new IllegalArgumentException("空树");
}
return min(root).e;
}
private Node min(Node node) {
if(node.left==null) {
return node;
}
return min(node.left);
}
public E max() {
if(size==0) {
throw new IllegalArgumentException("空树");
}
return max(root).e;
}
private Node max(Node node) {
if(node.right==null) {
return node;
}
return max(node.right);
}
public E removemin() {
E e=min();
root=removemin(root);
return e;
}
private Node removemin(Node node) {
if(node.left==null) {
Node rightnode=node.right;
node.right=null;
size--;
return rightnode;
}
node.left=removemin(node.left);
return node;
}
public E removemax() {
E e=max();
root=removemax(root);
return e;
}
private Node removemax(Node node) {
if(node.right==null) {
Node leftnode=node.left;
node.left=null;
size--;
return leftnode;
}
node.right=removemax(node.right);
return node;
}
public void remove(E e) {
root=remove(root,e);
}
private Node remove(Node node, E e) {
if(node==null) {
return null;
}
if(e.compareTo(node.e)>0) {
node.right=remove(node.right, e);
return node;
}else if(e.compareTo(node.e)<0) {
node.left=remove(node.left, e);
return node;
}else {
if(node.left==null) {
Node rightnode=node.right;
node.right=null;
size--;
return rightnode;
}if(node.right==null) {
Node leftnode=node.left;
node.left=null;
size--;
return leftnode;
}
Node temp=min(node.right);
temp.right=removemin(node.right);
temp.left=node.left;
node.left=node.right=null;
return temp;
}
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder();
BSTlterator bs=new BSTlterator(root);
while(bs.hasnext()) {
sb.append(bs.next()+"\n");
}
return sb.toString();
}
private void print(Node node, int level, StringBuilder sb) {
if(node==null) {
sb.append(printlevel(level)+"00\n");
}else {
print(node.left,level+1,sb);
sb.append(printlevel(level)+node.e+"\n");
print(node.right,level+1,sb);
}
}
private String printlevel(int level) {
StringBuilder sb2=new StringBuilder();
for(int i=0;i<level;i++) {
sb2.append(" ");
}
return sb2.toString();
}
class BSTlterator{
LinkedList<Node> stack=new LinkedList<>();
public BSTlterator(Node root) {
Node cur=root;
while(cur!=null) {
stack.push(cur);
cur=cur.left;
}
}
public E next() {
Node n=stack.pop();
Node cur=n.right;
while(cur!=null) {
stack.push(cur);
cur=cur.left;
}
return n.e;
}
public boolean hasnext() {
return !stack.isEmpty();
}
}
}
二分搜索树的时间复杂度为O(log(2,n))