public RedBlackTreeSet(RedBlackTreeSet <T> anotherSet) { var sortedArray = anotherSet.ToArray(); this.comparer = anotherSet.comparer; tree = new LeftLeaningRedBlackTree <T>(sortedArray, 0, sortedArray.Length - 1, this.comparer); }
public RedBlackTreeSet <T> Intersect(RedBlackTreeSet <T> anotherSet) { if (anotherSet == null) { throw new ArgumentNullException(nameof(anotherSet)); } if (anotherSet.Empty) { return(new RedBlackTreeSet <T>()); } int intersectionCount = 0; var minSize = this.Count > anotherSet.Count ? anotherSet.Count : this.Count; var intersectionArray = new T[minSize]; MergeInOrderTraversal(anotherSet, null, null, item => intersectionArray[intersectionCount++] = item); var intersectionTree = new LeftLeaningRedBlackTree <T>(intersectionArray, 0, intersectionCount - 1, comparer); return(new RedBlackTreeSet <T>(intersectionTree, comparer)); }
public RedBlackTreeSet <T> Union(RedBlackTreeSet <T> anotherSet) { if (anotherSet == null) { throw new ArgumentNullException(nameof(anotherSet)); } if (anotherSet.Empty) { return(new RedBlackTreeSet <T>(this)); } int unionCount = 0; var unionArray = new T[this.Count + anotherSet.Count]; Action <T> handler = item => unionArray[unionCount++] = item; MergeInOrderTraversal(anotherSet, handler, handler, handler); var unionTree = new LeftLeaningRedBlackTree <T>(unionArray, 0, unionCount - 1, comparer); return(new RedBlackTreeSet <T>(unionTree, comparer)); }
public RedBlackTreeSet <T> Difference(RedBlackTreeSet <T> anotherSet) { if (anotherSet == null) { throw new ArgumentNullException(nameof(anotherSet)); } if (anotherSet.Empty) { return(new RedBlackTreeSet <T>(this)); } int differenceCount = 0; var differenceArray = new T[this.Count]; MergeInOrderTraversal(anotherSet, item => differenceArray[differenceCount++] = item, null, null); var differenceTree = new LeftLeaningRedBlackTree <T>(differenceArray, 0, differenceCount - 1, comparer); return(new RedBlackTreeSet <T>(differenceTree, comparer)); }
private RedBlackTreeSet(LeftLeaningRedBlackTree <T> tree, IComparer <T> comparer) { this.tree = tree ?? throw new ArgumentNullException(nameof(tree)); this.comparer = comparer ?? throw new ArgumentNullException(nameof(comparer)); }
public RedBlackTreeSet(IComparer <T> comparer) { this.comparer = comparer ?? throw new ArgumentNullException(nameof(comparer)); tree = new LeftLeaningRedBlackTree <T>(this.comparer); }
public RedBlackTreeSet() { this.comparer = Comparer <T> .Default; tree = new LeftLeaningRedBlackTree <T>(this.comparer); }