Lesson 16 of 47
In Progress

# Red-Black Trees

##### Yasin Cakal

Red-black trees are a type of self-balancing binary search tree. This means that they are a data structure which consists of nodes that have up to two children. Each node contains a key and a value, and each node’s key is greater than the key of its left child and less than the key of its right child. They are often used in applications such as databases, operating systems, and search engines. Red-black trees have several advantages over other types of trees, such as being more efficient in terms of time and space complexity, and providing faster search and insertion operations.

## Time Complexity of Red-black Trees

Red-black trees have an average time complexity of O(log n) for access, search, insertion, and deletion operations. This means that the time it takes for these operations is proportional to the logarithm of the number of nodes in the tree. The worst case time complexity of these operations is O(log n). This means that the time it takes to perform these operations is proportional to the logarithm of the maximum number of nodes in the tree.

## How does Red-Black Trees Work?

Red-Black Trees (RBTs) are a type of self-balancing binary search tree that is used to store and quickly retrieve data. RBTs are an efficient data structure for maintaining an ordered set of elements.

Red-Black Trees (RBTs) are a type of self-balancing binary search tree that is used to store and quickly retrieve data. RBTs are an efficient data structure for maintaining an ordered set of elements.

RBTs have the following properties:

• Each node is either red or black. 2.
• The root node is always black. 3.
• All leaves (null nodes) are black. 4.
• Both children of every red node are black. 5.
• Every path from a node to a descendant leaf has the same number of black nodes.

RBTs also maintain the ordering of the data. The data is sorted into the tree according to a certain comparison function. In an RBT, when a node is inserted, the tree is rebalanced to make sure the properties of the tree are maintained.

When a node is inserted, the rules of the RBT are checked to see if they are violated. If they are violated, the tree is rebalanced to restore the properties. There are two types of rebalancing that can be done on an RBT: rotation and recoloring.

Rotation is the process of moving a node from one position in the tree to another in order to restore the balance. This is done by rotating the nodes around the tree so that the structure is maintained.

Recoloring is the process of changing the color of a node from red to black or from black to red. This is done to restore the properties of the tree.

RBTs are a very efficient data structure for maintaining an ordered set of elements. They provide quick insertion, deletion, and lookup of elements. They also maintain the order of the data and can be quickly rebalanced when needed.

## Space Complexity of Red-black Trees

The worst case space complexity of red-black trees is O(n). This means that the amount of memory needed to store the tree is proportional to the number of nodes in the tree.

## Python Implementation

In order to demonstrate the time and space complexity of red-black trees, we will look at a few coding examples.

### Access Operation

In this example, we will look at the time complexity for an access operation on a red-black tree. We will use the following code to access a node with a key of 10 in a red-black tree with 10 nodes.

def access(key):
current = root
while current is not None:
if current.key == key:
return current
elif current.key > key:
current = current.left
else:
current = current.right
return None

In this example, the time complexity of the access operation is O(log n). This is because, in the worst case, the access operation takes log n steps to traverse the tree.

### Search Operation

In this example, we will look at the time complexity for a search operation on a red-black tree. We will use the following code to search for a node with a key of 10 in a red-black tree with 10 nodes.

def search(key):
current = root
while current is not None:
if current.key == key:
return True
elif current.key > key:
current = current.left
else:
current = current.right
return False

In this example, the time complexity of the search operation is O(log n). This is because, in the worst case, the search operation takes log n steps to traverse the tree.

### Insertion Operation

In this example, we will look at the time complexity for an insertion operation on a red-black tree. We will use the following code to insert a node with a key of 10 in a red-black tree with 10 nodes.

def insert(key):
current = root
parent = None
while current is not None:
parent = current
if current.key > key:
current = current.left
else:
current = current.right
new_node = Node(key)
if parent is None:
root = new_node
elif parent.key > key:
parent.left = new_node
else:
parent.right = new_node

In this example, the time complexity of the insertion operation is O(log n). This is because, in the worst case, the insertion operation takes log n steps to traverse the tree.

### Deletion Operation

In this example, we will look at the time complexity for a deletion operation on a red-black tree. We will use the following code to delete a node with a key of 10 in a red-black tree with 10 nodes.

def delete(key):
current = root
parent = None
while current is not None:
if current.key == key:
break
elif current.key > key:
parent = current
current = current.left
else:
parent = current
current = current.right
if current is None:
return
# Case 1: Node is a leaf
if current.left is None and current.right is None:
if parent is None:
root = None
elif parent.left == current:
parent.left = None
else:
parent.right = None
# Case 2: Node has one child
elif current.left is None:
if parent is None:
root = current.right
elif parent.left == current:
parent.left = current.right
else:
parent.right = current.right
elif current.right is None:
if parent is None:
root = current.left
elif parent.left == current:
parent.left = current.left
else:
parent.right = current.left
# Case 3: Node has two children
else:
successor = get_successor(current)
if parent is None:
root = successor
elif parent.left == current:
parent.left = successor
else:
parent.right = successor
successor.left = current.left

In this example, the time complexity of the deletion operation is O(log n). This is because, in the worst case, the deletion operation takes log n steps to traverse the tree.

## Conclusion

In conclusion, red-black trees are a type of self-balancing binary search tree that have an average time complexity of O(log n) for access, search, insertion, and deletion operations, and a worst case time complexity of O(log n). They also have a worst case space complexity of O(n). This makes red-black trees a popular choice for applications such as databases, operating systems, and search engines.

## Exercises

#### Implement a function to search an element in a Red-Black Tree using Python.

def search_rb_tree(node, key):
if node is None or node.key == key:
return node
if key < node.key:
return search_rb_tree(node.left, key)
return search_rb_tree(node.right, key) 

#### Write a function to delete a node from a Red-Black Tree in Python.

def delete_rb_tree_node(node, key):
if node is None:
return node

if key < node.key:
node.left = delete_rb_tree_node(node.left, key)
elif key > node.key:
node.right = delete_rb_tree_node(node.right, key)
else:
if node.left is None:
temp = node.right
node = None
return temp
elif node.right is None:
temp = node.left
node = None
return temp
temp = min_value_node(node.right)
node.key = temp.key
node.right = delete_rb_tree_node(node.right, temp.key)

return node


#### Write a function to find the maximum value in a Red-Black Tree using Python.

def max_value_rb_tree(node):
if node is None:
return -1
current = node
while current.right is not None:
current = current.right
return current.key

#### Implement a function to insert a node into a Red-Black Tree using Python.

def insert_rb_tree_node(root, key):
if root is None:
return Node(key)
if root.key > key:
root.left = insert_rb_tree_node(root.left, key)
else:
root.right = insert_rb_tree_node(root.right, key)
return root 

#### Write a function to traverse a Red-Black Tree in order using Python.

def traverse_rb_tree(node):
if node is None:
return
traverse_rb_tree(node.left)
print(node.key)
traverse_rb_tree(node.right)