Пример #1
0
        public void PrintTreeByLevel()
        {
            PriorityQueue priorityQueue = new PriorityQueue(1000, false);
            int           currentLevel  = 1;
            int           previousLevel = currentLevel - 1;

            TreeAndLevel treeAndLevel = new TreeAndLevel(this, currentLevel);

            priorityQueue.Enqueue(treeAndLevel, currentLevel);

            while (priorityQueue.Count > 0)
            {
                treeAndLevel = (TreeAndLevel)priorityQueue.Dequeue();
                currentLevel = treeAndLevel.level;

                if (previousLevel < currentLevel)
                {
                    Console.WriteLine();
                    Console.Write("Level #{0}: {1}", currentLevel, treeAndLevel.tree.value);
                }
                else
                {
                    Console.Write(" {0}", treeAndLevel.tree.value);
                }
                previousLevel = currentLevel;

                foreach (int childValue in treeAndLevel.tree.htChildren.Keys)
                {
                    Tree childTree = (Tree)treeAndLevel.tree.htChildren[childValue];
                    childTree.PrintByLevelInternal(priorityQueue, currentLevel + 1);
                }
            }
        }
Пример #2
0
 private void PrintByLevelInternal(PriorityQueue priorityQueue, int level)
 {
     if (!this.deleted)
     {
         TreeAndLevel treeAndLevel = new TreeAndLevel(this, level);
         priorityQueue.Enqueue(treeAndLevel, level);
     }
     else
     {
         foreach (int childValue in htChildren.Keys)
         {
             Tree childTree = (Tree)htChildren[childValue];
             childTree.PrintByLevelInternal(priorityQueue, level);
         }
     }
 }