不能添加重复元素
package set;
import tree.BST;
public class BSTSet<E extends Comparable<E>>implements Set<E> {
private BST<E>bst;
public BSTSet(){
bst = new BST<>();
}
@Override
public void add(E e) {
bst.add(e);
}
@Override
public void remove(E e) {
bst.remove(e);
}
@Override
public boolean contains(E e) {
return bst.contains(e);
}
@Override
public int getSize() {
return bst.size();
}
@Override
public boolean isEmpty() {
return bst.isEmpty();
}
}
package set;
public interface Set<E> {
void add(E e);
void remove(E e);
boolean contains(E e);
int getSize();
boolean isEmpty();
}
package tree;
import java.util.*;
//具有可比性的泛型
public class BST<E extends Comparable> {
private class Node {
public E e;
private Node left;
private Node 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;
}
// 向二分搜索树中添加新的元素
// public void add(E e){
// if (root == null){
// root = new Node(e);
// size++;
// }else {
// add(root, e);
// }
// }
//向以node为根的二分搜索树中插入元素 递归算法
// 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 add(E e) {
root = add(root, 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;
}
// 是否包含某个节点
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 proOrder() {
preOrder(root);
}
private void preOrder(Node node) {
if (node == null) {
return;
}
System.out.println(node.e);
preOrder(node.left);
preOrder(node.right);
}
//遍历
//先序遍历根左右 非递归算法
public void proOrderNR() {
Stack<Node> 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);
}
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 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 maxmum() {
if (size == 0)
throw new IllegalArgumentException("BST is empty");
return maxmum(root).e;
}
private Node maxmum(Node node) {
if (node.right == null)
return node;
return maxmum(node.right);
}
//删除二分搜索树的最小值,返回最小值
public E removeMin() {
E ret = minimum();
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;
}
//删除最大值
public E removeMax() {
E ret = maxmum();
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;
}
//删除任意节点
public void remove(E e) {
root = remove(root, e);
}
//删除以node为根的二分搜索树中值为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;
}
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
generateBSTString(root, 0, stringBuilder);
return stringBuilder.toString();
}
// 以node为根节点,深度为depth描述二叉树的字符串
private void generateBSTString(Node node, int depth, StringBuilder stringBuilder) {
if (node == null) {
stringBuilder.append(generateBSTString(depth) + "null\n");
return;
}
stringBuilder.append(generateBSTString(depth) + node.e + "\n");
generateBSTString(node.left, depth + 1, stringBuilder);
generateBSTString(node.right, depth + 1, stringBuilder);
}
private String generateBSTString(int depth) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < depth; i++) {
stringBuilder.append("--");
}
return stringBuilder.toString();
}
//层序遍历(广度)
public void levelOrder() {
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (!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 static void main(String[] args) {
BST<Integer> bst = new BST<>();
Random random = new Random();
int n = 1000;
for (int i = 0; i < n; i++) {
bst.add(random.nextInt(10000));
}
ArrayList<Integer> nums = new ArrayList<>();
while (!bst.isEmpty()) {
nums.add(bst.removeMin());
}
System.out.println(nums);
for (int i = 1; i < nums.size(); i++) {
if (nums.get(i - 1) > nums.get(i))
throw new IllegalArgumentException("Error");
}
System.out.println("removeMin test completed");
ArrayList<Integer> nums1 = new ArrayList<>();
while (!bst.isEmpty()) {
nums1.add(bst.removeMax());
}
System.out.println(nums);
for (int i = 1; i < nums1.size(); i++) {
if (nums.get(i - 1) < nums1.get(i))
throw new IllegalArgumentException("Error");
}
System.out.println("removeMin test completed");
}
}