public class BST<E extends Comparable<E>>{
private class Node{
public E e;
public Node left;
public Node right;
public Node(E e){
this.e = e;
this.left = null;
this.right = null;
}
}
private Node root;
private int size;
public BST(){
root=null;
size=0;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public void add(E e){
if(root==null){
root = new Node(e);
size++;
}else{
add(root,e);
}
}
private void add(Node node,E e){
if(e.equals(node.e)){
return;
}else if(e.compareTo(node.e)<0&&node.left==null){
node.left=new Node(e);
size++;
return;
}else if(e.compareTo(node.e)>0&&node.right==null){
node.right=new Node(e);
size++;
return;
}
if(e.compareTo(node.e)<0){
add(node.left,e);
}else{
add(node.right,e);
}
}
public void add2(E e){
root = add(root,e);
}
private Node add2(Node node,E e){
if(node==null){
size++;
return new Node(e);
}
if(e.compareTo(node.e)<0){
node.left = add(node.left,e);
}else if(e.compareTo(node.e)>0){
node.right = add(node.right,e);
}
return node;
}
public boolean contains(E e){
return contains(root,e);
}
private boolean contains(Node node,E e){
if(node == null){
return false;
}
if(e.compareTo(node.e)==0){
return true;
}else if(e.compareTo(node.e)<0){
return contains(node.left,e);
}else{
return contains(node.right,e);
}
}
// 前序遍历
public void preOrder(){
preOrder(root);
}
private void preOrder(Node node){
if(node==null){
return;
}
System.out.println(node.e);
preOrder(node.left);
preOrder(node.right);
}
// 前序遍历 非递归
public void preOrderNR(){
Stack<Node> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
Node cur = stack.pop();
System.out.ptintln(cur.e);
if(cur.right!=null){
stack.push(cur.right);
}
if(cur.left!=null){
stack.push(cur.left);
}
}
}
// 中序遍历
public void inOrder(){
inOrder(root);
}
private void inOrder(Node node){
if(node==null){
return;
}
inOrder(node.left);
System.out.println(node.e);
inOrder(node.right);
}
// 后序遍历
public void postOrder(){
postOrder(root);
}
private void postOrder(Node node){
if(node==null){
return;
}
postOrder(node.left);
postOrder(node.right);
System.out.println(node.e);
}
// 层序遍历
public void levelOrder(){
Queue<Node> queue = new LinkedList<>();
queue.add(root);
whilie(!queue.isEmpty()){
Node cur = queue.remove();
System.out.println(cur.e);
if(cur.left!=null){
queue.add(cur.left);
}
if(cur.right!=null){
queue.add(cur.right);
}
}
}
// 最小元素
public E minimum(){
if(size==0){
throw new IllegalArgumentException("BST is empty");
}
return minimum(root).e;
}
private Node minimum(Node node){
if(node.left==null){
return node;
}
return minimum(node.left);
}
public E removeMin(){
E ret = minimum();
removeMin(root);
return ret;
}
privete 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 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.left=remove(node.left,e);
return node;
}else if(e.compareTo(node.e)>0){
node.right = remove(node.right,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 successor = minimum(node.right);
successor.right=removeMin(node.right);
successor.left=node.left;
node.left=node.right=null;
return successor;
}
}
}