當前位置: 首頁>>代碼示例>>C#>>正文


C# BinarySearchTree.Delete方法代碼示例

本文整理匯總了C#中BinarySearchTree.Delete方法的典型用法代碼示例。如果您正苦於以下問題:C# BinarySearchTree.Delete方法的具體用法?C# BinarySearchTree.Delete怎麽用?C# BinarySearchTree.Delete使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在BinarySearchTree的用法示例。


在下文中一共展示了BinarySearchTree.Delete方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

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

示例2: BST_Delete

        public void BST_Delete()
        {
            var BST = new BinarySearchTree<int, object>();
            BST[4] = 0; BST[3] = 0; BST[2] = 0; BST[1] = 0;
            BST[5] = 0; BST[6] = 0; BST[7] = 0;

            BST.Delete(4);
            BST.Delete(6);

            var keys = BST.Enumerator();
            string actual = "";
            foreach (var item in keys)
            {
                actual += item.ToString();
            }
            Assert.AreEqual("12357", actual);
        }
開發者ID:pbisadi,項目名稱:Algorithms,代碼行數:17,代碼來源:SearchTree_UnitTest.cs

示例3: DeleteTest

 public void DeleteTest()
 {
     BinarySearchTree target = new BinarySearchTree(); // TODO: Initialize to an appropriate value
     int key = 0;
     BinarySearchTree.Node delNode = target.Search(key);
     target.Delete(delNode);
     Assert.Inconclusive("Verify the correctness of this test method.");
 }
開發者ID:jamesjrg,項目名稱:taipan,代碼行數:8,代碼來源:BinarySearchTreeTest.cs

示例4: CanDeleteFromMiddleWithRightChild

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

            tree.Delete(2);

            CollectionAssert.AreEqual(new[] {1, 3}, tree.BreadthFirstSearchEnumerator().Select(_ => _.Value).ToList());
        }
開發者ID:marat-turaev,項目名稱:BinarySearchTree,代碼行數:11,代碼來源:WhenDeletingNode.cs

示例5: CanDeleteLeafNode

 public void CanDeleteLeafNode()
 {
     //Arrange
     var tree = new BinarySearchTree<int, int> { { 2, 2 }, { 1, 1 }, { 3, 3 }, { 4, 4 } };
     var expected = new[] { 1, 2, 3 }.ToList();
     //Act
     tree.Delete(4);
     var result = tree.Select(r => r.KeyValue.Value).ToList();
     //Assert
     CollectionAssert.AreEquivalent(expected, result);
 }
開發者ID:Romiko,項目名稱:Dev2,代碼行數:11,代碼來源:BinaryTreeTests.cs

示例6: CanDeleteFromLeftLeaf

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

            tree.Delete(1);

            CollectionAssert.AreEqual(new[] {3, 2}, tree.BreadthFirstSearchEnumerator().Select(_ => _.Value).ToList());
        }
開發者ID:marat-turaev,項目名稱:BinarySearchTree,代碼行數:11,代碼來源:WhenDeletingNode.cs

示例7: Main

    static void Main()
    {
        BinarySearchTree tree = new BinarySearchTree();
        tree.Add(2);
        tree.Add(5);
        tree.Add(10);
        tree.Add(6);
        tree.Add(4);
        tree.Add(15);

        tree.Delete(10);
        tree.Delete(15);
        Console.WriteLine(tree.Search(6));
        Console.WriteLine(tree.Search(10));

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

        BinarySearchTree secondTree = new BinarySearchTree();
        secondTree.Add(2);
        secondTree.Add(5);
        secondTree.Add(10);
        secondTree.Add(6);
        secondTree.Add(4);
        secondTree.Add(15);

        secondTree.Delete(10);
        secondTree.Delete(15);

        Console.WriteLine(tree.Equals(secondTree));
        Console.WriteLine(Object.ReferenceEquals(tree, secondTree));
        Console.WriteLine(tree.GetHashCode() == secondTree.GetHashCode());
        Console.WriteLine();

        BinarySearchTree clonedTree = (BinarySearchTree)tree.Clone();
        Console.WriteLine(tree.Equals(clonedTree));
        Console.WriteLine(Object.ReferenceEquals(tree, clonedTree));
    }
開發者ID:shnogeorgiev,項目名稱:Software-University-Courses,代碼行數:41,代碼來源:CustomTreeExample.cs

示例8: Delete

        public void Delete()
        {
            var tree = new BinarySearchTree<int>();
            var firstNode = new TNode<int>(17);
            tree.Insert(firstNode);
            var secondNode = new TNode<int>(7);
            tree.Insert(secondNode);
            var thirdNode = new TNode<int>(4);
            tree.Insert(thirdNode);
            var fourthNode = new TNode<int>(15);
            tree.Insert(fourthNode);
            tree.Insert(new TNode<int>(10));
            tree.Insert(new TNode<int>(16));
            tree.Insert(new TNode<int>(8));
            tree.Insert(new TNode<int>(12));

            tree.Delete(secondNode);
            var result = tree.CollectInOrder().ToArray();
            var expected = new int[] {4,8,10,12,15,16,17};
            for(int i=0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], result[i], "#E"+i);
            }

            tree.Delete(thirdNode);
            result = tree.CollectInOrder().ToArray();
            expected = new int[] {8, 10, 12, 15, 16, 17 };
            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], result[i], "#EE" + i);
            }

            tree.Delete(firstNode);
            result = tree.CollectInOrder().ToArray();
            expected = new int[] { 8, 10, 12, 15, 16};
            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], result[i], "#EEE" + i);
            }
        }
開發者ID:bpatra,項目名稱:sandbox,代碼行數:40,代碼來源:BinarySearchTreeTests.cs

示例9: RunBST

        private static void RunBST()
        {
            var values = new[] { 15, 6, 20, 3, 7, 17, 19, 22, 2, 4, 13, 9 };

            Console.WriteLine($"Building binary tree from: {Utils.FormatArray(values)} =>\n");

            var tree = new BinarySearchTree<int>();
            foreach (var v in values)
            {
                tree.Add(v);
            }

            Utils.PrintBinaryTree(tree.Root);

            Console.WriteLine($"Minimum : {tree.Minimum().Value}");
            Console.WriteLine($"Maximum : {tree.Maximum().Value}");

            var searchValues = new[] { 9, 3, 100, 20, 2 };
            Console.WriteLine($"Searching values {Utils.FormatArray(searchValues)}");
            foreach(var v in searchValues)
            {
                var node = tree.Search(v);
                Console.Write("The tree {0} value {1}. ",
                    node == null ? "does not contain" : "contains", v);

                if (node != null)
                {
                    var successor = BinarySearchTree<int>.Successor(node);
                    Console.Write("Successor {0}. ", successor != null ? successor.Value.ToString() : "N/A");
                    var predecessor = BinarySearchTree<int>.Predecessor(node);
                    Console.Write("Predecessor {0}.", predecessor != null ? predecessor.Value.ToString() : "N/A");
                }

                Console.WriteLine();
            }

            var deleteValues = new [] { 15 };
            foreach (var v in deleteValues)
            {
                var node = tree.Search(v);
                Console.WriteLine($"Deleting value: {v}");
                tree.Delete(node);
                Utils.PrintBinaryTree(tree.Root);
                Console.WriteLine();
            }
        }
開發者ID:moozzyk,項目名稱:AlgorithmsInCSharp,代碼行數:46,代碼來源:BinarySearchTreeRunner.cs

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

示例11: Main

    static void Main(string[] args)
    {
        BinarySearchTree<int> tree = new BinarySearchTree<int>();

        tree.Add(4);
        tree.Add(3);
        tree.Add(1);
        tree.Add(100);
        tree.Add(5);

        BinarySearchTree<int> newTree = (BinarySearchTree<int>)tree.Clone();

        tree.Delete(4);

        Console.WriteLine(tree);
        Console.WriteLine(newTree);

        Console.WriteLine(tree.Search(100));

        Console.WriteLine(tree.Equals(newTree));
    }
開發者ID:kalinnikol,項目名稱:TelerikAcademy-1,代碼行數:21,代碼來源:Program.cs

示例12: CanDeleteRootNodeWithTwoChildrenNestedBinaryTreeUseSuccessor

 public void CanDeleteRootNodeWithTwoChildrenNestedBinaryTreeUseSuccessor()
 {
     //Arrange
     var tree = new BinarySearchTree<int, int> { { 100, 100 }, { 200, 200 }, { 50, 50 }, { 40, 40 }, { 25, 25 }, { 26, 26 }, { 60, 60 }, { 20, 20 } };
     tree.ForceDeleteType = InOrderNode.Successor;
     var expected = new[] { 20, 25, 26, 40, 50, 60, 200 }.ToList();
     //Act
     tree.Delete(100);
     var result = tree.Select(r => r.KeyValue.Value).ToList();
     //Assert
     CollectionAssert.AreEquivalent(expected, result);
 }
開發者ID:Romiko,項目名稱:Dev2,代碼行數:12,代碼來源:BinaryTreeTests.cs

示例13: CanDeleteNodeWithOneLeftChild

 public void CanDeleteNodeWithOneLeftChild()
 {
     //Arrange
     var tree = new BinarySearchTree<int, int> { { 9, 9 }, { 1, 1 }, { 3, 3 }, { 2, 2 }, { 10, 10 }, { 11, 11 } };
     var expected = new[] { 1, 2, 9, 10, 11 }.ToList();
     //Act
     tree.Delete(3);
     var result = tree.Select(r => r.KeyValue.Value).ToList();
     //Assert
     CollectionAssert.AreEquivalent(expected, result);
 }
開發者ID:Romiko,項目名稱:Dev2,代碼行數:11,代碼來源:BinaryTreeTests.cs

示例14: Main

    public static void Main()
    {
        //Create binary search tree
        BinarySearchTree<int> firstTree = new BinarySearchTree<int>();
        firstTree.Root = new TreeNode<int>(10);
        firstTree.Root.Left = new TreeNode<int>(5);
        firstTree.Root.Right = new TreeNode<int>(20);

        firstTree.Root.Left.Left = new TreeNode<int>(1);
        firstTree.Root.Right.Right = new TreeNode<int>(24);

        firstTree.Root.Left.Left.Right = new TreeNode<int>(3);
        firstTree.Root.Right.Right.Right = new TreeNode<int>(35);

        firstTree.Root.Right.Right.Right.Right = new TreeNode<int>(50);

        //Test ToString() method
        Console.WriteLine("Elements of firstTree");
        Console.WriteLine(firstTree.ToString());

        //Create another binary search tree
        BinarySearchTree<int> secondTree = new BinarySearchTree<int>();
        secondTree.Root = new TreeNode<int>(10);
        secondTree.Root.Left = new TreeNode<int>(5);
        secondTree.Root.Right = new TreeNode<int>(20);

        secondTree.Root.Left.Left = new TreeNode<int>(1);
        secondTree.Root.Right.Right = new TreeNode<int>(24);

        secondTree.Root.Left.Left.Right = new TreeNode<int>(3);
        secondTree.Root.Right.Right.Right = new TreeNode<int>(35);

        secondTree.Root.Right.Right.Right.Right = new TreeNode<int>(49);

        //Test Equals() method
        Console.WriteLine("firstTree is equal to secondTree?:");
        Console.WriteLine(firstTree.Equals(secondTree));

        //Test Clone() method
        Console.WriteLine("Elements of the clone of firstTree");
        BinarySearchTree<int> clone = (BinarySearchTree<int>)firstTree.Clone();
        Console.WriteLine(clone.ToString());

        //Test Search() method
        Console.WriteLine("firstTree contains 50?:");
        Console.WriteLine(firstTree.Search(50));
        Console.WriteLine("secondTree contains 50?:");
        Console.WriteLine(secondTree.Search(50));

        //Test Add() method
        Console.WriteLine("Elements of firstTree after adding 22");
        firstTree.Add(22);
        Console.WriteLine(firstTree.ToString());

        //Test Delete() method
        Console.WriteLine("Elements of secondTree after deleting 24");
        Console.WriteLine(secondTree.Delete(24));
        Console.WriteLine(secondTree.ToString());

        //Use foreach to test the implementation of IEnumerable<T> interface
        clone.Add(100);
        Console.WriteLine("firstTree after adding 100 to clone");
        foreach (var item in firstTree)
        {
            Console.Write("{0} ", item);
        }

        Console.WriteLine("clone after adding 100 to clone");
        foreach (var item in clone)
        {
            Console.Write("{0} ", item);
        }
    }
開發者ID:vasilkrvasilev,項目名稱:ObjectOrientedProgramming,代碼行數:73,代碼來源:Start.cs

示例15: Delete_NodeIsNotRootAndHasTwoChildren_HasCorrectOrder

		public void Delete_NodeIsNotRootAndHasTwoChildren_HasCorrectOrder()
		{
			testTree = new BinarySearchTree<int>(new[] { 5, 3, 2, 4, 7, 6, 8 });

			testTree.Delete(7);
			Debug.WriteLine(testTree.PreOrder());
			Assert.IsTrue(IsCorrectOrder(testTree.PreOrder(), "532486"));
		}
開發者ID:natejenson,項目名稱:SharpTree,代碼行數:8,代碼來源:BinaryTreeTests.cs


注:本文中的BinarySearchTree.Delete方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。