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


C# BinarySearchTree.Add方法代码示例

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


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

示例1: 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

示例2: UnitTest

 public static void UnitTest()
 {
     Console.WriteLine ("----- Testing BinarySearchTree<int> -----");
     Console.Write ("Creating new BinarySearchTree of type <int>...");
     BinarySearchTree<int> test = new BinarySearchTree<int> (); d ();
     Console.Write ("Adding value 50...");
     test.Add (50); d ();
     Console.Write ("Adding value 25...");
     test.Add (25); d ();
     Console.Write ("Adding value 75...");
     test.Add (75); d ();
     Console.Write ("Checking for correct structure (25/50\\75)...");
     Debug.Assert (test.root.value == 50);
     Debug.Assert (test.root.nodes[0].value == 25);
     Debug.Assert (test.root.nodes[1].value == 75);
     d ();
     Console.Write ("Checking if BinarySearchTree.Contains(75) works...");
     Debug.Assert (test.Contains(75)); d ();
     Console.Write ("Testing BinarySearchTree.TraversePre()...");
     Debug.Assert( test.TraversePre ((x) => Console.Write (x.value + " ")) == "50 25 75 "); d ();
     Console.Write ("Testing BinarySearchTree.TraverseInOrder()...");
     Debug.Assert( test.TraverseInOrder ((x) => Console.Write (x.value + " ")) == "25 50 75 "); d ();
     Console.Write ("Testing BinarySearchTree.TraversePost()...");
     Debug.Assert( test.TraversePost ((x) => Console.Write (x.value + " ")) == "25 75 50 "); d ();
     Console.WriteLine ("----- Testing BinarySearchTree<string> -----");
     Console.Write ("Creating new BinarySearchTree of type <string> with custom IComparer...");
     BinarySearchTree<string> foo = new BinarySearchTree<string> ( new StringComparer() ); d ();
     Console.Write ("Adding value Urist McSmashes...");
     foo.Add ("Urist McSmashes"); d ();
     Console.Write ("Adding value Gordon Freeman...");
     foo.Add ("Gordon Freeman"); d ();
     Console.Write ("Adding value Winston Smith...");
     foo.Add ("Winston Smith"); d ();
     Console.Write ("Checking for correct structure (Gordon Freeman/Urist McSmashes\\Winston Smith)...");
     Debug.Assert (foo.root.value == "Urist McSmashes");
     Debug.Assert (foo.root.nodes[0].value == "Gordon Freeman");
     Debug.Assert (foo.root.nodes[1].value == "Winston Smith");
     d ();
     Console.WriteLine ("----- Testing BinaryExpressionTree -----");
     Console.Write ("Creating new BinaryExpressionTree with expression \"5 + 2 * 8 - 6 / 4\"...");
     BinaryExpressionTree bar = new BinaryExpressionTree ("5 + 2 * 8 - 6 / 4"); d ();
     Console.Write ("Checking for correct structure...");
     Debug.Assert( bar.TraverseInOrder ((x) => Console.Write (x.value + " ")) == "4 47 6 45 8 42 2 43 5 ");
     Debug.Assert( bar.root.value == 45 ); // -
     Debug.Assert( bar.root.nodes[0].value == 47 ); // /
     Debug.Assert( bar.root.nodes[0].nodes[0].value == 4 );
     Debug.Assert( bar.root.nodes[0].nodes[1].value == 6 );
     Debug.Assert( bar.root.nodes[1].value == 43 ); // +
     Debug.Assert( bar.root.nodes[1].nodes[1].value == 5 );
     Debug.Assert( bar.root.nodes[1].nodes[0].value == 42 ); // *
     Debug.Assert( bar.root.nodes[1].nodes[0].nodes[0].value == 8 );
     Debug.Assert( bar.root.nodes[1].nodes[0].nodes[1].value == 2 );
     d ();
     Console.Write ("Checking for correct answer \"5 + 2 * 8 - 6 / 4\" = " + (5.0 + 2.0 * 8.0 - 6.0 / 4.0) + "...");
     Debug.Assert (bar.Eval () == (5.0 + 2.0 * 8.0 - 6.0 / 4.0)); d ();
     Console.WriteLine ("Everything appears to be in order! Although asserts in C# don't actually end execution so there's no real way for me to know!");
 }
开发者ID:naelstrof,项目名称:2420-Binary-Trees,代码行数:57,代码来源:Program.cs

示例3: BinarySearchTreePositiveTest

 public void BinarySearchTreePositiveTest()
 {
     var bst = new BinarySearchTree<int, string>();
     bst.Add(10, "Mr. and Mrs. Iqbal Ahmed Mughal");
     bst.Add(1, "Sana");
     bst.Add(2, "Mehwish");
     bst.Add(3, "Adil");
     bst.Add(4, "Ammara");
     Assert.AreEqual("Ammara", bst.Retrieve(4));
 }
开发者ID:adilmughal,项目名称:Algorithms-and-Programming-Problems,代码行数:10,代码来源:TreeProblemsTest.cs

示例4: Simple

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

            Assert.IsFalse(tree.ContainsKey(5));

            tree.Add(4, "4");
            Assert.AreEqual(tree[4], "4");
            Assert.IsTrue(tree.ContainsKey(4));
            Assert.IsFalse(tree.ContainsKey(5));

            tree.Add(6, "6");
            Assert.AreEqual(tree[6], "6");
            Assert.IsTrue(tree.ContainsKey(4));
            Assert.IsFalse(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(6));

            tree.Add(2, "2");
            Assert.AreEqual(tree[2], "2");
            Assert.IsTrue(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(4));
            Assert.IsFalse(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(6));

            tree.Add(5, "5");
            Assert.AreEqual(tree[5], "5");
            Assert.IsTrue(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(4));
            Assert.IsTrue(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(6));

            var rand = new Random();

            tree = new BinarySearchTree<int, string>();

            var list = new List<int>();

            for (var i = 0; i < 100; i++)
            {
                int r;

                do
                {
                    r = rand.Next(5000);
                }
                while (list.Contains(r));

                list.Add(r);

                tree.Add(r, null);

                Assert.IsTrue(tree.ContainsKey(r));
            }
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:54,代码来源:Contains.cs

示例5: TestBinarySearchTreeEquals2

        public void TestBinarySearchTreeEquals2()
        {
            BinarySearchTree<int> tree1 = new BinarySearchTree<int>(1, 9, 7);

            BinarySearchTree<int> tree2 = new BinarySearchTree<int>();
            tree2.Add(7);
            tree2.Add(5);
            tree2.Add(9);

            Assert.AreEqual(false, tree1.Equals(tree2));
        }
开发者ID:nikolaynikolov,项目名称:Telerik,代码行数:11,代码来源:BinarySearchTreeTest.cs

示例6: AddTest

        public void AddTest()
        {
            var bst = new BinarySearchTree<int>() { 10, 15, 5, 4, 7, 20, 14, 0, -5, -8, -2, -1 };
            bst.Add(55);
            bst.Add(-55);
            bst.Add(35);

            // TODO: Dump as array then compare against expected array
            Assert.IsTrue(bst.Contains(55), "Did not add element correctly");
            Assert.IsTrue(bst.Contains(-55), "Did not add element correctly");
            Assert.IsTrue(bst.Contains(35), "Did not add element correctly");
        }
开发者ID:jojojojochen,项目名称:Algorithm-Implementations,代码行数:12,代码来源:BinarySearchTreeTests.cs

示例7: KeyValuePair

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

            Assert.IsFalse(tree.Contains(new KeyValuePair<int, string>(5, "5")));

            tree.Add(4, "4");
            Assert.IsTrue(tree.Contains(new KeyValuePair<int, string>(4, "4")));
            Assert.IsFalse(tree.Contains(new KeyValuePair<int, string>(4, "5")));

            tree.Add(6, "6");
            Assert.IsTrue(tree.Contains(new KeyValuePair<int, string>(6, "6")));
            Assert.IsFalse(tree.Contains(new KeyValuePair<int, string>(6, "5")));
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:14,代码来源:Contains.cs

示例8: Main

        static void Main(string[] args)
        {
            BinarySearchTree<int> tree = new BinarySearchTree<int>();
            tree.Add(2);
            tree.Add(4);
            tree.Add(1);
            tree.Add(20);
            tree.Add(15);

            foreach (var v in tree.GetEnumeratorPreorder())
            {
                Console.WriteLine(v);
            }
            Console.ReadLine();
        }
开发者ID:AlekseyKovalevsky,项目名称:ControlTest.Kovalevsky,代码行数:15,代码来源:Program.cs

示例9: 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

示例10: 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

示例11: 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

示例12: KeyValuePair

        public void KeyValuePair()
        {
            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(new KeyValuePair<int, string>(gen, gen.ToString()));

                Assert.AreEqual(tree.Count, i + 1);
                Assert.IsTrue(tree.ContainsKey(gen));
            }
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:25,代码来源:Add.cs

示例13: 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());

                string val;

                Assert.AreEqual(tree.Count, i + 1);

                tree.TryGetValue(gen, out val);
                Assert.AreEqual(val, gen.ToString());
                Assert.AreEqual(tree[gen], gen.ToString());
            }

            string val2;
            tree.TryGetValue(2001, out val2);
            Assert.IsNull(val2);
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:34,代码来源:TryGetValue.cs

示例14: Main

        static void Main()
        {
            BinarySearchTree<int> intTree = new BinarySearchTree<int>();

            intTree.Add(3);
            intTree.Add(1);
            intTree.Add(6);

            Console.WriteLine("Call intTree.ToString();");
            Console.WriteLine(intTree);

            Console.WriteLine("\nUse foreach:");

            foreach (var item in intTree)
            {
                Console.WriteLine(item);
            }

            BinarySearchTree<int> compareTree = intTree;

            Console.WriteLine("\nIs intTree equals with compareTree: {0}", compareTree.Equals(intTree));

            compareTree = new BinarySearchTree<int>();
            compareTree.Add(7);

            Console.WriteLine("\nIs intTree != with compareTree: {0}", compareTree != intTree);
            Console.WriteLine("\nIs intTree equals with compareTree: {0}", compareTree.Equals(intTree));

            Console.WriteLine("\nTest Clone");

            compareTree = intTree.Clone();

            Console.WriteLine("intTree: {0}", intTree);
            Console.WriteLine("compareTree: {0}", compareTree);

            int searchedNumber = -3;

            Console.WriteLine("\nintTree contains {0} = {1}", searchedNumber, intTree.Contains(searchedNumber));

            int deleteNumber = 3;
            intTree.Add(3);

            Console.WriteLine("\nintTree before deleting: {0}", intTree);
            Console.WriteLine("\nintTree Delete {0}, deleted elements are: {1}", deleteNumber, intTree.Delete(deleteNumber));
            Console.WriteLine("\nintTree after deleting: {0}", intTree);
        }
开发者ID:razsilev,项目名称:TelerikAcademy_Homework,代码行数:46,代码来源:MyTreeTest.cs

示例15: TestIsEmpty

        public void TestIsEmpty()
        {
            var tree = new BinarySearchTree<string>();
            Assert.IsTrue(tree.IsEmpty);

            tree.Add("a");

            Assert.IsFalse(tree.IsEmpty);
        }
开发者ID:deva666,项目名称:BinarySearchTree,代码行数:9,代码来源:BinaryTreeTests.cs


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