Beispiel #1
0
        public void ShouldMapAllNodesByPerformFullTreeTraversal()
        {
            var tree = CreateValidBinaryTree();

            var  nodeMap = BinaryTreeUtil.MapAllNodes(tree, n => new Node[] { n.Left, n.Right });
            Node nodeId3 = nodeMap.FirstOrDefault(n => n.NodeValue == 3);

            Assert.IsTrue(nodeMap.Count > 0);
            Assert.IsNotNull(nodeId3);
            Assert.IsTrue(nodeId3.NodeValue == 3);
            Assert.IsTrue(nodeId3.Left.NodeValue == 1);
            Assert.IsTrue(nodeId3.Right.NodeValue == 4);
        }
Beispiel #2
0
        public void ShouldGetParentIds()
        {
            var tree         = CreateValidBinaryTree();
            var parentLookUp = new Dictionary <int, List <int> >();

            var nodeMap = BinaryTreeUtil.MapAllNodes(
                tree,
                n => new Node[] { n.Left, n.Right },
                BinaryTreeUtil.MapParentNodes(parentLookUp)
                );
            Node       nodeId3   = nodeMap.FirstOrDefault(n => n.NodeValue == 3);
            List <int> parentIds = parentLookUp[nodeId3.NodeValue];

            Assert.IsTrue(parentLookUp.Count > 0);
            Assert.IsNotNull(nodeId3);
            Assert.IsTrue(parentIds.Count == 1);
        }