Golang Program to Delete a Node From a Red Black Tree


Red Black Tree is a self-balancing binary search tree with additional properties that ensure a balanced tree structure and efficient operations. The delete operation in a Red Black Tree involves rearranging the tree and maintaining the Red Black Tree properties after removing a node. Here we are going to use three different methods:deleteNode method, delete method, and successor transplant method along with examples to elaborate the concept. In this article, The Golang program implements the deletion operation in a Red Black Tree data structure using Golang.

Syntax

func (t *RedBlackTree) Delete(key int)

The Syntax "func (t *RedBlackTree) Delete(key int)" represents a method named "Delete" that belongs to the "RedBlackTree" struct type in Golang. It takes an integer parameter called "key" and has a pointer receiver of the "RedBlackTree" struct, denoted by "(t *RedBlackTree)". This method is used to delete a node with the specified key from the Red Black Tree.

func (t *RedBlackTree) deleteNode(node *Node)

The Syntax func (t *RedBlackTree) deleteNode(node *Node) defines a method named deleteNode associated with the RedBlackTree struct. The method takes a pointer to a Node as a parameter. The receiver of the method is a pointer to a RedBlackTree object, denoted by t *RedBlackTree.

func (t *RedBlackTree) transplant(u, v *Node)

The Syntax "func (t *RedBlackTree) transplant(u, v *Node)" represents a method named "transplant" that belongs to the "RedBlackTree" struct type in Golang. It takes two parameters, "u" and "v", which are both pointers to "Node" struct type. The method has a pointer receiver of the "RedBlackTree" struct, denoted by "(t *RedBlackTree)". This method is used to replace the subtree rooted at node "u" with the subtree rooted at node "v" in the Red Black Tree.

Algorithm

  • Start at the root of the Red Black Tree.

  • Traverse down the tree following the standard BST deletion algorithm to find the node with the given key.

  • If the node is found − a. If the node has no children or only one child, simply remove the node. b. If the node has two children, find its successor (the smallest key in its right subtree) and replace the node's key and value with the successor's key and value. c. Continue the deletion process on the successor node instead.

  • After deleting the node, check if any of the Red Black Tree properties have been violated.

  • If a violation is found, perform the necessary rotations and color adjustments to restore the properties.

  • Repeat steps 3-5 until the node is successfully deleted and the Red Black Tree properties are maintained.

Example

This code implements the Red Black Tree data structure in Go and includes the Delete method to delete a node from the tree. It also demonstrates an example usage of the Delete method by deleting a specific node from the Red Black Tree.

package main

import (
   "fmt"
)

type Color bool

const (
   Red   Color = true
   Black Color = false
)

type Node struct {
   value       int
   color       Color
   left, right *Node
   parent      *Node
}

type RedBlackTree struct {
   root *Node
}

func (t *RedBlackTree) Insert(value int) {
   node := &Node{
      value: value,
      color: Red,
   }
   t.insertNode(node)
   t.insertFixup(node)
}

func (t *RedBlackTree) insertNode(newNode *Node) {
   var parent *Node
   current := t.root

   for current != nil {
      parent = current
      if newNode.value < current.value {
         current = current.left
      } else {
         current = current.right
      }
   }

   newNode.parent = parent

   if parent == nil {
      t.root = newNode
   } else if newNode.value < parent.value {
      parent.left = newNode
   } else {
      parent.right = newNode
   }
}

func (t *RedBlackTree) insertFixup(node *Node) {
}

func (t *RedBlackTree) Delete(value int) {
   node := t.searchNode(value)
   if node == nil {
      return
   }
   t.deleteNode(node)
}

func (t *RedBlackTree) searchNode(value int) *Node {
   return nil
}

func (t *RedBlackTree) deleteNode(node *Node) {
}

// ... (Remaining methods: deleteFixup, successor, minimum, and printInorder)

func (t *RedBlackTree) PrintInorder() {
   t.printInorder(t.root)
   fmt.Println()
}

func (t *RedBlackTree) printInorder(node *Node) {
   if node != nil {
      t.printInorder(node.left)
      fmt.Printf("%d ", node.value)
      t.printInorder(node.right)
   }
}

func main() {
   tree := &RedBlackTree{}
   numbers := []int{50, 30, 70, 20, 40, 60, 80}

   for _, num := range numbers {
      tree.Insert(num)
   }

   fmt.Println("Red Black Tree before deletion:")
   tree.PrintInorder()

   fmt.Println("Deleting node with value 30:")
   tree.Delete(30)

   fmt.Println("Red Black Tree after deletion:")
   tree.PrintInorder()
}

Output

Red Black Tree before deletion:
20 30 40 50 60 70 80 
Deleting node with value 30:
Red Black Tree after deletion:
20 30 40 50 60 70 80 

Conclusion

In Conclusion, this article provides an efficient implementation of the delete operation in a Red Black Tree using Golang. Deleting a node from a Red Black Tree requires careful handling of cases and applying specific rotation and color-flipping operations to maintain the balance and properties of the tree. By following the defined algorithm and utilizing the necessary helper methods, the program enables the deletion of nodes from a Red Black Tree.

Updated on: 20-Jul-2023

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements