Beispiel #1
0
        public void DeleteNodeWithTwoChildren()
        {
            var t = new Tree <int>();

            t.Add(22);
            t.Add(20);
            t.Add(25);
            t.Add(24);
            t.Add(33);
            t.Add(10);
            t.Add(7);
            t.Add(8);
            t.Add(23);
            t.Add(35);

            t.Delete(25);

            Assert.AreEqual(22, t.Root.Item);
            Assert.AreEqual(20, t.Root.Left.Item);
            Assert.AreEqual(10, t.Root.Left.Left.Item);
            Assert.AreEqual(8, t.Root.Left.Left.Left.Right.Item);
            Assert.AreEqual(7, t.Root.Left.Left.Left.Item);

            Assert.AreEqual(24, t.Root.Right.Item);
            Assert.AreEqual(23, t.Root.Right.Left.Item);
            Assert.AreEqual(33, t.Root.Right.Right.Item);
            Assert.AreEqual(35, t.Root.Right.Right.Right.Item);

            Assert.IsNull(t.Find(25));
        }
Beispiel #2
0
        public void DeleteNodeWithSingleChild_RightParentLeftChild()
        {
            var t = new Tree <int>();

            t.Add(22);
            t.Add(20);
            t.Add(25);
            t.Add(24);
            t.Add(33);
            t.Add(10);
            t.Add(7);
            t.Add(8);
            t.Add(23);
            t.Add(27);

            t.Delete(33);

            Assert.AreEqual(22, t.Root.Item);
            Assert.AreEqual(20, t.Root.Left.Item);
            Assert.AreEqual(10, t.Root.Left.Left.Item);
            Assert.AreEqual(8, t.Root.Left.Left.Left.Right.Item);
            Assert.AreEqual(24, t.Root.Right.Left.Item);
            Assert.AreEqual(25, t.Root.Right.Item);
            Assert.AreEqual(23, t.Root.Right.Left.Left.Item);
            Assert.AreEqual(7, t.Root.Left.Left.Left.Item);
            Assert.AreEqual(27, t.Root.Right.Right.Item);
            Assert.IsNull(t.Find(33));
        }
Beispiel #3
0
        public void DeleteRootWithNoChildrenShouldSetRootToNull()
        {
            var t = new Tree <int>();

            t.Add(22);

            t.Delete(22);

            Assert.IsNull(t.Root);
        }
Beispiel #4
0
        public void DeleteRootOnlyOneChildSetsChildAsRoot_Right()
        {
            var t = new Tree <int>();

            t.Add(23);
            t.Add(28);

            t.Delete(23);

            Assert.AreEqual(28, t.Root.Item);
            Assert.IsNull(t.Root.Left);
            Assert.IsNull(t.Root.Right);
        }
Beispiel #5
0
        private static void Main()
        {
            Tree <int> tree = new Tree <int>();

            tree.Add(5);
            tree.Add(3);
            tree.Add(7);
            tree.Add(1);
            tree.Add(2);
            tree.Add(8);
            tree.Add(6);
            tree.Add(9);
            tree.Add(4);

            Console.WriteLine(tree.DisplayNode());

            Console.WriteLine($"Max element of tree: {tree.FindMax(tree.Root)}.");
            Console.WriteLine($"Min element of tree: {tree.FindMin(tree.Root)}.");
            Console.WriteLine(tree.Search(5));

            tree.Delete(8);

            Console.Write("Preorder traversal: ");
            foreach (int item in tree.Preorder())
            {
                Console.Write(item + ", ");
            }

            Console.Write("\nPostorder traversal: ");
            foreach (int item in tree.Postorder())
            {
                Console.Write(item + ", ");
            }

            Console.Write("\nInorder traversal: ");
            foreach (int item in tree.Inorder())
            {
                Console.Write(item + ", ");
            }
        }
Beispiel #6
0
        public void DeleteRootTwoChildren()
        {
            var t = new Tree <int>();

            t.Add(23);
            t.Add(20);
            t.Add(25);
            t.Add(24);
            t.Add(33);
            t.Add(10);
            t.Add(7);
            t.Add(8);

            t.Delete(23);

            Assert.AreEqual(20, t.Root.Item);
            Assert.AreEqual(10, t.Root.Left.Item);
            Assert.AreEqual(7, t.Root.Left.Left.Item);
            Assert.AreEqual(8, t.Root.Left.Left.Right.Item);
            Assert.AreEqual(25, t.Root.Right.Item);
            Assert.AreEqual(24, t.Root.Right.Left.Item);
            Assert.AreEqual(33, t.Root.Right.Right.Item);
        }
Beispiel #7
0
        public void DeleteLeaf_RightChild()
        {
            var t = new Tree <int>();

            t.Add(23);
            t.Add(20);
            t.Add(25);
            t.Add(24);
            t.Add(33);
            t.Add(10);
            t.Add(7);
            t.Add(8);

            t.Delete(33);

            Assert.AreEqual(23, t.Root.Item);
            Assert.AreEqual(20, t.Root.Left.Item);
            Assert.AreEqual(10, t.Root.Left.Left.Item);
            Assert.AreEqual(8, t.Root.Left.Left.Left.Right.Item);
            Assert.AreEqual(7, t.Root.Left.Left.Left.Item);
            Assert.AreEqual(25, t.Root.Right.Item);
            Assert.AreEqual(24, t.Root.Right.Left.Item);
            Assert.IsNull(t.Find(33));
        }