// Constructor public RedBlackNode(bool isBlack, T value, IRedBlack <T> left, IRedBlack <T> right) { Debug.Assert(left != null && right != null, "Children cannot be null."); IsBlack = isBlack; Value = value; Left = left; Right = right; }
public static IRedBlack <T> MakeRoot <T>(this IRedBlack <T> node) { if (node.IsRedNode()) { return(new RedBlackNode <T>(true, node.Value, node.Left, node.Right)); } return(node); }
// Private methods private IRedBlack <T> Balance(bool isBlack, T value, IRedBlack <T> left, IRedBlack <T> right) { if (Value.Equals(value) && Left == left && Right == right) { return(this); } return(RedBlackHelper.Balance(isBlack, value, left, right)); }
public ImmutableRedBlackDictionary(IComparer <TKey> keyComparer) { if (keyComparer == null) { throw new ArgumentNullException(nameof(keyComparer)); } _root = RedBlackLeaf <KeyValuePair <TKey, TValue> > .Instance; _comparer = new KeyComparer <TKey, TValue>(keyComparer); }
public ImmutableRedBlackSet(IComparer <T> comparer) { if (comparer == null) { throw new ArgumentNullException(); } _root = RedBlackLeaf <T> .Instance; _comparer = comparer; }
/// <summary> /// Balances the given subtree. /// </summary> /// <typeparam name="T">Type of values stored in the tree.</typeparam> /// <param name="isBlack">Is checked node black?</param> /// <param name="value">Value stored in the node.</param> /// <param name="left">New left node.</param> /// <param name="right">New right node.</param> /// <returns>Propagated node.</returns> public static IRedBlack <T> Balance <T>(bool isBlack, T value, IRedBlack <T> left, IRedBlack <T> right) { if (isBlack && left.IsRedNode() && left.Left.IsRedNode()) { var l = Black(left.Left.Value, left.Left.Left, left.Left.Right); var r = Black(value, left.Right, right); return(Red(left.Value, l, r)); } if (isBlack && left.IsRedNode() && left.Right.IsRedNode()) { var l = Black(left.Value, left.Left, left.Right.Left); var r = Black(value, left.Right.Right, right); return(Red(left.Right.Value, l, r)); } if (isBlack && right.IsRedNode() && right.Left.IsRedNode()) { var l = Black(value, left, right.Left.Left); var r = Black(right.Value, right.Left.Right, right.Right); return(Red(right.Left.Value, l, r)); } if (isBlack && right.IsRedNode() && right.Right.IsRedNode()) { var l = Black(value, left, right.Left); var r = Black(right.Right.Value, right.Right.Left, right.Right.Right); return(Red(right.Value, l, r)); } // Tree is already balanced. return(new RedBlackNode <T>(isBlack, value, left, right)); }
// Constructors public ImmutableRedBlackSet() { _root = RedBlackLeaf <T> .Instance; _comparer = Comparer <T> .Default; }
public static bool IsLeaf <T>(this IRedBlack <T> node) { return(node is RedBlackLeaf <T>); }
public static bool IsNode <T>(this IRedBlack <T> node) { return(node is RedBlackNode <T>); }
/// <summary> /// Checks if instance is black node. /// </summary> /// <typeparam name="T">Type contained in Red Black Tree.</typeparam> /// <param name="node">Checked node.</param> /// <returns>True, if node is not a leaf and it's black.</returns> public static bool IsRedNode <T>(this IRedBlack <T> node) { return(node is RedBlackNode <T> && !node.IsBlack); }
private ImmutableRedBlackDictionary(IRedBlack <KeyValuePair <TKey, TValue> > root, KeyComparer <TKey, TValue> comparer) { _root = root; _comparer = comparer; }
// Constructors public ImmutableRedBlackDictionary() { _root = RedBlackLeaf <KeyValuePair <TKey, TValue> > .Instance; _comparer = new KeyComparer <TKey, TValue>(); }
public void SetupRedBlack() { rbTree = redBlackFactory.MakeRedBlack(); for (int i = 0; i < capacity; i++) { rbTree.Add(i, initValue); } }
public WorkerNOTSync(IRedBlack rbTree, int nrOfOperations) { this.RedBlack = rbTree; this.nrOfOperations = nrOfOperations; }
public static IRedBlack <T> Insert <T>(IRedBlack <T> root, T item, IComparer <T> comparer = null) { comparer = comparer ?? Comparer <T> .Default; return(root.Insert(item, comparer).MakeRoot()); }
/// <summary> /// Creates new red node. /// </summary> private static RedBlackNode <T> Red <T>(T value, IRedBlack <T> left, IRedBlack <T> right) { return(new RedBlackNode <T>(false, value, left, right)); }
private ImmutableRedBlackSet(IRedBlack <T> root, IComparer <T> comparer) { _root = root; _comparer = comparer; }