コード例 #1
0
        public String WriteTreeToString(int iSpace)
        {
            int iDeep = InitWrite(iSpace, 0);
            int iRow  = 1;
            Queue <CTreeNodeWriteable> clQueue = new Queue <CTreeNodeWriteable>();
            StringBuilder clSB  = new StringBuilder();
            EWriteCommand eLast = EWriteCommand.EWRITE_DATA;

            m_eCommand = EWriteCommand.EWRITE_DATA;
            clQueue.Enqueue(this);

            using (StringWriter clSW = new StringWriter(clSB))
            {
                while (clQueue.Any())
                {
                    CTreeNodeWriteable pNode = clQueue.Dequeue();
                    EWriteCommand      eCom  = pNode.m_eCommand;

                    if (eCom != eLast)
                    {
                        clSW.WriteLine();
                    }

                    switch (eCom)
                    {
                    case EWriteCommand.EWRITE_DATA:
                        if (pNode.m_eCommand != eLast)
                        {
                            ++iRow;
                        }

                        pNode.WriteData(clSW, iSpace);

                        if (iRow < iDeep)
                        {
                            if (!pNode.HasChild())
                            {
                                pNode.m_sFork = -1;
                            }

                            pNode.m_eCommand = EWriteCommand.EWRITE_FORK;
                            clQueue.Enqueue(pNode);
                        }

                        break;

                    case EWriteCommand.EWRITE_FORK:
                        pNode.WriteFork(clSW, iSpace);

                        if (pNode.m_sFork < 0)
                        {
                            pNode.m_eCommand = EWriteCommand.EWRITE_BRANCH;
                            clQueue.Enqueue(pNode);
                        }
                        else
                        {
                            CTreeNodeWriteable pChild = pNode.FirstChild();
                            while (pChild != null)
                            {
                                pChild.m_eCommand = EWriteCommand.EWRITE_BRANCH;
                                clQueue.Enqueue(pChild);
                                pChild = pChild.RightBrother();
                            }
                        }

                        break;

                    case EWriteCommand.EWRITE_BRANCH:
                        pNode.WriteBranch(clSW, iSpace);
                        pNode.m_eCommand = EWriteCommand.EWRITE_DATA;
                        clQueue.Enqueue(pNode);

                        break;
                    }

                    eLast = eCom;
                }
            }

            return(clSB.ToString());
        }