I take an algorithm course at the university, and for one of my projects I want to implement a red-black tree in C # (the implementation itself is not a project, but I just decided to choose something to help me not).
In my red-black tree, string keys should be stored, and the object created for each node looks like this:
class sRbTreeNode
{
public sRbTreeNode Parent = null;
public sRbTreeNode Right = null;
public sRbTreeNode Left = null;
public String Color;
public String Key;
public sRbTreeNode()
{
}
public sRbTreeNode(String key)
{
Key = key;
}
}
I have already added some basic methods for printing a tree, finding a root, min / max key (alphabetically), etc.
I'm having trouble inserting nodes (hence creating a tree). Anyone familiar with red-black trees knows that when you add a node in one direction, you could change the balance of the tree. To fix this, you need to βrotateβ around the nodes in the tree to balance the tree.
RightRotate LeftRotate , , #, sRbTreeNode.
, LeftRotate:
LeftRotate(root, node)
y <- node.Right;
node.Right <- y.Left;
if (y.Left != null)
y.Left.Parent <- node;
y.Parent <- node.Parent;
if (node.Parent = null)
root <- y;
else
if (node = node.Parent.Left)
node.Parent.Left = y;
else
node.Parent.Right = y;
y.Left <- node;
node.Parent <- y
, 'ref', .
:
public static void LeftRotate(sRbTreeNode root, sRbTreeNode node)
{
sRbTreeNode y = node.Right;
node.Right = y.Left;
if (y.Left != null)
y.Left.Parent = node;
y.Parent = node.Parent;
if (node.Parent == null)
root = y;
else
if (node == node.Parent.Left)
node.Parent.Left = y;
else
node.Parent.Right = y;
y.Left = node;
node.Parent = y;
}
, , , , , , . , , . "ref" .
?