import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BST> {
private class Node {
public E e;
private Node left, right;
public Node(E e) {
this.e = e;
left = null;
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;
}
/**
* 向二分搜索树中添加一个新的元素e
*
* @param e
*/
public void add(E e) {
root = add(root, e);
}
//向以node为根的二分搜索树中插入元素E,递归算法
//返回插入新节点后二分搜索树的根
private Node add(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;
}
/**
* 看二分搜素树中是否包含元素e
*
* @param e
* @return
*/
public boolean contains(E e) {
return contains(root, e);
}
/**
* 看以node为根的二分搜索树中是否包含元素e,递归算法
*
* @param node
* @param e
* @return
*/
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);
}
//二分搜索树的前序遍历(非递归方式)
public void preOrderNR() {
Stack stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
Node cur = stack.pop();
System.out.println(cur.e);
if (cur.right != null) {
stack.push(cur.right);
}
if (cur.left != null) {
stack.push(cur.left);
}
}
}
//二分搜索树的中序遍历
public void inOrder() {
inOrder(root);
}
//二分搜索树的后序遍历
public void postOrder() {
postOrder(root);
}
public void levelOrder() {
Queue q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
Node cur = q.remove();
System.out.println(cur.e);
if (cur.left != null) {
q.add(cur.left);
}
if (cur.right != null) {
q.add(cur.right);
}
}
}
/**
* 前序遍历以Node为根的二分搜索树,递归算法
*
* @param node
*/
private void preOrder(Node node) {
if (node == null) {
return;
}
System.out.println(node.e);
preOrder(node.left);
preOrder(node.right);
}
/**
* 中序遍历以Node为根的二分搜索树,递归算法
*
* @param node
*/
private void inOrder(Node node) {
if (node == null) {
return;
}
inOrder(node.left);
System.out.println(node.e);
inOrder(node.right);
}
/**
* 后序遍历以Node为根的二分搜索树,递归算法
*
* @param node
*/
private void postOrder(Node node) {
if (node == null) {
return;
}
postOrder(node.left);
postOrder(node.right);
System.out.println(node.e);
}
/**
* 寻找二分搜索树中的最小元素
*
* @return
*/
public E minmun() {
if (size == 0) {
throw new IllegalArgumentException("BST IS EMPTY!");
}
Node node = minmun(root);
return node.e;
}
private Node minmun(Node node) {
if (node != null && node.left == null) {
return node;
}
return minmun(node.left);
}
/**
* 寻找二分搜索树中的最大元素
*
* @return
*/
public E maxmun() {
if (size == 0) {
throw new IllegalArgumentException("BST IS EMPTY!");
}
Node node = maxmun(root);
return node.e;
}
private Node maxmun(Node node) {
if (node != null && node.right == null) {
return node;
}
return maxmun(node.right);
}
/**
* 从二分搜索树中删除最小值所在的节点,返回最小值
*
* @return
*/
public E removeMin() {
E ret = minmun();
root = removeMin(root);
return ret;
}
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;
}
/**
* 从二分搜索树中删除最大值所在的节点,返回最大值
*
* @return
*/
public E removeMax() {
E ret = maxmun();
root = removeMax(root);
return ret;
}
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;
}
/**
* 从二分搜索树中删除元素为e的节点
*
* @param e
* @return
*/
public void remove(E e) {
root = remove(root, e);
}
/**
* 从二分搜索树中删除元素为e的节点
*
* @param node
* @param e
* @return
*/
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 = minmun(node.right);
successor.right = removeMin(node.right);
successor.left = node.left;
node.left = node.left = null;
return successor;
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
generateBSTStr(root, 0, res);
return res.toString();
}
/**
* 生成以Node为节点,深度为depth的描述二叉树的字符串
*
* @param node
* @param depth
* @param res
*/
private void generateBSTStr(Node node, int depth, StringBuilder res) {
if (node == null) {
res.append(generateDepthString(depth) + "null\n");
return;
}
res.append(generateDepthString(depth) + node.e + "\n");
generateBSTStr(node.left, depth + 1, res);
generateBSTStr(node.right, depth + 1, res);
}
private String generateDepthString(int depth) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < depth; i++) {
res.append("--");
}
return res.toString();
}
}
import java.util.ArrayList;
import java.util.Random;
public class Main {
public static void main(String[] args) {
BST bst = new BST<>();
/*int[] nums = {5, 3, 6, 8, 4, 2};
for (int num : nums) {
bst.add(num);
}
// bst.preOrder();
System.out.println();
// bst.inOrder();
//bst.postOrder();
//System.out.println(bst);
//bst.preOrderNR();
bst.levelOrder();*/
Random random = new Random();
int n=1000;
for (int i=0;i nums=new ArrayList<>();
while (!bst.isEmpty()){
nums.add(bst.removeMax());
}
System.out.println(nums);
}
}