protected void LevelOrderI()
        {
            // requires Queue data structure
            Node curr = getRoot();

            if (curr == null)
            {
                return;
            }

            cQueue q = new cQueue();

            q.enqueue(curr);

            while (!q.isEmpty())
            {
                curr = (Node)q.dequeue();
                Console.Write("{0} ", curr.val);

                if (curr.left != null)
                {
                    q.enqueue(curr.left);
                }
                if (curr.right != null)
                {
                    q.enqueue(curr.right);
                }
            }
        }
        protected int getHeightI(Node node)
        {
            if (node == null)
            {
                return(-1);
            }

            cQueue q      = new cQueue();
            int    height = 0;

            // adding nodes in level order
            q.enqueue(node);
            q.enqueue(null); // adding null as marker

            while (!q.isEmpty())
            {
                Node n = (Node)q.dequeue();

                if (n == null)
                {
                    // reached end of the current Level

                    // add null if there are more levels
                    if (!q.isEmpty())
                    {
                        q.enqueue(null);
                    }

                    height++;
                }
                else
                {
                    // if not null than add the
                    // children of extracted node

                    if (n.left != null)
                    {
                        q.enqueue(n.left);
                    }
                    if (n.right != null)
                    {
                        q.enqueue(n.right);
                    }
                }
            } // while(!q.isEmpty())

            return(height);
        }
Exemple #3
0
        protected void LevelOrderInLinesI()
        {
            Console.WriteLine("in level order");
            Node curr = getRoot();

            if (curr == null)
            {
                return;
            }

            int nCount = 0;

            cQueue q = new cQueue();

            q.enqueue(curr);

            while (1 == 1)
            {
                nCount = q.getSize();
                if (nCount == 0)
                {
                    break;
                }

                while (nCount > 0)
                {
                    curr = (Node)q.dequeue();

                    Console.Write("{0} ", curr.key);

                    if (curr.left != null)
                    {
                        q.enqueue(curr.left);
                    }
                    if (curr.right != null)
                    {
                        q.enqueue(curr.right);
                    }

                    nCount--;
                }

                Console.WriteLine();
            }
        }