写在前面
前两天写完红黑树的分析之后,趁热打铁写了下红黑树的部分实现代码,因删除部分比较复杂,所以这次只涉及查找和插入,删除部分过两天有时间了再写.
具体包已经上传,免费下载RedBlackTree.zip
代码部分
实现的功能比较简单,就是查找,插入和输出.
我基本上把代码的地方都测过了,但水平有限,难免有疏漏,欢迎指正.
运行环境:
jdk1.8
intellij idea ultimate 2020.2
左旋异常:
package com.test.Tree;
/**
* @author: cuijr
* @email:
* @date: 2020-11-10 15:30
* @description:
*/
public class CanNotTurnLeft extends Exception{
public CanNotTurnLeft(String s) {
super(s);
}
}
右旋异常:
package com.test.Tree;
/**
* @author: cuijr
* @email:
* @date: 2020-11-10 15:30
* @description:
*/
public class CanNotTurnRight extends Exception{
public CanNotTurnRight(String s) {
super(s);
}
}
红黑树结构异常:
package com.test.Tree;
/**
* @author: cuijr
* @email:
* @date: 2020-11-10 17:07
* @description:
*/
public class TreeIsNotValidException extends Exception{
public TreeIsNotValidException(String s) {
super(s);
}
}
树的节点类:
树的节点类中主要包含:
left 左子节点
right 右子节点
parent 父节点
key 键
value 值
isRed 是不是红色节点
isRoot 是不是根节点
isLeft 是不是父节点的左子节点
package com.test.Tree;
/**
* @author: cuijr
* @email:
* @date: 2020-11-10 15:08
* @description:
*/
public class TreeNode<V> {
private TreeNode<V> left;
private TreeNode<V> right;
private TreeNode<V> parent;
private int key;
private V value;
private boolean isRed;
private boolean isRoot;
private boolean isLeft;
public TreeNode() {
this.isRed = true;
this.isRoot = false;
this.isLeft = true;
}
public TreeNode(TreeNode<V> node) {
this.left = node.getLeft();
this.right = node.getRight();
this.parent = node.getParent();
this.key = node.getKey();
this.value = node.getValue();
this.isRed = node.isRed();
this.isRoot = node.isRoot();
this.isLeft = node.isLeft();
}
public TreeNode<V> getLeft() {
return left;
}
public void setLeft(TreeNode<V> left) {
this.left = left;
}
public TreeNode<V> getRight() {
return right;
}
public void setRight(TreeNode<V> right) {
this.right = right;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
public boolean isRed() {
return isRed;
}
public void setRed(boolean red) {
isRed = red;
}
public boolean isRoot() {
return isRoot;
}
public void setRoot(boolean root) {
isRoot = root;
}
public TreeNode<V> getParent() {
return parent;
}
public void setParent(TreeNode<V> parent) {
this.parent = parent;
}
public boolean isLeft() {
return isLeft;
}
public void setLeft(boolean left) {
isLeft = left;
}
}
红黑树类:
package com.test.Tree;
/**
* @author: cuijr
* @email:
* @date: 2020-11-10 15:08
* @description:
*/
public class RedBlackTree<V> {
// 树的根节点
private TreeNode<V> rootNode;
/**
* 查找
* @param key
* @param currentNode: the root node of the tree which you want to use
*/
public TreeNode<V> find(int key,TreeNode<V> currentNode) {
TreeNode<V> node = null;
if (currentNode != null) {
if (currentNode.getKey() == key) {
return currentNode;
}else if (currentNode.getKey() > key) {
return find(key, currentNode.getLeft());
}else {
return find(key, currentNode.getRight());
}
}
return node;
}
/**
* 内部类,存储找到的待插入节点的父节点及要插入的位置
*/
class TwoValue<V> {
private TreeNode<V> treeNode;
private char leftOrRight;
public TwoValue(TreeNode<V> treeNode, char leftOrRight) {
this.treeNode = treeNode;
this.leftOrRight = leftOrRight;
}
}
/**
* 查找待插入节点的父节点及要插入的位置
* @param key
* @param currentNode
* @return twoValue
*/
private TwoValue<V>