/// <summary> /// Initialize the BST with given sorted keys. /// Time complexity: O(n). /// </summary> public BST(IEnumerable <T> sortedCollection, IComparer <T> comparer = null) : this(comparer) { BSTHelpers.ValidateSortedCollection(sortedCollection, this.comparer); var nodes = sortedCollection.Select(x => new BSTNode <T>(null, x)).ToArray(); Root = (BSTNode <T>)BSTHelpers.ToBST(nodes); BSTHelpers.AssignCount(Root); }
/// <summary> /// Initialize the BST with given sorted keys. /// Time complexity: O(n). /// </summary> /// <param name="sortedCollection">The initial sorted collection.</param> public TreapTree(IEnumerable <T> sortedCollection, IComparer <T> comparer = null) : this(comparer) { BSTHelpers.ValidateSortedCollection(sortedCollection, this.comparer); var nodes = sortedCollection.Select(x => new TreapTreeNode <T>(null, x, rndGenerator.Next())).ToArray(); Root = (TreapTreeNode <T>)BSTHelpers.ToBST(nodes); BSTHelpers.AssignCount(Root); heapify(Root); }
/// <summary> /// Initialize the BST with given sorted keys. /// Time complexity: O(n). /// </summary> /// <param name="sortedCollection">The sorted collection.</param> public SplayTree(IEnumerable <T> sortedCollection) : this() { BSTHelpers.ValidateSortedCollection(sortedCollection); var nodes = sortedCollection.Select(x => new SplayTreeNode <T>(null, x)).ToArray(); Root = (SplayTreeNode <T>)BSTHelpers.ToBST(nodes); BSTHelpers.AssignCount(Root); }
/// <summary> /// Initialize the BST with given sorted keys. /// Time complexity: O(n). /// </summary> /// <param name="sortedCollection">The initial sorted collection.</param> /// <param name="enableNodeLookUp">Enabling lookup will fasten deletion/insertion/exists operations /// at the cost of additional space.</param> public AVLTree(IEnumerable <T> sortedCollection, bool enableNodeLookUp = false) { BSTHelpers.ValidateSortedCollection(sortedCollection); var nodes = sortedCollection.Select(x => new AVLTreeNode <T>(null, x)).ToArray(); Root = (AVLTreeNode <T>)BSTHelpers.ToBST(nodes); recomputeHeight(Root); BSTHelpers.AssignCount(Root); if (enableNodeLookUp) { nodeLookUp = nodes.ToDictionary(x => x.Value, x => x as BSTNodeBase <T>); } }
/// <summary> /// Initialize the BST with given sorted keys optionally. /// Time complexity: O(n). /// </summary> /// <param name="sortedCollection">The sorted initial collection.</param> /// <param name="enableNodeLookUp">Enabling lookup will fasten deletion/insertion/exists operations /// at the cost of additional space.</param> /// <param name="equalityComparer">Provide equality comparer for node lookup if enabled (required when T is not a value type).</param> public RedBlackTree(IEnumerable <T> sortedCollection, bool enableNodeLookUp = false, IEqualityComparer <T> equalityComparer = null) { BSTHelpers.ValidateSortedCollection(sortedCollection); var nodes = sortedCollection.Select(x => new RedBlackTreeNode <T>(null, x)).ToArray(); Root = (RedBlackTreeNode <T>)BSTHelpers.ToBST(nodes); assignColors(Root); BSTHelpers.AssignCount(Root); if (enableNodeLookUp) { if (!typeof(T).GetTypeInfo().IsValueType&& equalityComparer == null) { throw new ArgumentException("equalityComparer parameter is required when node lookup us enabled and T is not a value type."); } NodeLookUp = nodes.ToDictionary(x => x.Value, x => x as BSTNodeBase <T>, equalityComparer ?? EqualityComparer <T> .Default); } }