public int addLnode(double num, int Pid)
 {
     // This for numrical value .
     MathNode tmp = new MathNode(num);
     tmp.ParentId = Pid;
     int NodeId = Nodes.Add(tmp); // tmp is no longer needed
     tmp = (MathNode)Nodes[Pid]; // set the left child of node's parent.
     tmp.Lchild = NodeId; // tmp is copied by refernce so any will be applied .
     return NodeId;
 }
 public int addLnode(char varname, int Pid)
 {
     // this for var name .
     MathNode tmp = new MathNode(varname);
     tmp.ParentId = Pid;
     int NodeId = Nodes.Add(tmp); // tmp is no longer needed
     tmp = (MathNode)Nodes[Pid]; // set the left child of node's parent.
     tmp.Lchild = NodeId;
     return NodeId;
 }
示例#3
0
 public MathNode Clone()
 {
     MathNode tmp = new MathNode();
     tmp.Lchild = this.Lchild;
     tmp.Rchild = this.Rchild;
     tmp.ParentId = this.ParentId;
     tmp.Op = this.Op;
     tmp.Num = this.Num;
     tmp.Varname = this.Varname;
     return tmp;
 }
 public int addRnode(char varname, int Pid)
 {
     // this for var name .
     MathNode tmp = new MathNode(varname);
     tmp.ParentId = Pid;
     int NodeId = Nodes.Add(tmp); // tmp is no longer needed
     tmp = (MathNode)Nodes[Pid]; // set the left child of node's parent.
     tmp.Rchild = NodeId; // tmp is copied by refernce so any will be applied .
     return NodeId;
 }
 public int addLnode(Operator op, int Pid)
 {
     // this for Operator.
     MathNode tmp = new MathNode(op);
     tmp.ParentId = Pid;
     int NodeId = Nodes.Add(tmp); // tmp is no longer needed
     tmp = (MathNode)Nodes[Pid]; // set the left child of node's parent.
     tmp.Lchild = NodeId; // tmp is copied by refernce so any will be applied .
     return NodeId;
 }
 private void Build(string[] exp, string varname = "x")
 {
     Stack<int> Id_stack = new Stack<int>();
     for (int i = 0; i < exp.Length; i++)
     {
         if (exp[i] != null)
         {
             MathNode tmp = new MathNode();
             double res;
             if (double.TryParse(exp[i], out res)) // if it was a double
             {
                 tmp.Create(res);
             }
             else if (varname.Contains(exp[i])) // if it was a var name
             {
                 char t = Convert.ToChar(exp[i]);
                 tmp.Create(t);
             }
             else
             {   //check if it an operator
                 int abs_bracket_num = 0;// for check for ||
                 switch (exp[i])
                 {
                     case "+":
                         tmp.Create(Operator.Sum);
                         break;
                     case "-":
                         tmp.Create(Operator.Sub);
                         break;
                     case "*":
                         tmp.Create(Operator.Mult);
                         break;
                     case "/":
                         tmp.Create(Operator.Div);
                         break;
                     case "^":
                         tmp.Create(Operator.Pow);
                         break;
                     case "%":
                         tmp.Create(Operator.Mod);
                         break;
                     case "|":
                         if (abs_bracket_num == 0)
                         {
                             abs_bracket_num++;
                             tmp.Create(Operator.Abs);
                         }
                         break;
                     case "sin":
                         tmp.Create(Operator.Sin);
                         break;
                     case "cos":
                         tmp.Create(Operator.Cos);
                         break;
                     case "tan":
                         tmp.Create(Operator.Tan);
                         break;
                     case "cot":
                         tmp.Create(Operator.Cot);
                         break;
                     case "sinh":
                         tmp.Create(Operator.Sinh);
                         break;
                     case "cosh":
                         tmp.Create(Operator.Cosh);
                         break;
                     case "tanh":
                         tmp.Create(Operator.Tanh);
                         break;
                     case "asin":
                         tmp.Create(Operator.ASin);
                         break;
                     case "acos":
                         tmp.Create(Operator.Acos);
                         break;
                     case "atan":
                         tmp.Create(Operator.Atan);
                         break;
                     case "ln":
                         tmp.Create(Operator.Ln);
                         break;
                     case "log":
                         tmp.Create(Operator.Log);
                         break;
                     case "sqrt":
                         tmp.Create(Operator.Sqrt);
                         break;
                     case "e":
                         tmp.Create(Operator.Exp);
                         break;
                 }
             }//end checking of operator
             if (tmp.Op != Operator.None)
             {
                 /*if it was an op put two of stack node as
                  * left and right child
                  * set thire Pid
                  * finally push the new tree to the stack
                 */
                 if (Id_stack.Count > 0)
                 {
                     tmp.Rchild = Id_stack.Pop();
                 }
                 if (Id_stack.Count > 0)
                 {
                     tmp.Lchild = Id_stack.Pop();
                 }
                 int Nid = this.Nodes.Add(tmp);
                 if (tmp.Rchild != -1)
                 {
                     this[tmp.Rchild].ParentId = Nid;
                 }
                 if (tmp.Lchild != -1)
                 {
                     this[tmp.Lchild].ParentId = Nid;
                 }
                 Id_stack.Push(Nid);
                 if (tmp.ParentId == -1)
                 {
                     this.Root = Nid;
                 }
             }
             else
             {
                 //creat a new node and push it to the stack
                 int Nid = this.Nodes.Add(tmp);
                 Id_stack.Push(Nid);
             }
         }
     }
 }
 public MathBinaryTree(double num)
 {
     MathNode tmp = new MathNode(num);
     Nodes.Add(tmp);
 }
 public void Clone(MathBinaryTree Original)
 {
     this.Nodes.Clear();
     foreach (MathNode Node in Original.Nodes)
     {
         if (Node != null)
         {
             MathNode t = new MathNode();
             t = Node.Clone();
             this.Nodes.Add(t);
         }
     }
     this.Root = Original.Root;
 }