Example #1
0
        public static int ComputeExprTree(BinNode <Char> expr)
        {
            Char ch = expr.GetValue();

            if (isDigit(ch))
            {
                return((int)(ch - '0'));
            }
            int left  = ComputeExprTree(expr.GetLeft());
            int right = ComputeExprTree(expr.GetRight());

            switch (ch)
            {
            case '+':
                return(left + right);

            case '-':
                return(left - right);

            case '*':
                return(left * right);

            case '/':
                return(left / right);
            }
            return(0);
        }
Example #2
0
 public static void PrintPostOrder <T>(BinNode <T> bt)
 {
     if (bt != null)
     {
         PrintPostOrder(bt.GetLeft());
         PrintPostOrder(bt.GetRight());
         Console.Write(bt.GetValue() + " ");
     }
 }
Example #3
0
        public static void TraverseByLevel <T>(BinNode <T> bt)
        {
            Queue <BinNode <T> > q = new Queue <BinNode <T> >();

            q.Insert(bt);
            while (!q.IsEmpty())
            {
                bt = q.Remove();
                // Action on bt
                Console.Write(bt.GetValue() + " ");
                if (bt.HasLeft())
                {
                    q.Insert(bt.GetLeft());
                }
                if (bt.HasRight())
                {
                    q.Insert(bt.GetRight());
                }
            }
        }