import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class MyBinarySearchTree<A extends Comparable<? super A>> {
private NodeType<A> root;
Integer num1 = 0;
protected static class NodeType<A1 extends Comparable<? super A1>> extends MyBinarySearchTree{
protected A1 element;
protected NodeType<A1> leftNode;
protected NodeType<A1> rightNode;
protected int num;
protected NodeType(A1 element) {
this(element, null, null);
}
private NodeType(A1 element, NodeType<A1> leftNode, NodeType<A1> rightNode) {
super();
this.element = element;
this.leftNode = leftNode;
this.rightNode = rightNode;
this.num = 0;
MyBinarySearchTree<A1> myTree = new MyBinarySearchTree<>();
System.out.println(myTree.root);
System.out.println(num1);
// System.out.println();
}
}
public NodeType<A> getter(){
return root;
}
public Boolean isEmpty() {
return root == null;
}
public Map<Boolean, NodeType<A>> setEmpty() {
Map<Boolean, NodeType<A>> returnEmpty = new ConcurrentHashMap<>(1);
try {
root = null;
returnEmpty.clear();
returnEmpty.put(true, root);
return returnEmpty;
} catch (Exception e) {
e.printStackTrace();
returnEmpty.clear();
returnEmpty.put(false, root);
return returnEmpty;
}
}
public ReturnTwo<Boolean,NodeType<A>> contains(A itemToFind){
return contains(itemToFind,root);
}
private ReturnTwo<Boolean,NodeType<A>> contains(A itemToFind,NodeType<A> rootNow){
ReturnTwo<Boolean,NodeType<A>> returnTwo = new ReturnTwo<>(false,null);
if (rootNow== null){
return returnTwo;
}
int flag = itemToFind.compareTo(rootNow.element) >= 0? 1:0;
if (flag == 1){
flag= itemToFind.compareTo(rootNow.element) > 0? 1:-1;
}
switch (flag){
case -1:
returnTwo.a = true;
returnTwo.b = rootNow;
break;
case 0:
return contains(itemToFind,rootNow.leftNode);
case 1:
return contains(itemToFind,rootNow.rightNode);
}
return returnTwo;
}
public NodeType<A> findMax(){
return findMax(root);
}
private NodeType<A> findMax(NodeType<A> rootNow){
if (rootNow == null){
throw new NullPointerException();
}
if (rootNow.rightNode == null){
return rootNow;
}
return findMax(rootNow.rightNode);
}
public NodeType<A> findMin(){
return findMin(root);
}
private NodeType<A> findMin(NodeType<A> rootNow){
if (rootNow == null){
throw new NullPointerException();
}
if (rootNow.leftNode == null){
return rootNow;
}
return findMin(rootNow.leftNode);
}
public Map<Boolean,NodeType<A>> insert(A elementToInsert){
Map<Boolean,NodeType<A>> mapFinal = insert(elementToInsert,root);
List<NodeType<A>> valuesOfNode = new ArrayList<>(mapFinal.values());
// List<Boolean> valuesOfBoolean = new ArrayList<>(mapFinal.keySet());
root = valuesOfNode.get(0);
return mapFinal;
}
private Map<Boolean,NodeType<A>> insert(A elementToInsert,NodeType<A> rootNow){
Map<Boolean,NodeType<A>> returnMap = new ConcurrentHashMap<>(1);
// (List<NodeType<A>>)(returnMap.values())
try {
if (rootNow== null){
// rootNow.element= elementToInsert;
rootNow = new NodeType<A>(elementToInsert,null,null);
returnMap.clear();
returnMap.put(true,rootNow);
}
int flag= elementToInsert.compareTo(rootNow.element) >= 0? 1:0;
if (flag== 1){
flag= elementToInsert.compareTo(rootNow.element) > 0? 1:-1;
}
switch (flag){
case -1:
rootNow.num += 1; //计数器加一
returnMap.clear();
returnMap.put(true,rootNow);
break;
case 0:
Map<Boolean,NodeType<A>> leftMap = insert(elementToInsert,rootNow.leftNode);
List<NodeType<A>> valuesOfNode = new ArrayList<>(leftMap.values()); //这个直接转 ide没报错但是编译时错了
// Set<Boolean> valueOfBoolean = (Set<Boolean>)leftMap.keySet();
List<Boolean> valuesOfBoolean = new ArrayList<>(leftMap.keySet());
rootNow.leftNode = valuesOfNode.get(0);
returnMap.clear();
returnMap.put(valuesOfBoolean.get(0),rootNow);
break;
case 1:
Map<Boolean,NodeType<A>> rightMap = insert(elementToInsert,rootNow.rightNode);
List<NodeType<A>> valuesOfNode1 = new ArrayList<>(rightMap.values());
// Set<Boolean> valueOfBoolean = (Set<Boolean>)leftMap.keySet();
List<Boolean> valuesOfBoolean1 = new ArrayList<>(rightMap.keySet());
rootNow.rightNode = valuesOfNode1.get(0);
returnMap.clear();
returnMap.put(valuesOfBoolean1.get(0),rootNow);
break;
}
return returnMap;
}catch (Exception e){
e.printStackTrace();
returnMap.clear();
returnMap.put(false,rootNow);
throw new NullPointerException("对null查询了其对象属性!");
// return returnMap;
}
}
public void remove(A elementToRemove){
root = remove(elementToRemove,root);
}
private NodeType<A> remove(A elementToRemove,NodeType<A> rootNow){ // 三种情况 一种只有一个null 一种两个null 一种两个都不是null(找最小值)
if (rootNow == null){
throw new NullPointerException(String.format("当前元素为空,没有%s存在",String.valueOf(elementToRemove)));
}
int flag= elementToRemove.compareTo(rootNow.element) >= 0? 1:0;
if (flag== 1){
flag= elementToRemove.compareTo(rootNow.element) > 0? 1:-1;
}
switch (flag){
case -1:
if ((rootNow.leftNode == null) && (rootNow.rightNode == null)){
return null;
}
else if ((rootNow.leftNode != null) && (rootNow.rightNode != null)){
NodeType<A> minInLeftNode = findMin(rootNow.rightNode);
rootNow.element = minInLeftNode.element;
rootNow = remove(minInLeftNode.element,rootNow);
return rootNow;
}
else { //存在一边为null 一边不为null的情况
rootNow = rootNow.leftNode == null ? rootNow.rightNode : rootNow.leftNode;
return rootNow;
}
case 0:
rootNow.leftNode= remove(elementToRemove,rootNow.leftNode);
break;
case 1:
rootNow.rightNode= remove(elementToRemove,rootNow.rightNode);
break;
}
return rootNow;
}
public MyBinarySearchTree() {
root = null;
}
}
class ReturnTwo<A,B>{
A a;
B b;
public ReturnTwo(A a,B b){
this.a= a;
this.b= b;
}
}
java实现二叉搜索树
最新推荐文章于 2022-03-25 17:24:17 发布