Ejemplo n.º 1
0
        /// <summary>
        /// Predict which container put under the bottom level of branches will not get a ball.
        /// Prediction must be ran before <see cref="RunBalls"/> or after <see cref="Reset"/>
        /// The predication is based on bellow rules:
        /// 1. It is a full binary tree;
        /// 2. The number of balls is one ball less than the total bottom level branches;
        /// 3. After a ball passed through a node, the gate of the node will switch.
        /// </summary>
        /// <returns>
        /// The index of the branch/container which will not get a ball
        /// We index the branch/container from left to right with number sequence start from 1 for easy understanding.
        /// So for a tree with depth as 4, the indices would be:
        /// 1, 2, 3, ..., 16
        /// </returns>
        public int PredictEmptyContainer()
        {
            //The index of the node we will check next step.
            int nodeIndex = 0;
            //The node we will check next step
            IGatedNode node = tree.Nodes[nodeIndex];

            for (int level = 0; level < tree.Depth - 1; level++)
            {
                //If the gate is open to left, then the left child tree will get enough balls.
                //So we only need to consider the right child tree.
                //Vice versa.
                nodeIndex = node.GatePosition == GatePosition.Left ?
                            2 * nodeIndex + 2 :
                            2 * nodeIndex + 1;

                node = tree.Nodes[nodeIndex];
            }

            //Now the keyNode is the node at bottom level which not get enough ball, it will only get one ball.
            //So now we can know which branch of this node no ball will pass through it.
            //If its gate is open to left, then the right branch will not get a ball.

            //Translate the index to a lidex at the level instead of the whole tree.
            nodeIndex = TranslateToLevelIndex(nodeIndex, tree.Depth);

            //Then let's check which branch/containter will not get a ball
            //Node index is start from 0, while returned human readable start from 1.
            return(node.GatePosition == GatePosition.Left ?
                   nodeIndex * 2 + 2 :
                   nodeIndex * 2 + 1);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Check and return which branch/container did not get a ball。
        /// </summary>
        /// <returns></returns>
        public int CheckEmptyContainer()
        {
            int        nodeIndex = 0;
            IGatedNode node      = tree.Nodes[nodeIndex];

            while (nodeIndex < tree.NumberOfNodes)
            {
                int nextNodeIndex = node.BallsPassedToLeft < node.BallsPassedToRight ?
                                    2 * nodeIndex + 1 :
                                    2 * nodeIndex + 2;

                if (nextNodeIndex >= tree.NumberOfNodes)
                {
                    break;
                }

                nodeIndex = nextNodeIndex;
                node      = tree.Nodes[nodeIndex];
            }

            //Translate the index to a lidex at the level instead of the whole tree.
            nodeIndex = TranslateToLevelIndex(nodeIndex, tree.Depth);

            //Node index is start from 0, while returned human readable start from 1.
            return(node.BallsPassedToLeft == 0 ?
                   nodeIndex * 2 + 1 :
                   nodeIndex * 2 + 2);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Run one ball through this gated tree.
        /// </summary>
        public void RunOneBall()
        {
            //Run from the root node.
            int nodeIndex = 0;

            while (nodeIndex < this.numberOfNodes)
            {
                IGatedNode node = this.nodes[nodeIndex];

                int nextNodeIndex = node.GatePosition == GatePosition.Left ?
                                    2 * nodeIndex + 1 :
                                    2 * nodeIndex + 2;

                node.RunOneBall();

                nodeIndex = nextNodeIndex;
            }
        }
Ejemplo n.º 4
0
 public void TestCleanup()
 {
     node = null;
 }
Ejemplo n.º 5
0
 public void TestInitialize()
 {
     node = new GatedNode(GatePosition.Left);
 }