Skip to content

Commit

Permalink
Fixed test insert colors
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasala98 committed Sep 18, 2021
1 parent 9208ccc commit 7a89cc1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 29 deletions.
41 changes: 33 additions & 8 deletions lib/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ OTHER DEALINGS IN THE SOFTWARE.

using System;

namespace Tree
namespace Tree
{
/// <summary>
/// Enumeration of all possible Colours of a node
Expand All @@ -50,14 +50,22 @@ public class Node

//Constructors
public Node(int key)
=> this.val = key;
=> this.val = key;

public Node(int key, Col colr)
{
this.val = key;
this.colour = colr;
}


public Node(int key, Col colr, Node l, Node r)
{
this.val = key;
this.colour = colr;
this.Left = l;
this.Right = r;
}

// Properties
public Node Left
{
Expand Down Expand Up @@ -102,20 +110,22 @@ public bool isLeftChild()
{
if (this.Parent.Left != null)
if (this == this.Parent.Left)
return true; else return false;
return true;
else return false;
else return false;
}

public bool isRightChild()
{
if (this.Parent.Right is not null)
if (this == this.Parent.Right)
return true; else return false;
if (this == this.Parent.Right)
return true;
else return false;
else return false;
}

public bool isRoot()
{
{
if (Parent is null) return true;
else return false;
}
Expand All @@ -132,6 +142,21 @@ public int getNodeHeight()
return height;
}

public static int getBlackHeight(Node x)
{
//Assuming this is a CORRECT RB tree
int bh = 0;
while (x.Left == null)
{
if (x.Colour == Col.BLK)
bh++;
x = x.Left;

}

return bh;
}

} //class Node

} //namespace
94 changes: 75 additions & 19 deletions lib/Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ namespace Tree
/// <summary>
/// Class representing a red-black tree
/// </summary>
public class RBTree {
public class RBTree
{

/// <summary>
/// THe root node of the red black tree
Expand All @@ -49,9 +50,9 @@ public class RBTree {
/// <summary>
/// Constructor passing the root node
/// </summary>
public RBTree (Node r)
public RBTree(Node r)
=> this.root = r;


/// <summary>
/// Returns the Node with minimum value of a subtree
Expand All @@ -60,7 +61,7 @@ public RBTree (Node r)
/// <param name="x"> Root of the subtree</param>
public static Node subtreeMinimum(Node x)
{
while(x.Left!=null) x = x.Left;
while (x.Left != null) x = x.Left;
return x;
}

Expand All @@ -71,7 +72,7 @@ public static Node subtreeMinimum(Node x)
/// <param name="x"> Root of the subtree</param>
public static Node subtreeMaximum(Node x)
{
while (x.Right != null) x = x.Right;
while (x.Right != null) x = x.Right;
return x;
}

Expand All @@ -80,13 +81,14 @@ public static Node subtreeMaximum(Node x)
/// </summary>
/// <param name="T">The tree</param>
/// <param name="x">The pivot of the rotation</param>
private static void leftRotation (RBTree T, Node x)
private static void leftRotation(RBTree T, Node x)
{
if (x.Right == null) throw new ArgumentException($"Node with key {x.Val} must have a right child!");

Node y = x.Right;
x.Right = y.Left; //Attach the rest of the tree (ONE DIRECTION)
if (y.Left is not null){
if (y.Left is not null)
{
y.Left.Parent = x; //Attach the rest of the tree (OTHER DIRECTION)
}
y.Parent = x.Parent;
Expand Down Expand Up @@ -114,14 +116,15 @@ private static void leftRotation (RBTree T, Node x)
/// <param name="T"> The tree</param>
/// <param name="x"> Pivot of the rotation</param>
/// <exception cref="ArgumentException"></exception>
private static void rightRotation (RBTree T, Node x)
private static void rightRotation(RBTree T, Node x)
{
if (x.Left is null) throw new ArgumentException($"Node with key {x.Val} must have a left child!");

Node y = x.Left;
x.Left = y.Right; //Attach the rest of the tree (ONE DIRECTION)

if (y.Right is not null){

if (y.Right is not null)
{
y.Right.Parent = x; //Attach the rest of the tree (OTHER DIRECTION)
}
y.Parent = x.Parent;
Expand Down Expand Up @@ -154,7 +157,7 @@ private static void rightRotation (RBTree T, Node x)
if (appo == null) break;
if (key < appo.Val) { appo = appo.Left; continue; }
if (key > appo.Val) { appo = appo.Right; continue; }
if (key == appo.Val) {found = true; obj = appo; }
if (key == appo.Val) { found = true; obj = appo; }
}

if (found) return obj;
Expand All @@ -166,21 +169,23 @@ private static void rightRotation (RBTree T, Node x)
/// Note that colours will be automatically balanced
/// </summary>
/// <param name="newnode"> The value of the Node you want to insert </param>
public void Insert (int newnodeVal)
public void Insert(int newnodeVal)
{
Node newnode = new Node(newnodeVal);
Node? y = null;
Node x = this.root;

while (x != null){
while (x != null)
{
y = x;
if (newnode.Val < x.Val)
x = x.Left;
else
x = x.Right;
}
newnode.Parent = y;
if (y == null){
if (y == null)
{
this.root = newnode;
}
else if (newnode.Val < y.Val)
Expand All @@ -194,12 +199,13 @@ public void Insert (int newnodeVal)
FixColorsInsert(this, newnode);

}

/// <summary>
/// Recolors nodes after insertion
/// (only if needed)
/// </summary>
private static void FixColorsInsert(RBTree T, Node z) {
private static void FixColorsInsert(RBTree T, Node z)
{

Node y;

Expand Down Expand Up @@ -249,11 +255,10 @@ private static void FixColorsInsert(RBTree T, Node z) {
leftRotation(T, z.Parent.Parent);
}
}

}
T.root.Colour = Col.BLK;
}
#nullable enable

// Some printing functions

Expand All @@ -273,7 +278,7 @@ private string InOrderDisplayStr(Node current)
if (current != null)
{
s += InOrderDisplayStr(current.Left);
s +=$"({current.Val}) ";
s += $"({current.Val}) ";
s += InOrderDisplayStr(current.Right);
}
return s;
Expand Down Expand Up @@ -345,6 +350,57 @@ public string DisplayFull()
else return InOrderFull(this.root);
}

/*
public static RBTree joinRight(Node left, int key, Node right)
{
if (2 * Node.getBlackHeight(left) == (int)(2 * Node.getBlackHeight(left) / 2) * 2)
{
Node newroot = new Node(key, Col.RED, left, right);
return new RBTree(newroot);
}
else
{
Node T = new Node(key: left.Val, colr: left.Colour,
l: left.Left, r: joinRight(left.Right, key, right).root);
RBTree Tprime = new RBTree(T);
if (left.Colour == Col.RED && T.Right.Colour == Col.RED && T.Right.Right.Colour == Col.RED)
{
T.Right.Right.Colour = Col.BLK;
leftRotation(Tprime, Tprime.root);
return Tprime;
}
else return Tprime;
}
}
public static RBTree joinLeft(Node left, int key, Node right)
{
if (2 * Node.getBlackHeight(right) == (int)(2 * Node.getBlackHeight(right) / 2) * 2)
{
Node newroot = new Node(key, Col.RED, left, right);
return new RBTree(newroot);
}
else
{
Node T = new Node(key: right.Val, colr: right.Colour,
l: right.Left, r: joinRight(right.Right, key, right).root);
RBTree Tprime = new RBTree(T);
if (right.Colour == Col.RED && T.Left.Colour == Col.RED && T.Left.Left.Colour == Col.RED)
{
T.Left.Left.Colour = Col.BLK;
rightRotation(Tprime, Tprime.root);
return Tprime;
}
else return Tprime;
}
}
public static RBTree join(RBTree left, int k, RBTree right)
{
}
*/


} //class
} //namespace
Binary file modified redblack_pres.key
Binary file not shown.
4 changes: 2 additions & 2 deletions test/TreeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public void TestInsertColors()
tree.Insert(13);
tree.Insert(1);

Assert.True(tree.DisplayTreeColorStr() == "(1RED) (3BLACK) (4RED) (5BLACK) (7BLACK) (8BLACK) (9RED) (13RED) (15BLACK) ",
"TestInsertColors failed!");
Assert.True(tree.DisplayTreeColorStr() == "(1RED) (3BLK) (4RED) (5BLK) (7BLK) (8BLK) (9RED) (13RED) (15BLK) ",
$"TestInsertColors failed! {tree.DisplayTreeColorStr()}");
return;
}
}
Expand Down

0 comments on commit 7a89cc1

Please sign in to comment.