using System;
class RbTree
{
public TreeNode Root = null;
public int HeightT(TreeNode Root)
{
int lefth, righth;
if (Root == null ||
(Root.children == null &&
Root.children[1] == null))
{
return 0;
}
lefth = HeightT(Root.children[0]);
righth = HeightT(Root.children[1]);
return (Math.Max(lefth, righth) + 1);
}
public int check(int dir)
{
return dir == 0 ? 1 : 0;
}
public bool isRed(TreeNode Node)
{
return Node != null &&
Node.color.Equals("R");
}
public TreeNode SingleRotate(TreeNode Node, int dir)
{
TreeNode temp = Node.children[check(dir)];
Node.children[check(dir)] = temp.children[dir];
temp.children[dir] = Node;
Root.color = "R";
temp.color = "B";
return temp;
}
public TreeNode DoubleRotate(TreeNode Node, int dir)
{
Node.children[check(dir)] =
SingleRotate(Node.children[check(dir)],
check(dir));
return SingleRotate(Node, dir);
}
public TreeNode Insert(RbTree tree,
String data)
{
if (tree.Root == null)
{
tree.Root = new TreeNode(data);
if (tree.Root == null)
return null;
}
else
{
TreeNode temp = new TreeNode("");
TreeNode g, t;
TreeNode p, q;
int dir = 0, last = 0;
t = temp;
g = p = null;
t.children[1] = tree.Root;
q = t.children[1];
while (true)
{
if (q == null)
{
q = new TreeNode(data);
p.children[dir] = q;
}
else if (isRed(q.children[0]) &&
isRed(q.children[1]))
{
q.color = "R";
q.children[0].color = "B";
q.children[1].color = "B";
}
if (isRed(q) && isRed(p))
{
int dir2;
if (t.children[1] == g)
{
dir2 = 1;
}
else
{
dir2 = 0;
}
if (q == p.children[last])
{
t.children[dir2] =
SingleRotate(g, last == 0 ? 1 : 0);
}
else
{
t.children[dir2] =
DoubleRotate(g, last == 0 ? 1 : 0);
}
}
if (q.data.Equals(data))
{
break;
}
last = dir;
dir = q.data.CompareTo(data) < 0 ? 1 : 0;
if (g != null)
{
t = g;
}
g = p;
p = q;
q = q.children[dir];
}
tree.Root = temp.children[1];
}
tree.Root.color = "B";
return tree.Root;
}
public void PrintLevel(TreeNode root, int i)
{
if (root == null)
{
return;
}
if (i == 1)
{
Console.Write("| " + root.data +
" | " + root.color + " |");
if (root.children[0] != null)
{
Console.Write(" " +
root.children[0].data + " |");
}
else
{
Console.Write(" " + "NULL" + " |");
}
if (root.children[1] != null)
{
Console.Write(" " +
root.children[1].data + " |");
}
else
{
Console.Write(" " + "NULL" + " |");
}
Console.Write(" ");
return;
}
PrintLevel(root.children[0], i - 1);
PrintLevel(root.children[1], i - 1);
}
public void LevelOrder(TreeNode root)
{
int i;
for (i = 1; i < HeightT(root) + 1; i++)
{
PrintLevel(root, i);
Console.Write("\n\n");
}
}
}
public class TreeNode
{
public String data, color;
public TreeNode []children;
public TreeNode(String data)
{
this.data = data;
this.color = "R";
children = new TreeNode[2];
children[0] = null;
children[1] = null;
}
}
public class Driver
{
public static void Main(String[] args)
{
RbTree Tree = new RbTree();
String Sentence, Word;
Sentence = "old is gold";
char[] spearator = { ' ', ' ' };
String []Word_Array = Sentence.Split(spearator,
StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < Word_Array.Length; i++)
{
Tree.Root = Tree.Insert(Tree,
Word_Array[i]);
}
Console.WriteLine("The Level" +
"Order Traversal" +
"of the tree is:");
Tree.LevelOrder(Tree.Root);
Console.WriteLine("\nInserting a" +
" word in the tree:");
Word = "forever";
Tree.Root = Tree.Insert(Tree, Word);
Console.WriteLine("");
Tree.LevelOrder(Tree.Root);
}
}