Ejemplo n.º 1
0
        // Q4 BFS
        public static List<LinkedList> Tree2LinkedList(BinarySearchTree.Node Root)
        {
            List<LinkedList> Result;
            if (Root == null)
                return null;
            else
            {
                Result = new List<LinkedList>();
                Queue<BinarySearchTree.Node> Queue = new Queue<BinarySearchTree.Node>();
                Queue<int> QLevels = new Queue<int>();
                Queue.Enqueue(Root);
                QLevels.Enqueue(0);
                Result.Add(new LinkedList());
                Result[0].Add(Root);
                while (Queue.Count != 0)
                {
                    BinarySearchTree.Node TempNode = Queue.Dequeue();
                    LinkedList TempLinkedList;
                    int Level = QLevels.Dequeue();
                    if (TempNode != null)
                    {
                        // Check if(TempNode) is CurrentLevel
                        if (TempNode.Left != null)
                        {
                            Queue.Enqueue(TempNode.Left);
                            QLevels.Enqueue(Level + 1);
                        }
                        if (TempNode.Right != null)
                        {
                            Queue.Enqueue(TempNode.Right);
                            QLevels.Enqueue(Level + 1);
                        }
                        if (Level + 1 != Result.Count)
                        {
                            if (TempNode.Left != null) Result.Last().Add(TempNode.Left);
                            if (TempNode.Right != null) Result.Last().Add(TempNode.Right);

                        }
                        else
                        {
                            TempLinkedList = new LinkedList();
                            if (TempNode.Left != null) TempLinkedList.Add(TempNode.Left);
                            if (TempNode.Right != null) TempLinkedList.Add(TempNode.Right);
                            if (TempNode.Right != null || TempNode.Left != null)
                            Result.Add(TempLinkedList);
                        }
                    }
                }

                return Result;
            }
        }
Ejemplo n.º 2
0
        // Input: Root of the Tree
        // Output: Printed Tree Level-by-level on Console
        // Compute: Traverse and print the tree level-by-level
        static void printTree(Node root)
        {
            // If the root node is null then return nothing
            if (root == null)
            {
                return; // Do Nothing
            }

            int currentLevel = 0;
            Queue<Node> q = new Queue<Node>(); // Error
            Queue<int> qLevels = new Queue<int>();
            q.Enqueue(root);
            qLevels.Enqueue(0);

            // Loop while there is an element in the queue
            while (q.Count > 0)
            {
                Node temp = q.Dequeue();
                int temLevel = qLevels.Dequeue();
                // Printing node
                if (temLevel != currentLevel)
                {
                    System.Console.WriteLine("");
                    System.Console.Write(temp.Value);
                    currentLevel = temLevel;
                }
                else
                {
                    if (temp == root)
                    { // Special Case: If just the root don't print ", "
                        System.Console.Write(temp.Value);
                    }
                    else
                    {
                        System.Console.Write(", " + temp.Value);
                    }
                }

                if (temp.Left != null)
                {
                    q.Enqueue(temp.Left);
                    qLevels.Enqueue(currentLevel+1);
                }

                if (temp.Right != null)
                {
                    q.Enqueue(temp.Right);
                    qLevels.Enqueue(currentLevel+1);
                }

            }
        }