Exemple #1
0
        public void Sum()
        {
            var tree = new TreeOf();

            tree.Put(5)
            .Put(3)
            .Put(2)
            .Put(1);

            int result = tree.Sum();

            Assert.Equal(11, result);
        }
Exemple #2
0
        public void GetMinRecursive()
        {
            var tree = new TreeOf();
            tree.Put(5)
                .Put(7)
                .Put(4)
                .Put(3)
                .Put(2)
                .Put(8)
                .Put(1);

            Assert.Equal(1, tree.GetMinRecursive());
        }
Exemple #3
0
        public void Postorder()
        {
            var tree = new TreeOf();

            tree.Put(5)
            .Put(7)
            .Put(4);

            var result = new List <int>();

            tree.Postorder(result.Add);

            Assert.Equal(new[] { 4, 7, 5 }, result);
        }
Exemple #4
0
        public void BottomView()
        {
            var tree = new TreeOf();
            tree.Put(20)
                .Put(30)
                .Put(25)
                .Put(40)
                .Put(10)
                .Put(8)
                .Put(15);

            List<int> result = tree.BottomView();
            Assert.Equal(new[] { 25, 10, 8, 30, 40 }, result);
        }
Exemple #5
0
        public void Preoder()
        {
            var tree = new TreeOf();

            tree.Put(5)
            .Put(7)
            .Put(4);

            var result = new List <int>();

            tree.Preoder(result.Add);

            Assert.Equal(new[] { 5, 4, 7 }, result);
        }
Exemple #6
0
        public void GetMinRecursive()
        {
            var tree = new TreeOf();

            tree.Put(5)
            .Put(7)
            .Put(4)
            .Put(3)
            .Put(2)
            .Put(8)
            .Put(1);

            Assert.Equal(1, tree.GetMinRecursive());
        }
Exemple #7
0
        public void FindDeeppest()
        {
            var tree = new TreeOf();
            tree.Put(20)
                .Put(30)
                .Put(25)
                .Put(40)
                .Put(10)
                .Put(8)
                .Put(15)
                .Put(50);

            int result = tree.FindDeeppest();
            Assert.Equal(3, result);
        }
Exemple #8
0
        public void AddAllGreaterValuesToEveryNode()
        {
            var tree = new TreeOf();
            tree.Put(5)
                .Put(7)
                .Put(3)
                .Put(6)
                .Put(8)
                .Put(2)
                .Put(4);

            tree.AddAllGreaterValuesToEveryNode();

            List<int> result = tree.Select(x => x).ToList();
            Assert.Equal(new[] { 26, 33, 35, 30, 15, 21, 8 }, result);
        }
Exemple #9
0
        public void BottomView()
        {
            var tree = new TreeOf();

            tree.Put(20)
            .Put(30)
            .Put(25)
            .Put(40)
            .Put(10)
            .Put(8)
            .Put(15);

            List <int> result = tree.BottomView();

            Assert.Equal(new[] { 25, 10, 8, 30, 40 }, result);
        }
Exemple #10
0
        public void IsBinarySearchTree()
        {
            var tree = new TreeOf();

            tree.Put(20)
            .Put(30)
            .Put(25)
            .Put(40)
            .Put(10)
            .Put(8)
            .Put(15);

            bool result = tree.IsBinarySearchTree();

            Assert.True(result);
        }
Exemple #11
0
        public void FindDeeppest()
        {
            var tree = new TreeOf();

            tree.Put(20)
            .Put(30)
            .Put(25)
            .Put(40)
            .Put(10)
            .Put(8)
            .Put(15)
            .Put(50);

            int result = tree.FindDeeppest();

            Assert.Equal(3, result);
        }
Exemple #12
0
        public void AddAllGreaterValuesToEveryNode()
        {
            var tree = new TreeOf();

            tree.Put(5)
            .Put(7)
            .Put(3)
            .Put(6)
            .Put(8)
            .Put(2)
            .Put(4);

            tree.AddAllGreaterValuesToEveryNode();

            List <int> result = tree.Select(x => x).ToList();

            Assert.Equal(new[] { 26, 33, 35, 30, 15, 21, 8 }, result);
        }
Exemple #13
0
        public void HasPathSumBySum()
        {
            var tree = new TreeOf();

            tree.Put(5)
            .Put(7)
            .Put(4)
            .Put(3)
            .Put(2)
            .Put(8)
            .Put(1);

            bool result = tree.HasPathSumBySum(20);

            Assert.True(result);

            result = tree.HasPathSumBySum(21);
            Assert.False(result);
        }
Exemple #14
0
        public void VerticalSum()
        {
            var tree = new TreeOf();

            tree.Put(6)
            .Put(8)
            .Put(4)
            .Put(3)
            .Put(5)
            .Put(7)
            .Put(9);

            Dictionary <int, int> result = tree.VerticalSum();

            var expected = new Dictionary <int, int>
            {
                { 0, 18 }, { -1, 4 }, { -2, 3 }, { 1, 8 }, { 2, 9 }
            };

            Assert.Equal(expected, result);
        }
Exemple #15
0
        public void IsBinarySearchTree()
        {
            var tree = new TreeOf();
            tree.Put(20)
                .Put(30)
                .Put(25)
                .Put(40)
                .Put(10)
                .Put(8)
                .Put(15);

            bool result = tree.IsBinarySearchTree();
            Assert.True(result);
        }
Exemple #16
0
        public void Postorder()
        {
            var tree = new TreeOf();
            tree.Put(5)
                .Put(7)
                .Put(4);

            var result = new List<int>();
            tree.Postorder(result.Add);

            Assert.Equal(new[] { 4, 7, 5 }, result);
        }
Exemple #17
0
        public void Preoder()
        {
            var tree = new TreeOf();
            tree.Put(5)
                .Put(7)
                .Put(4);

            var result = new List<int>();
            tree.Preoder(result.Add);

            Assert.Equal(new[] { 5, 4, 7 }, result);
        }
Exemple #18
0
        public void Sum()
        {
            var tree = new TreeOf();
            tree.Put(5)
                .Put(3)
                .Put(2)
                .Put(1);

            int result = tree.Sum();

            Assert.Equal(11, result);
        }
Exemple #19
0
        public void VerticalSum()
        {
            var tree = new TreeOf();
            tree.Put(6)
                .Put(8)
                .Put(4)
                .Put(3)
                .Put(5)
                .Put(7)
                .Put(9);

            Dictionary<int, int> result = tree.VerticalSum();

            var expected = new Dictionary<int, int>
            {
                { 0, 18 }, { -1, 4 }, { -2, 3 }, { 1, 8 }, { 2, 9 }
            };
            Assert.Equal(expected, result);
        }
Exemple #20
0
        public void HasPathSumBySubtract()
        {
            var tree = new TreeOf();
            tree.Put(5)
                .Put(7)
                .Put(4)
                .Put(3)
                .Put(2)
                .Put(8)
                .Put(1);

            bool result = tree.HasPathSumBySubtract(20);
            Assert.True(result);

            result = tree.HasPathSumBySubtract(21);
            Assert.False(result);
        }