红黑树是一种自平衡的二叉查找树,通过在每个节点上增加一个存储位来表示节点的颜色(红色或黑色),并通过一系列的操作来维护树的平衡性质。红黑树的实现思路原理如下:
手写红黑树的实现有以下必要性:
红黑树作为一种高效的数据结构,在许多领域都有广泛的应用。根据市场调查的结果,红黑树的应用范围包括但不限于以下几个方面:
红黑树的实现步骤如下:
class RedBlackTreeNode<T> {
T value;
boolean isRed;
RedBlackTreeNode<T> left;
RedBlackTreeNode<T> right;
RedBlackTreeNode<T> parent;
}
class RedBlackTree<T extends Comparable<T>> {
private RedBlackTreeNode<T> root;
public void insert(T value) {
RedBlackTreeNode<T> newNode = new RedBlackTreeNode<>();
newNode.value = value;
newNode.isRed = true;
if (root == null) {
root = newNode;
root.isRed = false;
} else {
RedBlackTreeNode<T> current = root;
RedBlackTreeNode<T> parent;
while (true) {
parent = current;
if (value.compareTo(current.value) < 0) {
current = current.left;
if (current == null) {
parent.left = newNode;
newNode.parent = parent;
break;
}
} else {
current = current.right;
if (current == null) {
parent.right = newNode;
newNode.parent = parent;
break;
}
}
}
}
fixInsert(newNode);
}
private void fixInsert(RedBlackTreeNode<T> node) {
while (node != root && node.parent.isRed) {
if (node.parent == node.parent.parent.left) {
RedBlackTreeNode<T> uncle = node.parent.parent.right;
if (uncle != null && uncle.isRed) {
node.parent.isRed = false;
uncle.isRed = false;
node.parent.parent.isRed = true;
node = node.parent.parent;
} else {
if (node == node.parent.right) {
node = node.parent;
rotateLeft(node);
}
node.parent.isRed = false;
node.parent.parent.isRed = true;
rotateRight(node.parent.parent);
}
} else {
RedBlackTreeNode<T> uncle = node.parent.parent.left;
if (uncle != null && uncle.isRed) {
node.parent.isRed = false;
uncle.isRed = false;
node.parent.parent.isRed = true;
node = node.parent.parent;
} else {
if (node == node.parent.left) {
node = node.parent;
rotateRight(node);
}
node.parent.isRed = false;
node.parent.parent.isRed = true;
rotateLeft(node.parent.parent);
}
}
}
root.isRed = false;
}
private void rotateLeft(RedBlackTreeNode<T> node) {
RedBlackTreeNode<T> rightChild = node.right;
node.right = rightChild.left;
if (rightChild.left != null) {
rightChild.left.parent = node;
}
rightChild.parent = node.parent;
if (node.parent == null) {
root = rightChild;
} else if (node == node.parent.left) {
node.parent.left = rightChild;
} else {
node.parent.right = rightChild;
}
rightChild.left = node;
node.parent = rightChild;
}
private void rotateRight(RedBlackTreeNode<T> node) {
RedBlackTreeNode<T> leftChild = node.left;
node.left = leftChild.right;
if (leftChild.right != null) {
leftChild.right.parent = node;
}
leftChild.parent = node.parent;
if (node.parent == null) {
root = leftChild;
} else if (node == node.parent.right) {
node.parent.right = leftChild;
} else {
node.parent.left = leftChild;
}
leftChild.right = node;
node.parent = leftChild;
}
}
class RedBlackTree<T extendsComparable<T>> {
private RedBlackTreeNode<T> root;
public void delete(T value) {
RedBlackTreeNode<T> node = search(value);
if (node == null) {
return;
}
RedBlackTreeNode<T> child;
if (node.left != null && node.right != null) {
RedBlackTreeNode<T> successor = node.right;
while (successor.left != null) {
successor = successor.left;
}
node.value = successor.value;
node = successor;
}
if (node.left != null) {
child = node.left;
} else {
child = node.right;
}
if (node.isRed) {
if (node.parent.left == node) {
node.parent.left = child;
} else {
node.parent.right = child;
}
if (child != null) {
child.parent = node.parent;
}
} else if (child != null && child.isRed) {
child.isRed = false;
} else {
deleteFix(node, child);
}
}
private void deleteFix(RedBlackTreeNode<T> node, RedBlackTreeNode<T> child) {
while (node != root && (child == null || !child.isRed)) {
if (node == node.parent.left) {
RedBlackTreeNode<T> sibling = node.parent.right;
if (sibling.isRed) {
sibling.isRed = false;
node.parent.isRed = true;
rotateLeft(node.parent);
sibling = node.parent.right;
}
if ((sibling.left == null || !sibling.left.isRed) &&
(sibling.right == null || !sibling.right.isRed)) {
sibling.isRed = true;
node = node.parent;
} else {
if (sibling.right == null || !sibling.right.isRed) {
sibling.left.isRed = false;
sibling.isRed = true;
rotateRight(sibling);
sibling = node.parent.right;
}
sibling.isRed = node.parent.isRed;
node.parent.isRed = false;
sibling.right.isRed = false;
rotateLeft(node.parent);
node = root;
}
} else {
RedBlackTreeNode<T> sibling = node.parent.left;
if (sibling.isRed) {
sibling.isRed = false;
node.parent.isRed = true;
rotateRight(node.parent);
sibling = node.parent.left;
}
if ((sibling.left == null || !sibling.left.isRed) &&
(sibling.right == null || !sibling.right.isRed)) {
sibling.isRed = true;
node = node.parent;
} else {
if (sibling.left == null || !sibling.left.isRed) {
sibling.right.isRed = false;
sibling.isRed = true;
rotateLeft(sibling);
sibling = node.parent.left;
}
sibling.isRed = node.parent.isRed;
node.parent.isRed = false;
sibling.left.isRed = false;
rotateRight(node.parent);
node = root;
}
}
if (child != null) {
child.isRed = false;
}
}
}
private void rotateLeft(RedBlackTreeNode<T> node) {
RedBlackTreeNode<T> rightChild = node.right;
node.right = rightChild.left;
if (rightChild.left != null) {
rightChild.left.parent = node;
}
rightChild.parent = node.parent;
if (node.parent == null) {
root = rightChild;
} else if (node == node.parent.left) {
node.parent.left = rightChild;
} else {
node.parent.right = rightChild;
}
rightChild.left = node;
node.parent = rightChild;
}
private void rotateRight(RedBlackTreeNode<T> node) {
RedBlackTreeNode<T> leftChild = node.left;
node.left = leftChild.right;
if (leftChild.right != null) {
leftChild.right.parent = node;
}
leftChild.parent = node.parent;
if (node.parent == null) {
root = leftChild;
} else if (node == node.parent.right) {
node.parent.right = leftChild;
} else {
node.parent.left = leftChild;
}
leftChild.right = node;
node.parent = leftChild;
}
}
class RedBlackTree<T extends Comparable<T>> {
// ...
public RedBlackTreeNode<T> search(T value) {
RedBlackTreeNode<T> current = root;
while (current != null) {
if (value.compareTo(current.value) == 0) {
return current;
} else if (value.compareTo(current.value) < 0) {
current = current.left;
} else {
current = current.right;
}
}
return null;
}
}
class RedBlackTree<T extends Comparable<T>> {
// ...
public void preOrderTraversal() {
preOrderTraversal(root);
}
private void preOrderTraversal(RedBlackTreeNode<T> node) {
if (node != null) {
System.out.println(node.value);
preOrderTraversal(node.left);
preOrderTraversal(node.right);
}
}
public void inOrderTraversal() {
inOrderTraversal(root);
}
private void inOrderTraversal(RedBlackTreeNode<T> node) {
if (node != null) {
inOrderTraversal(node.left);
System.out.println(node.value);
inOrderTraversal(node.right);
}
}
public void postOrderTraversal() {
postOrderTraversal(root);
}
private void postOrderTraversal(RedBlackTreeNode<T> node) {
if (node != null) {
postOrderTraversal(node.left);
postOrderTraversal(node.right);
System.out.println(node.value);
}
}
}
红黑树是一种自平衡的二叉搜索树,通过保持以下性质来保持平衡:
通过实现红黑树的插入、删除、查找和遍历操作,我们可以有效地操作红黑树。红黑树在实际应用中有很多用途,例如在数据库中实现索引、在操作系统中实现文件系统等。