コード例 #1
0
        // returns the Nth node of the tree
        // received parameters are the root node of the tree and a number n
        // the method works by counting a static variable and when the counter reaches the number n,
        // the method will return the current node
        // n value is ranged from 1....nodeCount
        // the traversal method is pre-order traversal
        public Node getNodeN(Node root, int n)
        {
            counter++;
            Node returnRoot = null;

            // if the counter reached n, return the current root
            if (counter == n)
            {
                return(root);
            }

            for (int i = 0; i < root.getNumChildren(); i++)
            {
                if (root.getChildAtIndex(i) != null)
                {
                    returnRoot = root.getChildAtIndex(i).getNodeN(root.getChildAtIndex(i), n);
                    if (returnRoot != null)
                    {
                        return(returnRoot);
                    }
                }
            }
            return(null);
        }
コード例 #2
0
        private void printTreeConsole(Node currentNode)
        {
            int numOfChildren = currentNode.getNumChildren();

            for (int i = 0; i < indentation; i++)
            {
                Console.Write("\t");
            }
            Console.WriteLine(currentNode.toString());

            indentation++;
            for (int i = 0; i < numOfChildren; i++)
            {
                printTreeConsole(currentNode.getChildAtIndex(i));
            }
            indentation--;
        }
コード例 #3
0
        private Point DrawTreeGUI(Point frameBegin, Point frameEnd, Node currentNode)
        {
            Point  nodeLocation    = new Point();
            int    frameWidth      = frameEnd.X - frameBegin.X;
            string nodeDescription = currentNode.toString();
            int    numOfChildren   = currentNode.getNumChildren();

            nodeLocation.X = frameBegin.X + frameWidth / 2;
            nodeLocation.Y = frameBegin.Y;

            // Draw currentNode
            DrawEllipse(nodeLocation, nodeRadius, nodeColor);
            DrawString(nodeDescription, nodeLocation, TEXT_SIZE);

            if (numOfChildren > 0)
            {
                int newFrameWidth = frameWidth / numOfChildren;

                // Draw node's Children
                for (int i = 0; i < numOfChildren; i++)
                {
                    Node  newNode       = currentNode.getChildAtIndex(i);
                    Point newFrameBegin = new Point();
                    Point newFrameEnd   = new Point();

                    newFrameBegin.X = frameBegin.X + newFrameWidth * i;
                    newFrameEnd.X   = newFrameBegin.X + newFrameWidth;

                    newFrameBegin.Y = frameBegin.Y + TREE_LEVEL_MARGIN;
                    newFrameEnd.Y   = frameBegin.Y + TREE_LEVEL_MARGIN;

                    Point childLocation = DrawTreeGUI(newFrameBegin, newFrameEnd, newNode); // Draw child and get it location
                    DrawLine(nodeLocation, childLocation);                                  // Draw line to child
                }
            }
            return(nodeLocation);
        }
コード例 #4
0
        public long evalIndexGrade(Node root, int index)
        {
            /*
             * evaluation function
             * traverse the entire tree and returns the result of the evaluation
             */
            long grade = 0;

            //		root.color = Color.red;
            // if the current root is a terminal, return it's evaluation
            if (root is Terminal)
            {
                grade = ((Terminal)root).Eval(index);
                return(grade);
            }

            // if the root is 'If <=' function, check if arg0 is <= than arg1, if so, return arg2, else, return arg3
            if (((Function)root).getIdentity().Equals("If <=", StringComparison.OrdinalIgnoreCase))
            {
                if (evalIndexGrade(root.getChildAtIndex(0), index) <= evalIndexGrade(root.getChildAtIndex(1), index))
                {
                    grade = evalIndexGrade(root.getChildAtIndex(2), index);
                }
                else
                {
                    grade = evalIndexGrade(root.getChildAtIndex(3), index);
                }
            }
            if (((Function)root).getIdentity().Equals("If >=", StringComparison.OrdinalIgnoreCase))
            {
                if (evalIndexGrade(root.getChildAtIndex(0), index) >= evalIndexGrade(root.getChildAtIndex(1), index))
                {
                    grade = evalIndexGrade(root.getChildAtIndex(2), index);
                }
                else
                {
                    grade = evalIndexGrade(root.getChildAtIndex(3), index);
                }
            }
            if (((Function)root).getIdentity().Equals("Minus", StringComparison.OrdinalIgnoreCase))
            {
                grade = evalIndexGrade(root.getChildAtIndex(0), index) -
                        evalIndexGrade(root.getChildAtIndex(1), index);
            }
            if (((Function)root).getIdentity().Equals("Plus", StringComparison.OrdinalIgnoreCase))
            {
                grade = evalIndexGrade(root.getChildAtIndex(0), index) +
                        evalIndexGrade(root.getChildAtIndex(1), index);
            }
            if (((Function)root).getIdentity().Equals("Multi", StringComparison.OrdinalIgnoreCase))
            {
                grade = evalIndexGrade(root.getChildAtIndex(0), index) *
                        evalIndexGrade(root.getChildAtIndex(1), index);
            }

            return(grade);
        }