添加元素
最大最小值
package 树形结构;
import shixianClass02.LinkedList;
import java.util.Iterator;
//二分搜索树
public class BinarySearchThree<E extends Comparable<E>> implements Iterable<E>{
//二分搜索树的结点定义
private class Node{
public E e;
public Node left,right;
public Node(E e){
this.e=e;
left=null;
right=null;
}
}
//二分搜索树的根节点的指针
private Node root;
//二分搜索树的的元素个数
private int size;
//创建一个空的二分搜索树
public BinarySearchThree(){
root=null;
size=0;
}
//获取元素个数
public int size(){
return size;
}
//判断是否为空
public boolean isEmpty(){
return size==0&&root==null;
}
//向二分搜索树添加一个元素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
public boolean contains(E e){
return contains(root,e);
}
//以node为根节点的树中 判断e是否存在
private boolean contains(Node node,E e){
if (node==null){
return false;
}
if (e.compareTo(node.e)==0){
return true;
}
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.print(node.e);
preOrder(node.left);
preOrder(node.right);
}
//前序遍历的非递归实现方式
public void preOrderNR(){
LinkedList<Node> stack=new LinkedList<>();
stack.push(root);
while (!stack.isEmpty()){
Node cur=stack.pop();
System.out.print(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.print(node.e);
inOrder(node.right);
}
//中序遍历的非递归实现方式
public void inOrderNR(){
LinkedList<Node> stack=new LinkedList<>();
Node p=root;
while (p!=null){
stack.push(p);
p=p.left;
}
while (!stack.isEmpty()){
Node cur=stack.pop();
System.out.print(cur.e);
if(cur.right!=null){
p=cur.right;
while (p!=null){
stack.push(p);
p=p.left;
}
}
}
}
//后序遍历
public void postOrder(){
postOrder(root);
}
//后序遍历的递归实现
private void postOrder(Node node){
if (node==null){
return;
}
postOrder(node.left);
postOrder(node.right);
System.out.print(node.e);
}
//后序遍历的非递归实现方式
public void postOrderNR(){
}
//层序遍历
public void levelOrder(){
LinkedList<Node> queue=new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
Node cur=queue.poll();
System.out.print(cur.e);
if (cur.left!=null){
queue.offer(cur.left);
}
if (cur.right!=null){
queue.offer(cur.right);
}
}
}
//返回二分搜索树中的最小值
public E minnum(){
/* 迭代方法
if(isEmpty()){
throw new IllegalArgumentException("BST is empty");
}
Node p=root;
while(p.left!=null){
p=p.left;
}
return p.e;
*/
//递归实现方式
if(isEmpty()){
throw new IllegalArgumentException("BST is empty");
}
return minnum(root).e;
}
//返回以node为根节点的二分搜索树中最小值得结点
private Node minnum(Node node){ //o(logn)
if(node.left==null){
return node;
}
return minnum(node.left);
}
//返回二分搜索树中的最大值
public E maxnum(){
if(isEmpty()){
throw new IllegalArgumentException("BST is empty");
}
return maxnum(root).e;
}
//返回以node为根节点的二分搜索树中最大值得结点
private Node maxnum(Node node) {
if(node.right==null){
return node;
}
return maxnum(node.right);
}
//删除最小值
public E removeMin(){
E ret=minnum();
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 removeMax(){
E ret=maxnum();
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=removeMin(node.right);
return node;
}
//删除二分搜索树中的任意值e
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=minnum(node.right);
successor.right=removeMin(node.right);
successor.left=node.left;
node.left=node.right=null;
return successor;
}
}
@Override
public String toString() {
if(isEmpty()){
return "[]";
}
StringBuilder sb=new StringBuilder();
inOrderByString(root,sb);
return sb.toString();
}
private void inOrderByString(Node node,StringBuilder sb) {
if(node==null){
return;
}
inOrderByString(node.left,sb);
sb.append(node.e+"");
inOrderByString(node.right,sb);
}
@Override
public Iterator<E> iterator() {
return new BSTIterator();
}
private class BSTIterator implements Iterator<E>{
private LinkedList<E> list=new LinkedList<>();
public BSTIterator(){
LinkedList<Node> stack=new LinkedList<Node>();
Node p=root;
while (p!=null){
stack.push(p);
p=p.left;
}
while (!stack.isEmpty()){
Node cur=stack.pop();
list.add(cur.e);
if(cur.right!=null){
p=cur.right;
while (p!=null){
stack.push(p);
p=p.left;
}
}
}
}
@Override
public boolean hasNext() {
return !list.isEmpty();
}
@Override
public E next() {
return list.removeFirst();
}
}
}
测试
package ceshi;
import 查找算法.BinarySearch;
import 树形结构.BinarySearchThree;
public class TestBST {
public static void main(String[] args) {
BinarySearchThree<Integer> tree=new BinarySearchThree<>();
tree.add(5);
tree.add(2);
tree.add(4);
tree.add(6);
tree.add(3);
tree.add(1);
System.out.println(tree.contains(6));
System.out.println(tree.contains(8));
tree.preOrder();
System.out.println("=========");
tree.inOrder();
System.out.println("=========");
tree.postOrder();
System.out.println("=========");
tree.preOrderNR();
System.out.println("=========");
tree.inOrderNR();
System.out.println("=========");
tree.levelOrder();
System.out.println("=========");
System.out.println("min:"+tree.minnum());
System.out.println(tree.removeMin());
System.out.println("min:"+tree.minnum());
System.out.println("max:"+tree.maxnum());
System.out.println(tree.removeMax());
System.out.println("max:"+tree.maxnum());
tree.inOrderNR();
System.out.println();
tree.remove(3);
System.out.println(tree);
for(Integer num:tree){
System.out.println(num+"");
}
}
}
执行结果
true
false
521436=========
123456=========
134265=========
521436=========
123456=========
526143=========
min:1
1
min:2
max:6
6
max:5
2345
245
2
4
5