public void Delete_Single() { const int insertValue = 1; IBinarySearchTree<int> tree = new AvlTree<int>(); tree.Insert(insertValue); Assert.IsTrue(tree.Find(insertValue)); Assert.IsTrue(tree.Delete(insertValue)); Assert.IsFalse(tree.Find(insertValue)); }
public List <List <Doll> > LongestChains(List <Doll> envelopes) { if (envelopes == null) { return(new List <List <Doll> >()); } if (!envelopes.Any()) { return(new List <List <Doll> >()); } envelopes.Sort(); AvlTree <Doll> currTree = new AvlTree <Doll>(); AvlTree <Doll> .Node root = null; foreach (var envelope in envelopes) { currTree.Insert(envelope); root = currTree.Find(envelope); } currTree.Print(); return(new List <List <Doll> > { }); }
private bool TestFind() { Console.WriteLine("Starting find test..."); foreach (var i in HelpList) { var result = AvlTree.Find(i); if (result == 0) { Console.WriteLine($"could not find value {i}"); return(false); } if (result != i) { Console.WriteLine($"tree should find {i} but found {result}"); return(false); } if (!AvlTree.Contains(i)) { Console.WriteLine($"Contains test failed... result = {result} i = {i}"); return(false); } } Console.WriteLine("Find test successful"); return(true); }
public virtual GroupCollection <T> Perform( IReadOnlyCollection <T> items, int level, Group <T> parent) { GroupDescriptorCollection groupDescriptors = this.index.CollectionView.GroupDescriptors; if (level >= groupDescriptors.Count) { return(GroupCollection <T> .Empty); } IGroupFactory <T> groupFactory = this.index.CollectionView.GroupFactory; GroupCollection <T> cachedGroups = this.GetCachedGroups(items, level); if (!GroupBuilder <T> .IsValid(level, groupDescriptors)) { cachedGroups?.Dispose(); return(groupFactory.CreateCollection((IList <Group <T> >)GroupBuilder <T> .Empty)); } IComparer <Group <T> > comparer = Activator.CreateInstance(this.Comparer.GetType()) as IComparer <Group <T> > ?? this.Comparer; if (comparer is GroupComparer <T> ) { ((GroupComparer <T>)comparer).Directions = this.GetComparerDirections(level); } AvlTree <Group <T> > avlTree1 = new AvlTree <Group <T> >(comparer); AvlTree <Group <T> > avlTree2 = new AvlTree <Group <T> >((IComparer <Group <T> >) new GroupComparer <T>(this.GetComparerDirections(level))); foreach (T key1 in !this.CollectionView.CanPage || level != 0 || !this.CollectionView.PagingBeforeGrouping ? (IEnumerable <T>)items : (IEnumerable <T>) this.index.GetItemsOnPage(this.CollectionView.PageIndex)) { object key2 = this.GroupPredicate(key1, level); Group <T> group1 = (Group <T>) new DataItemGroup <T>(key2); Group <T> group2 = avlTree2.Find(group1); if (group2 == null) { group2 = this.GetGroup(cachedGroups, group1, parent, key2, level); avlTree2.Add(group2); } group2.Items.Add(key1); if (this.itemGroupsCache.ContainsKey(key1)) { this.itemGroupsCache[key1] = group2; } else { this.itemGroupsCache.Add(key1, group2); } } for (int index = 0; index < avlTree2.Count; ++index) { avlTree1.Add(avlTree2[index]); } cachedGroups?.Dispose(); return(groupFactory.CreateCollection((IList <Group <T> >)avlTree1)); }
public void Delete_EntireArray() { int[] data = { 5, 2, 6, 3, 1, 4 }; int[] expected = { 1, 2, 3, 4, 5, 6 }; IBinarySearchTree<int> tree = new AvlTree<int>(); foreach (int value in data) tree.Insert(value); int[] actualArray = tree.ToArray(); CollectionAssert.AreEqual(expected, actualArray); Assert.AreEqual(expected.Length, tree.Size); Assert.IsTrue(tree.Delete(1)); Assert.IsFalse(tree.Find(1)); Assert.IsTrue(tree.Delete(2)); Assert.IsFalse(tree.Find(2)); Assert.IsTrue(tree.Delete(3)); Assert.IsFalse(tree.Find(3)); Assert.IsTrue(tree.Delete(4)); Assert.IsFalse(tree.Find(4)); Assert.IsTrue(tree.Delete(5)); Assert.IsFalse(tree.Find(5)); Assert.IsTrue(tree.Delete(6)); Assert.IsFalse(tree.Find(6)); Assert.AreEqual(0, tree.Size); }
private static Group <T> GetCachedGroup(GroupCollection <T> cache, Group <T> newGroup) { if (cache == null) { return((Group <T>)null); } AvlTree <Group <T> > groupList = cache.GroupList as AvlTree <Group <T> >; if (groupList == null) { return((Group <T>)null); } Group <T> group = groupList.Find(newGroup); group?.Items.Clear(); return(group); }
static void Main(string[] args) { string resultsPath = @"C:\VsProjs\lotalot\ej.csv"; IMultiLottoManager managerMain = new MultiLottoManager(resultsPath); IMultiLottoManager specMain = new FeaturesMultiLottoManager(managerMain); IList <IData> dataSet = specMain.Data; AvlTree <LottoAggregate> tree = new AvlTree <LottoAggregate>(); List <LottoAggregate> aggregates = new List <LottoAggregate>(); string[] names = new string[3]; for (int i = dataSet.Count - 1; i >= 0; i--) { LottoAggregate aggregate = new LottoAggregate(); for (int j = 0; j < 3; j++) { names[j] = GetName(dataSet, i, j); aggregate.Add(names[j], GetData(dataSet, i, j)); } tree.Insert(aggregate); aggregates.Add(aggregate); //tree.Print(); } MultiCombination <int> mainModel = (MultiCombination <int>)managerMain.Spec.CreateModel(); LottoAggregate findAggregate = new LottoAggregate(); findAggregate.Add(names[0], GetIndex(dataSet, aggregates, 0)); findAggregate.Add(names[1], GetIndex(dataSet, aggregates, 1)); findAggregate.Add(names[2], GetIndex(dataSet, aggregates, 2)); //tree.Print(); var nearAggregate = tree.Find(aggregates[0]); Print(nearAggregate.left?.value, mainModel, names[0]); Print(nearAggregate.left?.value, mainModel.Main, names[1]); Print(nearAggregate.left?.value, mainModel.Sub, names[2]); Print(nearAggregate.right?.value, mainModel, names[0]); Print(nearAggregate.right?.value, mainModel.Main, names[1]); Print(nearAggregate.right?.value, mainModel.Sub, names[2]); Print(nearAggregate.parent?.value, mainModel, names[0]); Print(nearAggregate.parent?.value, mainModel.Main, names[1]); Print(nearAggregate.parent?.value, mainModel.Sub, names[2]); Console.Read(); }
public void InsertAndFindTest() { AvlTree <int> newTree = new AvlTree <int>(); newTree.Add(40); newTree.Add(30); newTree.Add(35); newTree.Add(20); newTree.Add(50); newTree.Add(60); newTree.Add(70); newTree.Add(45); newTree.Add(46); newTree.Add(41); newTree.Add(42); AvlTreeNode <int> node = newTree.Find(70); Assert.IsNotNull(node); }
private static Group <T> GetCachedGroup(GroupCollection <T> cache, Group <T> newGroup) { if (cache != null) { AvlTree <Group <T> > avl = cache.GroupList as AvlTree <Group <T> >; if (avl == null) { return(null); } Group <T> group = avl.Find(newGroup); if (group != null) { group.Items.Clear(); } return(group); } return(null); }
protected RadTokenizedTextItem Find(string text) { AvlTree <RadTokenizedTextItem> items = this.Items as AvlTree <RadTokenizedTextItem>; RadTokenizedTextItem tokenizedTextItem1 = (RadTokenizedTextItem)null; if (items != null) { tokenizedTextItem1 = items.Find(new RadTokenizedTextItem(text, (object)null)); } else { foreach (RadTokenizedTextItem tokenizedTextItem2 in (IEnumerable <RadTokenizedTextItem>) this.Items) { if (string.Compare(tokenizedTextItem2.Text, text) == 0) { tokenizedTextItem1 = tokenizedTextItem2; break; } } } return(tokenizedTextItem1); }
/// <summary> /// Performs the grouping operation for specified items. /// </summary> /// <param name="items">The items.</param> /// <param name="level">The level.</param> /// <param name="parent">The parent.</param> /// <returns></returns> public virtual GroupCollection <T> Perform(IReadOnlyCollection <T> items, int level, Group <T> parent) { GroupDescriptorCollection groupDescriptors = this.index.CollectionView.GroupDescriptors; if (level >= groupDescriptors.Count) { return(GroupCollection <T> .Empty); } IGroupFactory <T> groupFactory = this.index.CollectionView.GroupFactory; GroupCollection <T> cache = GetCachedGroups(items, level); if (!IsValid(level, groupDescriptors)) { if (cache != null) { cache.Dispose(); } return(this.index.CollectionView.GroupFactory.CreateCollection(GroupBuilder <T> .Empty)); } IComparer <Group <T> > newComparer = Activator.CreateInstance(this.Comparer.GetType()) as IComparer <Group <T> >; if (newComparer == null) { newComparer = this.Comparer; } if (newComparer is GroupComparer <T> ) { ((GroupComparer <T>)newComparer).Directions = this.GetComparerDirections(level); } AvlTree <Group <T> > groupList = new AvlTree <Group <T> >(newComparer); AvlTree <Group <T> > list = new AvlTree <Group <T> >(new GroupComparer <T>(this.GetComparerDirections(level))); foreach (T item in items) { object key = this.GroupPredicate(item, level); Group <T> group, newGroup = new DataItemGroup <T>(key); group = list.Find(newGroup); groupList.Find(newGroup); if (group == null) { group = GroupBuilder <T> .GetCachedGroup(cache, newGroup); if (group == null) { group = groupFactory.CreateGroup(key, parent); DataItemGroup <T> dataGroup = group as DataItemGroup <T>; dataGroup.GroupBuilder = this; } list.Add(group);// groupList.Add(group); } group.Items.Add(item); } for (int i = 0; i < list.Count; i++) { groupList.Add(list[i]); } if (cache != null) { cache.Dispose(); } return(groupFactory.CreateCollection(groupList)); }
public void Insert_OneValue_ToArray() { IBinarySearchTree<int> tree = new AvlTree<int>(); tree.Insert(1); int[] sortedArray = tree.ToArray(); Assert.IsTrue(tree.Find(1)); Assert.AreEqual(1, sortedArray.Length); Assert.AreEqual(1, sortedArray[0]); }
public void Insert_OneValue() { IBinarySearchTree<int> tree = new AvlTree<int>(); tree.Insert(1); Assert.IsTrue(tree.Find(1)); }