예제 #1
0
        // Paints the current node, then recursively paints children, until all child nodes are null.
        private void PaintNode(BinaryTreeNode node, int treeCenterX, Graphics graphics, int parentX, int parentY)
        {
            if (node != null)
            {
                int adjustedX = (int)(node.GetX() * BinaryTreeNode.SIZE * Math.Pow(2, treeDepth) / 2);
                int adjustedY = (int)(node.GetY() * BinaryTreeNode.SIZE * 2);
                if (parentX != -1 && parentY != -1)
                {
                    graphics.DrawLine(drawingPen,
                                      treeCenterX + parentX,
                                      parentY,
                                      treeCenterX + adjustedX + BinaryTreeNode.SIZE / 2,
                                      adjustedY);
                }
                graphics.DrawEllipse(drawingPen, treeCenterX + adjustedX,
                                     adjustedY,
                                     BinaryTreeNode.SIZE,
                                     BinaryTreeNode.SIZE);
                graphics.DrawString("" + node.GetValue(),
                                    drawingFont,
                                    drawingPen.Brush,
                                    treeCenterX + adjustedX + BinaryTreeNode.SIZE / 4,
                                    (int)(node.GetY() * BinaryTreeNode.SIZE * 2) + BinaryTreeNode.SIZE / 4);

                foreach (BinaryTreeNode n in node.GetChildren())
                {
                    PaintNode(n, treeCenterX, graphics, adjustedX + BinaryTreeNode.SIZE / 2, adjustedY + BinaryTreeNode.SIZE);
                }
            }
        }
예제 #2
0
        // The recursive method for adding a node to the tree.
        private void AddNode(BinaryTreeNode thisNode, BinaryTreeNode newNode, int tempDepth)
        {
            // Increment the current depth of the tree traversal
            tempDepth++;
            // If the new value is less than or equal to the current value, add to the left, otherwise add to the right.
            BinaryTreeNode.ChildNode nextNode =
                (newNode.CompareTo(thisNode) > 0) ?
                BinaryTreeNode.ChildNode.RIGHT :
                BinaryTreeNode.ChildNode.LEFT;

            // If the chosen child node is null, set it to the new value, otherwise continue the recursion.
            if (thisNode.GetChildNode(nextNode) == null)
            {
                newNode.SetPosition(thisNode.GetX() + ((int)nextNode * 2 - 1) * (Math.Pow(2, -tempDepth)), thisNode.GetY() + 1);
                thisNode.SetChildNode(nextNode, newNode);

                // If we are currently deeper than the known depth of the tree, increase the tree depth and calculate the tree width.
                if (tempDepth + 1 > treeDepth)
                {
                    treeDepth = tempDepth + 1;
                    treeWidth = (int)Math.Pow(2, treeDepth);
                }
            }
            else
            {
                AddNode(thisNode.GetChildNode(nextNode), newNode, tempDepth);
            }
        }