示例#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);
        }
示例#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());
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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());
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }