示例#1
0
        protected void InOrderI()
        {
            /*
             * this require Stack data structure. If you don't
             * want to use stack you can check InOrderI2() which
             * uses Morris tree traversal algorithm
             */

            cStack stack = new cStack();
            Node   curr  = getRoot();

            while (1 == 1)
            {
                if (curr != null)
                {
                    stack.push(curr);
                    curr = curr.left;
                }
                else
                {
                    if (stack.isEmpty())
                    {
                        return;
                    }
                    else
                    {
                        curr = (Node)stack.pop();

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

                        curr = curr.right;
                    }
                } // curr!=null end
            }     // while end
        }
示例#2
0
        protected void PreOrderI()
        {
            // requires stack data structure
            Node curr = getRoot();

            if (curr == null)
            {
                return;
            }

            cStack s = new cStack();

            s.push(curr);

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

                if (curr.left != null)
                {
                    s.push(curr.left);
                }
                if (curr.right != null)
                {
                    s.push(curr.right);
                }
            }
        }
示例#3
0
        protected void PostOrderI()
        {
            // requires stack data structure

            Node curr = getRoot();

            if (curr == null)
            {
                return;
            }

            cStack s = new cStack();

            do
            {
                while (curr != null)
                {
                    if (curr.right != null)
                    {
                        s.push(curr.right);
                    }
                    s.push(curr);

                    curr = curr.left;
                }

                curr = (Node)s.pop();

                if (curr.right != null && (Node)s.peek() == curr.right)
                {
                    s.pop();
                    s.push(curr);
                    curr = curr.right;
                }
                else
                {
                    Console.Write("{0} ", curr.val);
                    curr = null;
                }
            } while(!s.isEmpty());
        }
示例#4
0
        protected bool isBstI()
        {
            Node curr = getRoot();

            if (curr == null)
            {
                return(false);
            }

            // if only root than it is bst
            if (curr.right == null && curr.left == null)
            {
                return(true);
            }

            Node last = null;

            cStack s = new cStack();

            while (!s.isEmpty() && curr != null)
            {
                if (curr != null)
                {
                    s.push(curr);
                    curr = curr.left;
                }
                else
                {
                    curr = (Node)s.pop();

                    if (last != null && curr.val < last.val)
                    {
                        return(false);
                    }

                    last = curr;
                    curr = curr.right;
                }
            } // while end
            return(true);
        }