#二分搜索树是二叉树
#二分搜索树的每个节点的值:
1.大于左子树的所有节点的值
2.小于右子树的所有节点的值
#存储的元素要具有可比较性。例如:如果存储学生对象,可以比较学号。
public class BST> { private Node root;
private int size;
private class Node {
public E e;
public Node left, right;
public Node(E e) {
this.e = e;
this.left = null;
this.right = null;
}
}
public BST() {
this.root = null;
this.size = 0;
}
public int getSize() {
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.left = new Node(e);
size++;
return;
}
//递归调用
if (e.compareTo(node.e) < 0)
add(node.left, e);
else//e.compareTo(node.e) > 0
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 //e.compareTo(node.e) > 0
return contains(node.right, e);
}
public void preorderTraversal() {
preorderTraversal(root);
}
private void preorderTraversal(Node node) {
if (node == null)
return;
System.out.println(node.e);
preorderTraversal(node.left);
preorderTraversal(node.right);
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
generateBSTString(root, 0, res);
return res.toString();
}
private void generateBSTString(Node node, int depth, StringBuilder res) {
if (node == null) {
res.append(generateDepthString(depth) + "Null\n");
return;
}
res.append(generateDepthString(depth)+node.e + "\n");
generateBSTString(node.left,depth+1,res);
generateBSTString(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();
}
public void intermediateTraversal(){
intermediateTraversal(root);
}
private void intermediateTraversal(Node node) {
if (node==null)
return;
intermediateTraversal(node.left);
System.out.println(node.e);
intermediateTraversal(node.right);
}
public void postorderTraversal(){
postorderTraversal(root);
}
private void postorderTraversal(Node node) {
if (node==null)
return;
postorderTraversal(node.left);
postorderTraversal(node.right);
System.out.println(node.e);
}
说明:
1.先将根节点压入栈,出栈,记录,并将左右孩子节点压入(先压入右孩子节点,后压入左孩子节点)
2.出栈(即左孩子节点(栈顶元素)先出栈),记录,并查看左孩子节点是否有孩子节点,仍然是右孩子节点先压入栈,然后压入左孩子节点。如果没有孩子,则出栈现在栈顶元素(右孩子节点),记录。
3.循环以上过程
如下图:
28入栈,出栈,记录。30,16入栈,16为栈顶,出栈,记录,并将16的左右孩子节点13和22入栈。13为栈顶,出栈,此时13没有左右孩子节点,22出栈,记录。22没有左右孩子,30出栈,记录。
压入30的左右孩子节点。此时,栈顶元素为30的左孩子节点,即29,29出栈,记录。由于29没有孩子节点,42出栈,记录。42没有孩子节点,并且此时栈为空,前序遍历结束。
代码实现:
public void preorderTraversalNR() {
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);
}
}
由于使用了java中的栈,需要在代码前加:
import java.util.Stack;
说明:(使用辅助数据结构:队列)
1.先将根节点入队,出队,记录。并将根节点的左右孩子节点入队(左孩子节点先入队,右孩子节点后入队)
2.入队完成后,队首元素出队,记录,并将其左右孩子入队(仍然按照左先右后的顺序)
3.由于队列先进先出的性质,每次都是左孩子节点先出队,右孩子后出队。并且左孩子节点和右孩子节点都入队,左孩子节点出队后,压入左孩子节点的左右孩子节点之前,上一层右孩子节点为队首,所以下层的左右孩子节点在上层的右孩子节点之后,所以可以做到层序遍历。
如下图:
根节点28入队,出队并记录,并将左右孩子节点16,30入队。
左孩子节点16出队,记录。并将16的左右孩子节点13,22入队。
此时30位队首元素,出队,记录,并入队30的左右孩子节点。
此时队首元素为13,出队,记录,入队13的左右孩子节点,由于没有孩子节点,所以此时什么都不做。然后去队首,出队队首元素22,没有孩子节点,出队,记录,没有孩子节点,什么都不做。出队队首元素29,记录,没有孩子节点,什么都不做。出队队首元素42,记录。没有孩子节点,什么都不做。出队队首元素,由于此时队列为空,遍历结束。
public void levelTraversal(){
Queue 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);
}
}
最小值:一直向左一直到null之前
最大值:一直向右一直到null之前
public E minimumValue(){
if (size == 0)
throw new IllegalArgumentException("BST is Empty!");
return minimumValue(root);
}
private E minimumValue(Node node) {
if (node.left==null)
return node.e;
return minimumValue(node.left);
}
#查找最小值(非递归)
public E minimumValueNR() {
if (size == 0)
throw new IllegalArgumentException("BST is Empty!");
Node cur = root;
while (cur.left!=null)
cur = cur.left;
return cur.e;
}
#查找最大值(递归)
public E maximumValue(){
if (size == 0)
throw new IllegalArgumentException("BST is Empty!");
return maximumValue(root);
}
private E maximumValue(Node node) {
if (node.right==null)
return node.e;
return maximumValue(node.right);
}
#查找最大值(非递归)
public E maximumValueNR() {
if (size == 0)
throw new IllegalArgumentException("BST is Empty!");
Node cur = root;
while (cur.right != null)
cur = cur.right;
return cur.e;
}
#删除最小值
情况1:最小值为叶子节点(如下图)
操作:直接删除就好了
情况2:最小值为非叶子节点(如下图)
操作:将删除节点的右子树变成删除节点的父亲节点的左子树
情况2删除后:(如下图)
#删除最小值(递归)
public E removeMin() {
E ret = minimumValueNR();
root = removeMin(root);
return ret;
}
//删除以node为根的最小值
//返回删除节点后新的二分搜索树的跟
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 removeMinNR() {
E ret = minimumValueNR();
Node cur = root;
if (cur.left == null) {
Node rightNode = cur.right;
cur.right = null;
root = rightNode;
size--;
return ret;
}
while (cur.left.left != null) {
cur = cur.left;
}
if (cur.left.right != null) {
Node rightNode = cur.left.right;
cur.left = rightNode;
} else
cur.left = null;
size--;
return ret;
}
#删除最大值
情况1:最大值为叶子节点(如下图)
操作:直接删除就好了
情况二:最大值为非叶子节点(如下图)
操作:将删除节点的左子树变成删除节点的父亲节点的右子树
情况2删除后(如下图):
#删除最大值(递归)
public E removeMax() {
E ret = maximumValueNR();
root = removeMax(root);
return ret;
}
//删除以node为根的最大值
//返回删除节点后新的二分搜索树的跟
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 E removeMaxNR() {
E ret = maximumValueNR();
Node cur = root;
if (cur.right == null) {
Node leftNode = cur.left;
cur.left = null;
root = leftNode;
size--;
return ret;
}
while (cur.right.right != null)
cur = cur.right;
if (cur.right.left != null) {
Node leftNode = cur.right.left;
cur.right = leftNode;
} else
cur.right = null;
size--;
return ret;
}
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 {//e.compareTo(node.e) == 0
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 = minimumValue(node.right);
successor.right = removeMin(node.right);
successor.left = node.left;
node.left = node.right = null;
return successor;
}
}
package com.cc;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
* @Author: 李立建
* @Date: 2019/7/26 22:03
* @Version 1.0
*/
public class BST> {
private Node root;
private int size;
private class Node {
public E e;
public Node left, right;
public Node(E e) {
this.e = e;
this.left = null;
this.right = null;
}
@Override
public String toString() {
return e.toString();
}
}
public BST() {
this.root = null;
this.size = 0;
}
public int getSize() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
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 //e.compareTo(node.e) > 0
return contains(node.right, e);
}
public void preorderTraversal() {
preorderTraversal(root);
}
private void preorderTraversal(Node node) {
if (node == null)
return;
System.out.println(node.e);
preorderTraversal(node.left);
preorderTraversal(node.right);
}
public void preorderTraversalNR() {
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 intermediateTraversal() {
intermediateTraversal(root);
}
private void intermediateTraversal(Node node) {
if (node == null)
return;
intermediateTraversal(node.left);
System.out.println(node.e);
intermediateTraversal(node.right);
}
public void postorderTraversal() {
postorderTraversal(root);
}
private void postorderTraversal(Node node) {
if (node == null)
return;
postorderTraversal(node.left);
postorderTraversal(node.right);
System.out.println(node.e);
}
public void levelTraversal() {
Queue 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 E minimumValue() {
if (size == 0)
throw new IllegalArgumentException("BST is Empty!");
return minimumValue(root).e;
}
private Node minimumValue(Node node) {
if (node.left == null)
return node;
return minimumValue(node.left);
}
public E minimumValueNR() {
if (size == 0)
throw new IllegalArgumentException("BST is Empty!");
Node cur = root;
while (cur.left != null)
cur = cur.left;
return cur.e;
}
public E maximumValue() {
if (size == 0)
throw new IllegalArgumentException("BST is Empty!");
return maximumValue(root);
}
private E maximumValue(Node node) {
if (node.right == null)
return node.e;
return maximumValue(node.right);
}
public E maximumValueNR() {
if (size == 0)
throw new IllegalArgumentException("BST is Empty!");
Node cur = root;
while (cur.right != null)
cur = cur.right;
return cur.e;
}
public E removeMin() {
E ret = minimumValueNR();
root = removeMin(root);
return ret;
}
public 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 removeMinNR() {
E ret = minimumValueNR();
Node cur = root;
if (cur.left == null) {
Node rightNode = cur.right;
cur.right = null;
root = rightNode;
size--;
return ret;
}
while (cur.left.left != null) {
cur = cur.left;
}
if (cur.left.right != null) {
Node rightNode = cur.left.right;
cur.left = rightNode;
} else
cur.left = null;
size--;
return ret;
}
public E removeMax() {
E ret = maximumValueNR();
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;
}
public E removeMaxNR() {
E ret = maximumValueNR();
Node cur = root;
if (cur.right == null) {
Node leftNode = cur.left;
cur.left = null;
root = leftNode;
size--;
return ret;
}
while (cur.right.right != null)
cur = cur.right;
if (cur.right.left != null) {
Node leftNode = cur.right.left;
cur.right = leftNode;
} else
cur.right = null;
size--;
return ret;
}
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 {//e.compareTo(node.e) == 0
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 = minimumValue(node.right);
successor.right = removeMin(node.right);
successor.left = node.left;
node.left = node.right = null;
return successor;
}
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
generateBSTString(root, 0, res);
return res.toString();
}
private void generateBSTString(Node node, int depth, StringBuilder res) {
if (node == null) {
res.append(generateDepthString(depth) + "Null\n");
return;
}
res.append(generateDepthString(depth) + node.e + "\n");
generateBSTString(node.left, depth + 1, res);
generateBSTString(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();
}
}