195k views
0 votes
Hi how do i continue my code for insertion and searching function for LLRB?

class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
self.color=BLACK
class LeftLeaningRedBlack:
def rotateLeft(n):
x = n.right
n.right = x.left
x.left = n
x.color = n.color
n.color = RED
return x
def rotateRight(n):
x = n.left
n.left = x.right
x.right = n
x.color = n.color
n.color = RED
return x
def flipColor(n):
n.color = RED
n.left.color = BLACK
n.right.color = BLACK

User Eveliotc
by
8.1k points

1 Answer

0 votes

Final answer:

To continue an LLRB tree implementation, insert nodes as in a binary search tree and then make adjustments to maintain LLRB properties. For searching, traverse similarly to a standard BST, returning the node when a match is found.

Step-by-step explanation:

To continue coding the insertion and searching functions for a Left-Leaning Red Black (LLRB) tree, we must consider the properties that define an LLRB. These include ensuring that red links lean left, no node has two red links connected to it, and the tree has the same number of black links on any path from the root to the leaves.

The insertion function should start by inserting the node as in a regular binary search tree, then applying LLRB fix-up operations to restore the LLRB properties. This involves checking for right-leaning red links and rotating them left, ensuring that two red links do not appear in succession, and in case of a black node with two red children, flipping the colors.

The searching function is similar to that in a binary search tree (BST). You traverse the tree based on the value you're searching for, moving left if the value is less than the current node and right if it's greater, returning the node when the value matches.

User Ilya Zaytsev
by
8.6k points