当前位置: 首页>>代码示例>>C#>>正文


C# BinarySearchTree类代码示例

本文整理汇总了C#中BinarySearchTree的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree类的具体用法?C# BinarySearchTree怎么用?C# BinarySearchTree使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


BinarySearchTree类属于命名空间,在下文中一共展示了BinarySearchTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: KeyValuePair

        public void KeyValuePair()
        {
            var tree = new BinarySearchTree<int, string>
                           {
                               {4, "4"},
                               {6, "6"},
                               {2, "2"},
                               {5, "5"},
                               {19, "19"},
                               {1, "1"}
                           };

            Assert.AreEqual(tree.Count, 6);

            Assert.IsTrue(tree.ContainsKey(4));
            Assert.IsTrue(tree.ContainsKey(6));
            Assert.IsTrue(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(19));
            Assert.IsTrue(tree.ContainsKey(1));

            Assert.IsFalse(tree.Remove(new KeyValuePair<int, string>(20, "20")));

            Assert.IsTrue(tree.Remove(new KeyValuePair<int, string>(4, "4")));
            Assert.AreEqual(tree.Count, 5);

            Assert.IsFalse(tree.ContainsKey(4));
            Assert.IsTrue(tree.ContainsKey(6));
            Assert.IsTrue(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(19));
            Assert.IsTrue(tree.ContainsKey(1));
        }
开发者ID:havok,项目名称:ngenerics,代码行数:33,代码来源:Remove.cs

示例2: Run

        //Ans
        //         5
        //       3    7
        //      2 4  6 8

        //Find ele at length/2 (odd) and insert , call recursively left arry and right arr.
        // if length is 1 - insert, if lngth

        public void Run()
        {
            int[] inputArr = new[] { 2, 3, 4, 6, 7, 8 };
            var newTree = new BinarySearchTree<int>();

            InsertToTree(inputArr, newTree);                  
        }
开发者ID:suhasaraos,项目名称:Test,代码行数:15,代码来源:CreateBSTUsingSortedArray.cs

示例3: BinaryTreeMustSortTheSameAsSortedDictionary

        public void BinaryTreeMustSortTheSameAsSortedDictionary()
        {
            // Arrange
            var asm = Assembly.GetExecutingAssembly();
            var importer = new Importer(asm.GetManifestResourceStream("ClientImport.Tests.records.txt"), ',');
            var dictionary = new SortedDictionary<string, IPerson>();
            var tree = new BinarySearchTree<string, IPerson>();

            //Act
            importer.Data
                           .ToList()
                           .ForEach(record =>
                           {
                               var person = new Person
                               {
                                   FirstName = record.FirstName,
                                   Surname = record.Surname,
                                   Age = Convert.ToInt16(record.Age)
                               };
                               var key = PersonRepository.BuildKey(person, SortKey.SurnameFirstNameAge);
                               if (tree.Find(key) == null)
                                   tree.Add(key, person);
                           }
                              );

            tree
                .ToList()
                .Shuffle() //Shuffle result from binary tree, to test sorting
                .ForEach(r => dictionary.Add(PersonRepository.BuildKey(r.KeyValue.Value, SortKey.SurnameFirstNameAge), r.KeyValue.Value));

            var expected = dictionary.Select(r => r.Value).ToList();
            var actual = tree.Select(n => n.KeyValue.Value).ToList();
            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }
开发者ID:Romiko,项目名称:Dev2,代码行数:35,代码来源:BinaryTreeTests.cs

示例4: Simple

        public void Simple()
        {
            var tree = new BinarySearchTree<int, string>();

            var dictionary = new Dictionary<int, string>();

            var rand = new Random(Convert.ToInt32(DateTime.Now.Ticks % Int32.MaxValue));

            for (var i = 0; i < 50; i++)
            {
                var gen = rand.Next(2000);

                while (dictionary.ContainsKey(gen))
                {
                    gen = rand.Next(2000);
                }

                dictionary.Add(gen, null);

                tree.Add(gen, gen.ToString());

                Assert.AreEqual(tree.Count, i + 1);
                Assert.IsTrue(tree.ContainsKey(gen));
            }

            using (var enumerator = dictionary.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    Assert.IsTrue(tree.Remove(enumerator.Current));
                }
            }
        }
开发者ID:havok,项目名称:ngenerics,代码行数:33,代码来源:Remove.cs

示例5: CanEnumerateDepthFirstSearch

        public void CanEnumerateDepthFirstSearch()
        {
            var tree = new BinarySearchTree<int, int>(new[] {3, 2, 1, 2, 4, 3}.AsKeyValueList());

            var expected = new[] {3, 2, 1, 2, 4, 3};
            CollectionAssert.AreEqual(expected, tree.DepthFirstSearchEnumerator().Select(_ => _.Value).ToList());
        }
开发者ID:marat-turaev,项目名称:BinarySearchTree,代码行数:7,代码来源:WhenEnumeratingTree.cs

示例6: NonIComparable

        public void NonIComparable()
        {
            var tree = new BinarySearchTree<NonComparableTClass, string>
                           {
                               {new NonComparableTClass(4), "4"},
                               {new NonComparableTClass(6), "6"},
                               {new NonComparableTClass(2), "2"},
                               {new NonComparableTClass(5), "5"},
                               {new NonComparableTClass(19), "19"},
                               {new NonComparableTClass(1), "1"}
                           };
            var newTree = SerializeUtil.BinarySerializeDeserialize(tree);

            Assert.AreNotSame(tree, newTree);
            Assert.AreEqual(tree.Count, newTree.Count);

            var treeEnumerator = tree.GetEnumerator();
            var newTreeEnumerator = newTree.GetEnumerator();

            while (treeEnumerator.MoveNext())
            {
                Assert.IsTrue(newTreeEnumerator.MoveNext());
                Assert.AreEqual(treeEnumerator.Current.Key.Number, newTreeEnumerator.Current.Key.Number);
                Assert.AreEqual(treeEnumerator.Current.Value, newTreeEnumerator.Current.Value);

                Assert.IsTrue(newTree.ContainsKey(treeEnumerator.Current.Key));
                Assert.AreEqual(newTree[treeEnumerator.Current.Key], treeEnumerator.Current.Value);
            }

            Assert.IsFalse(newTreeEnumerator.MoveNext());
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:31,代码来源:Serializability.cs

示例7: Main

        //public static void Main(string[] args)
        //{
        //DoubleLinkedList<int> list = new DoubleLinkedList<int>();
        //SingleLinkedList<int> list = new SingleLinkedList<int>();
        //int index = 5;
        //Console.WriteLine("The ADD Method and toString");
        //Add Method and toString
        //list.Add(12);
        //list.Add(20);
        //list.Add(1);
        //list.Add(0);
        //list.Add(121);
        //list.Add(420);
        //list.Add(78);
        //list.Add(90);
        //list.Add(34);
        //list.Add(4);
        //Console.WriteLine(list.ToString());
        //Console.WriteLine("\nThe COUNT Method");
        //Console.WriteLine("The the count of this list is: " + list.Count);
        //Console.WriteLine("\nThe INSERT Method");
        //Console.WriteLine("I am inserting the number 22 at index: " + index);
        //list.Insert(22, index);
        //Console.WriteLine("New List: " + list.ToString());
        //Console.WriteLine("\nThe COUNT Method");
        //Console.WriteLine("The the count of this list is: " + list.Count);
        //Console.WriteLine("\nThe Get Method");
        //Console.WriteLine("What is at index " + index + ": " + list.Get(index));
        //Console.WriteLine("\nThe REMOVEAT Method");
        //Console.WriteLine("What is at index " + (list.Count - 1) + ": " + list.Get((list.Count - 1)));
        //Console.WriteLine("I am removing the element at index " + (list.Count - 1) + ": " + list.RemoveAt((list.Count - 1)));
        //Console.WriteLine("New List: " + list.ToString());
        //Console.WriteLine("\nThe COUNT Method");
        //Console.WriteLine("The the count of this list is: " + list.Count);
        //Console.WriteLine("\nThe INSERT Method");
        //Console.WriteLine("I am inserting the number 15 at index: " + );
        //list.Insert(15,(list.Count - 1));
        //Console.WriteLine("New List: " + list.ToString());
        //Console.WriteLine("\nThe COUNT Method");
        //Console.WriteLine("The the count of this list is: " + list.Count);
        //Console.WriteLine("\nThe SEARCH Method");
        //Console.WriteLine("Searching for 1: " + list.Search(1));
        //Console.WriteLine("\nThe REMOVE Method");
        //Console.WriteLine(list.Remove());
        //Console.WriteLine("New List: " + list.ToString());
        //Console.WriteLine("\nThe COUNT Method");
        //Console.WriteLine("The the count of this list is: " + list.Count);
        //Console.WriteLine("\nThe REMOVELAST Method");
        //Console.WriteLine(list.RemoveLast());
        //Console.WriteLine("New List: " + list.ToString());
        //Console.WriteLine("\nThe COUNT Method");
        //Console.WriteLine("The the count of this list is: " + list.Count);
        //Console.WriteLine("\nThe CLEAR Method");
        //list.Clear();
        //Console.WriteLine("New List: " + list.ToString());
        //    Console.WriteLine("\nThe REMOVEAT Method");
        //    Console.WriteLine("What is at index " + index + ": " + list.Get(index));
        //    //Console.WriteLine("I am removing the element at index " + index + ": " + list.RemoveAt(index));
        //    Console.WriteLine("New List: " + list.ToString());
        //    Console.WriteLine("\nThe COUNT Method");
        //    Console.WriteLine("The the count of this list is: " + list.Count);
        //}
        public static void Main(string[] args)
        {
            BinarySearchTree<int> bst = new BinarySearchTree<int>();

            bst.Add(24);
            bst.Add(10);
            bst.Add(1337);

            //bst.Add(50);
            //bst.Add(1472);
            //bst.Add(72);
            //bst.Add(1360);
            //bst.Add(1345);
            //bst.Add(54);

            //bst.Add(20);
            //bst.Add(9);
            //bst.Add(19);
            //bst.Add(14);
            //bst.Add(67);

            //bst.Add(35);
            //bst.Add(80);
            //bst.Add(65);
            //bst.Add(24);
            //bst.Add(5);

            Console.WriteLine("Count:" + bst.Count);
            //Console.WriteLine("\ncontains 24: " + bst.Contains(24));

            //Console.WriteLine("\ncontains 0: " + bst.Contains(0));

            //Console.WriteLine("\nInorder:" + bst.Inorder());

            //Console.WriteLine("\nRemove: I'm removing 17 : " + bst.Remove(17));
            Console.WriteLine("\nInorder:" + bst.Inorder());

            Console.WriteLine("\nRemove: I'm removing 24 : " + bst.Remove(24));
//.........这里部分代码省略.........
开发者ID:NUdbrown,项目名称:Algo2Assignments,代码行数:101,代码来源:Program.cs

示例8: CopyContructorTest

        public void CopyContructorTest()
        {
            List<string> collection = new List<string> { "Granville", "Barnett", "Luca", "Del", "Tongo" };
            BinarySearchTree<string> actual = new BinarySearchTree<string>(collection);

            Assert.AreEqual(5, actual.Count);
        }
开发者ID:taoxiease,项目名称:asegrp,代码行数:7,代码来源:BinarySearchTreeTest.cs

示例9: CanFindMinimumInOneElementTree

        public void CanFindMinimumInOneElementTree()
        {
            var tree = new BinarySearchTree<int, int>();
            tree.Insert(1.AsKeyValue());

            Assert.AreEqual(1, tree.Minimum);
        }
开发者ID:marat-turaev,项目名称:BinarySearchTree,代码行数:7,代码来源:WhenSearchingMinimum.cs

示例10: ClearTest

 public void ClearTest(string[] elements)
 {
     BinarySearchTree<string> bst = new BinarySearchTree<string>(elements);
     bst.Clear();
     PexAssert.IsNull(bst.Root);
     PexAssert.AreEqual(0, bst.Count);
 }
开发者ID:taoxiease,项目名称:asegrp,代码行数:7,代码来源:BinarySearchTreeTest.cs

示例11: ContainsItemNotPresentTest

        public void ContainsItemNotPresentTest()
        {
            BinarySearchTree<int> bst = new BinarySearchTree<int> {12, 5, 3, 8, 42};

            Assert.IsFalse(bst.Contains(99));
            Assert.IsFalse(bst.Contains(1));
        }
开发者ID:taoxiease,项目名称:asegrp,代码行数:7,代码来源:BinarySearchTreeTest.cs

示例12: Check

        public bool Check(BinarySearchTree<int>.Node<int> root)
        {
            if (root == null)
                return true;

            return Check(root.Left, int.MinValue, root.Data) && Check(root.Right, root.Data, int.MaxValue);
        }
开发者ID:kanisorn,项目名称:c-sharp,代码行数:7,代码来源:BSTValidator.cs

示例13: Main

        static void Main()
        {
            var sb = new StringBuilder();

            var tree = new BinarySearchTree<int>(15);

            for (int i = 0; i < 30; i++)
            {
                tree.Add(i);
            }

            var clonedNode = (TreeNode<int>)tree.Root.Clone();

            sb.AppendLine(tree.ToString())
                .AppendLine("Tree root: " + tree.Root.ToString())
                .AppendLine("Tree contains 7? " + tree.Contains(7).ToString())
                .AppendLine("Cloned root: " + clonedNode.ToString())
                .AppendLine("Cloned Equals root? " + (clonedNode.Equals(tree.Root)).ToString())
                .AppendLine("Cloned == root? " + (clonedNode == tree.Root).ToString())
                .AppendLine("Cloned != root? " + (clonedNode != tree.Root).ToString())
                .AppendLine("12 deleted. New tree:");

            Console.Write(sb.ToString());

            tree.Delete(12);
            Console.WriteLine(tree.ToString());
        }
开发者ID:AYankova,项目名称:CSharp,代码行数:27,代码来源:Program.cs

示例14: Main

        static void Main()
        {
            BinarySearchTree<int> tree = new BinarySearchTree<int>();
            tree.AddElement(13);
            tree.AddElement(17);
            tree.AddElement(101);
            tree.AddElement(44);
            tree.AddElement(9);
            tree.AddElement(11);

            BinarySearchTree<int> tree2 = (BinarySearchTree<int>)tree.Clone();
            //tree2.AddElement(45);
            //tree2.AddElement(99);

            Console.WriteLine("Tree: " + tree.ToString());
            Console.WriteLine("Tree2: " + tree2.ToString());
            Console.WriteLine("Tree equals Tree2: " + tree.Equals(tree2));
            Console.WriteLine("Tree == Tree2: " + (tree == tree2));
            Console.WriteLine("Tree != Tree2: " + (tree != tree2));

            Console.Write("Traverse with foreach: ");
            foreach (TreeNode<int> item in tree)
                Console.Write(item.Value + " ");

            Console.WriteLine();

            Console.WriteLine("Tree hash: {0}", tree.GetHashCode());
            Console.WriteLine("Tree2 hash: {0}", tree2.GetHashCode());
        }
开发者ID:Rokata,项目名称:TelerikAcademy,代码行数:29,代码来源:TreeDataStructure.cs

示例15: CanFindElementInOneElementTree

        public void CanFindElementInOneElementTree()
        {
            var tree = new BinarySearchTree<int, int>();
            tree.Insert(new KeyValue<int, int>(1, 2));

            Assert.AreEqual(2, tree[1]);
        }
开发者ID:marat-turaev,项目名称:BinarySearchTree,代码行数:7,代码来源:WhenSearching.cs


注:本文中的BinarySearchTree类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。