Exemple #1
0
    // Use this for initialization
    void Start()
    {
        BinaryTree <int> sampleTree = new BinaryTree <int>(42);

        BinaryTreeNode <int> left  = sampleTree.Root().AddChild(5);
        BinaryTreeNode <int> right = sampleTree.Root().AddChild(17);

        left.AddChild(-6);
        left.AddChild(12);

        right.AddChild(128);
        right.AddChild(1024);

        BinaryTreeNode <int>         treeRoot = sampleTree.Root();
        List <BinaryTreeNode <int> > leaves   = new List <BinaryTreeNode <int> >();

        CollectLeaves(treeRoot, leaves);

        foreach (BinaryTreeNode <int> leaf in leaves)
        {
            print("Leaf found with value " + leaf.Value() + " and parent value " + leaf.parent.Value());

            int currentLeafSum = CountFromNodeToRoot(leaf);
            print("Sum of root is " + currentLeafSum);
        }
    }
Exemple #2
0
    }//end vertical split

    private void CreatePartitionHorizontal(BinaryTreeNode<RectInt> nodeToSplit, out BinaryTreeNode<RectInt> topChild, out BinaryTreeNode<RectInt> bottomChild)
    {
        int horizSplitNodeX = 0;
        int horizSplitNodeY = 0;
        int horizSplitNodeWidth = nodeToSplit.Value().width;
        int horizSplitNodeHeight = nodeToSplit.Value().height / 2;

        RectInt newHorizNodeTop = new RectInt(horizSplitNodeX, horizSplitNodeHeight, horizSplitNodeWidth, nodeToSplit.Value().height - horizSplitNodeHeight);
        RectInt newHorizNodeBottom = new RectInt(horizSplitNodeX, horizSplitNodeY, horizSplitNodeWidth, horizSplitNodeHeight);

        //set children
        topChild = nodeToSplit.AddChild(newHorizNodeTop);
        bottomChild = nodeToSplit.AddChild(newHorizNodeBottom);

        //add to list of partitions
        partitions.Add(topChild);
        partitions.Add(bottomChild);

        //create corridor and transfer to world coors
        RectInt horizCoor = CreateHorizCoors(bottomChild);
        //bottomChild.AddCoor(horizCoor);

        //debug stuff
        print("Top: " + newHorizNodeTop);
        print("Bottom: " + newHorizNodeBottom);
    }//end horizontal split
Exemple #3
0
    }//end recursive split

    private void CreatePartitionVertical(BinaryTreeNode<RectInt> nodeToSplit, out BinaryTreeNode<RectInt> leftChild, out BinaryTreeNode<RectInt> rightChild)
    {
        int vertSplitNodeX = 0;
        int vertSplitNodeY = 0;
        int vertSplitNodeWidth = nodeToSplit.Value().width / 2;
        int vertSplitNodeHeight = nodeToSplit.Value().height;

        RectInt newVertNodeLeft = new RectInt(vertSplitNodeX, vertSplitNodeY, vertSplitNodeWidth, vertSplitNodeHeight);
        RectInt newVertNodeRight = new RectInt(vertSplitNodeWidth, vertSplitNodeY, nodeToSplit.Value().width - vertSplitNodeWidth, vertSplitNodeHeight);

        //set children
        leftChild = nodeToSplit.AddChild(newVertNodeLeft);
        rightChild = nodeToSplit.AddChild(newVertNodeRight);

        //add to list of partitions
        partitions.Add(leftChild);
        partitions.Add(rightChild);

        //create coors
        RectInt vertCoor = CreateVertCoors(leftChild);
        //leftChild.AddCoor(vertCoor);

        //debug stuff
        print("LeftSide: " + newVertNodeLeft);
        print("RightSide: " + newVertNodeRight);
    }//end vertical split
        public void BinaryTreeNodeIsFullTest()
        {
            BinaryTreeNode <int> node = new BinaryTreeNode <int>(1);

            Assert.IsFalse(node.IsFull);
            node.AddChild(new BinaryTreeNode <int>(2));
            Assert.IsFalse(node.IsFull);
            node.AddChild(new BinaryTreeNode <int>(3));
            Assert.IsTrue(node.IsFull);
        }
    private void SplitHorizontally(BinaryTreeNode <RectInt> node)
    {
        int partitionWidth        = node.Value().width;
        int bottomPartitionHeight = node.Value().height / 2;

        RectInt bottomPartitionRect = new RectInt(0, 0, partitionWidth, bottomPartitionHeight);

        RectInt topPartitionRect = new RectInt(0, bottomPartitionHeight, partitionWidth, node.Value().height - bottomPartitionHeight);

        node.AddChild(topPartitionRect);
        node.AddChild(bottomPartitionRect);
    }
    private void SplitVertically(BinaryTreeNode <RectInt> node)
    {
        int leftPartitionWidth = node.Value().width / 2;
        int partitionHeight    = node.Value().height;

        RectInt leftPartitionRect = new RectInt(0, 0, leftPartitionWidth, partitionHeight);

        RectInt rightPartitionRect = new RectInt(leftPartitionWidth, 0, node.Value().width - leftPartitionWidth, partitionHeight);

        node.AddChild(leftPartitionRect);
        node.AddChild(rightPartitionRect);
    }
        public void BinaryTreeNodeAddChildTest()
        {
            BinaryTreeNode <int> node = new BinaryTreeNode <int>(1);

            Assert.IsNull(node.Left);
            Assert.IsNull(node.Right);
            node.AddChild(new BinaryTreeNode <int>(2));
            Assert.IsNotNull(node.Left);
            Assert.IsNull(node.Right);
            node.AddChild(new BinaryTreeNode <int>(3));
            Assert.IsNotNull(node.Left);
            Assert.IsNotNull(node.Right);
        }
Exemple #8
0
        public void AddChild_ShouldProduceDesiredResults_ForTargetContainingChildNodeArgument()
        {
            // Arrange.
            var childNode = new BinaryTreeNode <Int32>();
            var target    = new BinaryTreeNode <Int32>(0);

            target.AddChild(childNode);

            // Act.
            var result = target.AddChild(childNode);

            // Assert.
            result.Should().BeFalse();
        }
        public void BinaryTreeNodeInsertTest()
        {
            BinaryTreeNode <int> n = new BinaryTreeNode <int>(1);

            n.AddChild(2);
            n.AddChild(3);
            Assert.AreEqual(2, n.Left.Value);
            Assert.AreEqual(3, n.Right.Value);
            n.InsertLeft(5);
            n.InsertRight(7);
            Assert.AreEqual(5, n.Left.Value);
            Assert.AreEqual(7, n.Right.Value);
            Assert.AreEqual(2, n.Left.Left.Value);
            Assert.IsNull(n.Left.Right);
            Assert.IsNull(n.Right.Left);
            Assert.AreEqual(3, n.Right.Right.Value);
        }
Exemple #10
0
    private BinaryTreeNode <RectInt> MakeRoom(int x, int y, BinaryTreeNode <RectInt> parentNode, BinaryTreeNode <RectInt> nodeName, RectInt roomName)
    {
        int     width  = parentNode.Value().width - 2;
        int     height = parentNode.Value().height - 2;
        RectInt room1  = new RectInt(1, 1, width, height);

        nodeName = parentNode.AddChild(room1);
        return(nodeName);
    }
Exemple #11
0
    private BinaryTreeNode <RectInt> SplitFromLeaf(int x, int y, BinaryTreeNode <RectInt> parentNode, BinaryTreeNode <RectInt> nodeName, RectInt partName)
    {
        int width  = parentNode.Value().width;
        int height = parentNode.Value().height / 2;

        partName = new RectInt(x, y, width, height);
        nodeName = parentNode.AddChild(partName);
        return(nodeName);
    }
    private void MakeRooms(BinaryTreeNode <RectInt> node)
    {
        //if (node.Value().width > 4 && node.Value().height > 4)
        //{
        int roomWidth  = node.Value().width - 2;
        int roomHeight = node.Value().height - 2;

        RectInt room = new RectInt(1, 1, roomWidth, roomHeight);

        node.AddChild(room);

        //}
    }
Exemple #13
0
        public void AddChild_ShouldProduceDesiredResults_ForTargetNotContainingChildNodeArgument()
        {
            // Arrange.
            var childNode = new BinaryTreeNode <Int32>();
            var target    = new BinaryTreeNode <Int32>();

            // Act.
            var result = target.AddChild(childNode);

            // Assert.
            result.Should().BeTrue();
            target.Children.Should().Contain(childNode);
            childNode.Parent.Should().BeSameAs(target);
        }
Exemple #14
0
        public void AddChild_ShouldRaiseArgumentNullException_ForNullChildNodeArgument()
        {
            // Arrange.
            var childNode = (BinaryTreeNode <Int32>)null;
            var target    = new BinaryTreeNode <Int32>();

            // Act.
            var action = new Action(() =>
            {
                target.AddChild(childNode);
            });

            // Assert.
            action.Should().Throw <ArgumentNullException>($"because {nameof(childNode)} is null");
        }
Exemple #15
0
    }//end horizontal split

    private void CreateRoom(BinaryTreeNode<RectInt> nodeToRommify, List<BinaryTreeNode<RectInt>> roomNodes)
    {
        int roomWidth = nodeToRommify.Value().width - 2;
        int roomHeight = nodeToRommify.Value().height - 2;
        int roomXStart = 1;
        int roomYStart = 1;

        //create a room based off of the current node's(partition's) dimensions
        if (nodeToRommify.leftChild == null && nodeToRommify.rightChild == null)
        {
            //generate a room inside the chosen node
            RectInt room = new RectInt(roomXStart, roomYStart, roomWidth, roomHeight);
            BinaryTreeNode<RectInt> roomNode = nodeToRommify.AddChild(room);
            //LocalNodeCoorsToWorldCoors(roomNode);
            roomNodes.Add(roomNode);
        }
    }//end createRoom
    void Start()
    {
        // TODO: Use the BinaryTree class to demonstrate its correctness
        BinaryTree <int> sampleTree = new BinaryTree <int>(42);

        BinaryTreeNode <int> left  = sampleTree.Root().AddChild(5);
        BinaryTreeNode <int> right = sampleTree.Root().AddChild(17);

        left.AddChild(-6);
        left.AddChild(12);

        right.AddChild(128);
        right.AddChild(1024);

        // Now what?
        BinaryTreeNode <int>         treeRoot = sampleTree.Root();
        List <BinaryTreeNode <int> > leaves   = new List <BinaryTreeNode <int> >();

        CollectLeaves(treeRoot, leaves);
        // At this point, <leaves> should contain all the childless nodes in the tree

        foreach (BinaryTreeNode <int> leaf in leaves)
        {
            Debug.Log("Leaf found with value " + leaf.Value() + " and parent value " + leaf.parent.Value());
            int currentLeafSum = CountFromNodeToRoot(leaf);
            Debug.Log("Current leaf sum: " + currentLeafSum);
        }

        #region NotNeededAnymore

        /** Not needed anymore
         * BinaryTreeNode<int> current;
         *
         * current = leftOfLeft;
         *
         * int leftofLeftSum = 0;
         * while (current != null)
         * {
         * leftofLeftSum += current.Value();
         * current = current.parent;
         *
         * } // end current null
         * // leftOfSum expected = 41
         * Debug.Log("leftOfSum = " + leftofLeftSum);
         *
         * int rightOfLeftSum = 0;
         * current = rightOfLeft;
         *
         * while (current != null)
         * {
         * rightOfLeftSum += current.Value();
         * current = current.parent;
         *
         * } // end current null
         * // rightOfLeftSum expected = 59
         * Debug.Log("rightOfLeftSum = " + rightOfLeftSum);
         */

        //Debug.Log("leftOfLeft: " + CountFromNodeToRoot(leftOfLeft));
        //Debug.Log("righOfLeft: " + CountFromNodeToRoot(rightOfLeft));
        //Debug.Log("leftOfRight: " + CountFromNodeToRoot(leftOfRight));
        //Debug.Log("rightOfRight: " + CountFromNodeToRoot(rightOfRight));

        #endregion
    } // end Start
Exemple #17
0
    // Use this for initialization
    void Start()
    {
        //todo:
        //use BinaryTree class to demonsrate its correctness
        BinaryTree <int> sampleTree = new BinaryTree <int>(42);

        BinaryTreeNode <int> left  = sampleTree.Root().AddChild(5);
        BinaryTreeNode <int> right = sampleTree.Root().AddChild(17);

        left.AddChild(-6);
        left.AddChild(12);

        right.AddChild(128);
        right.AddChild(1024);

        //now:
        BinaryTreeNode <int>         treeRoot = sampleTree.Root();
        List <BinaryTreeNode <int> > leaves   = new List <BinaryTreeNode <int> >();

        CollectLeaves(treeRoot, leaves);
        //at this piont <leaves> nshould conatain all chldless nodes in the tree

        foreach (BinaryTreeNode <int> leaf in leaves)
        {
            print("Leaf found with value: " + leaf.Value() + " and parent value: " + leaf.parent.Value());
            int currentLeafSum = CountFromNodeToRoot(leaf);
            print("sum to root is: " + currentLeafSum);
        }

        /*
         * //task: find the sum from all the leaf nodes (dumb way)
         * //BinaryTreeNode<int> current;
         *
         * //int leftofLeftSum = 0;
         * //current = leftofLeft;
         * //while(current != null)
         * //{
         * //    leftofLeftSum += current.Value();
         * //    current = current.parent;
         * //}
         * ////left of left sum shpould be 41
         * //print("L.O.L SUM = " + leftofLeftSum);
         *
         * //int rightofleftSum = 0;
         * //current = rightofLeft;
         * //while (current != null)
         * //{
         * //    rightofleftSum += current.Value();
         * //    current = current.parent;
         * //}
         * ////left of left sum shpould be 59
         * //print("R.O.L SUM = " + rightofleftSum);
         *
         * ////another way
         * //int leftOfLeftSum = CountFromNodeToRoot(leftofLeft);
         * //int rightOfLeftSum = CountFromNodeToRoot(rightofLeft);
         * //int leftOfRightSum = CountFromNodeToRoot(leftofRight);
         * //int rightOfRightSum = CountFromNodeToRoot(rightofRight);
         *
         * ////print all the sums
         * //print("leftofleftsum= " + leftOfLeftSum);
         */
    }//end start