由于红黑树的实现比较复杂,需要考虑很多情况,因此在这里只提供伪代码演示。
插入操作:
func (t *RBTree) Insert(value interface{}) {
node := &Node{value: value, color: red}if t.root == nil {t.root = node} else {parent := t.root // 从根节点开始查找插入位置for {if node.value < parent.value {if parent.left == nil {parent.left = nodenode.parent = parentbreak}parent = parent.left} else {if parent.right == nil {parent.right = nodenode.parent = parentbreak}parent = parent.right}}t.insertFixup(node) // 插入后进行修复}
}
func (t *RBTree) insertFixup(node *Node) {
for node.parent != nil && node.parent.color == red { // 父节点存在且为红色时继续修复if node.parent == node.parent.parent.left { // 父节点为祖父节点的左子节点uncle := node.parent.parent.right // 叔叔节点if uncle != nil && uncle.color == red { // Case 1: 叔叔节点为红色node.parent.color = blackuncle.color = blacknode.parent.parent.color = rednode = node.parent.parent} else { // Case 2/3: 叔叔节点为黑色if node == node.parent.right { // Case 2: 新节点为父节点的右子节点node = node.parentt.leftRotate(node)}// Case 3: 新节点为父节点的左子节点node.parent.color = blacknode.parent.parent.color = redt.rightRotate(node.parent.parent)}} else { // 父节点为祖父节点的右子节点,与上面类似但是左右反转uncle := node.parent.parent.leftif uncle != nil && uncle.color == red {node.parent.color = blackuncle.color = blacknode.parent.parent.color = rednode = node.parent.parent} else {if node == node.parent.left {node = node.parentt.rightRotate(node)}node.parent.color = blacknode.parent.parent.color = redt.leftRotate(node.parent.parent)}}}t.root.color = black // 根节点总是黑色
}
删除操作:
func (t *RBTree) Delete(value interface{}) {
node := t.searchNode(t.root, value)if node == nil {return}var child *Nodevar color boolif node.left == nil { // 左子树为空或只有一个右子节点child = node.rightt.transplant(node, node.right)} else if node.right == nil { // 右子树为空或只有一个左子节点child = node.leftt.transplant(node, node.left)} else { // 左右子树均不为空successor := t.minimumNode(node.right) // 找到右子树的最小节点color = successor.colorchild = successor.rightif successor.parent == node { // 后继节点是待删除节点的右子节点child.parent = successor} else {t.transplant(successor, successor.right)successor.right = node.rightsuccessor.right.parent = successor}t.transplant(node, successor)successor.left = node.leftsuccessor.left.parent = successorsuccessor.color = node.color}if color == black { // 如果删除的是黑色节点,则需要修复t.deleteFixup(child)}
}
func (t *RBTree) deleteFixup(node *Node) {
for node != t.root && node.color == black {if node == node.parent.left { // 被删除节点为父节点的左子节点sibling := node.parent.right // 兄弟节点if sibling.color == red { // Case 1: 兄弟节点为红色sibling.color = blacknode.parent.color = redt.leftRotate(node.parent)sibling = node.parent.right}if sibling.left.color == black && sibling.right.color == black { // Case 2: 兄弟节点和兄弟节点的子节点均为黑色sibling.color = rednode = node.parent} else {if sibling.right.color == black { // Case 3: 兄弟节点为黑色,兄弟节点的右子节点为黑色,兄弟节点的左子节点为红色sibling.left.color = blacksibling.color = redt.rightRotate(sibling)sibling = node.parent.right}// Case 4: 兄弟节点为黑色,兄弟节点的右子节点为红色sibling.color = node.parent.colornode.parent.color = blacksibling.right.color = blackt.leftRotate(node.parent)node = t.root // 跳出循环}} else { // 被删除节点为父节点的右子节点,与上面类似但是左右反转sibling := node.parent.leftif sibling.color == red {sibling.color = blacknode.parent.color = redt.rightRotate(node.parent)sibling = node.parent.left}if sibling.left.color == black && sibling.right.color == black {sibling.color = rednode = node.parent} else {if sibling.left.color == black {sibling.right.color = blacksibling.color = redt.leftRotate(sibling)sibling = node.parent.left}sibling.color = node.parent.colornode.parent.color = blacksibling.left.color = blackt.rightRotate(node.parent)node = t.root}}}node.color = black
}
前序遍历:
func (t *RBTree) PreorderTraversal() []interface{} {
var values []interface{}var preorder func(node *Node)preorder = func(node *Node) {if node != nil {values = append(values, node.value)preorder(node.left)preorder(node.right)}}preorder(t.root)return values
}
中序遍历:
func (t *RBTree) InorderTraversal() []interface{} {
var values []interface{}var inorder func(node *Node)inorder = func(node *Node) {if node != nil {inorder(node.left)values = append(values, node.value)inorder(node.right)}}inorder(t.root)return values
}
后序遍历:
func (t *RBTree) PostorderTraversal() []interface{} {
var values []interface{}var postorder func(node *Node)postorder = func(node *Node) {if node != nil {postorder(node.left)postorder(node.right)values = append(values, node.value)}}postorder(t.root)return values
}




