public static OneNode NewNode(ref string st, ref int pos) { OneNode res = null; while (pos < st.Length && st[pos] == ' ') { ++pos; } //MessageBox.Show("In ONENODE, '" + st[pos] + "'"); if (pos < st.Length) { if (Char.IsLower(st[pos])) { res = new VarNode(""); } else if (Char.IsDigit(st[pos])) { res = new IntNode(0); } else if (st[pos] == '(') { res = new SignNode(Sign.empty); } } if (res != null && !res.LoadNodeFromString(ref st, ref pos)) { res = null; } return(res); }
public static void DelNode(OneNode node) { if (node != null) { if (node.left != null) { DelNode(node.left); } if (node.right != null) { DelNode(node.right); } node = null; } }
void PrintOnScreen(OneNode one, TreeNode tree) { if (one != null) { if (one.left != null) { TreeNode tl = new TreeNode(one.left.GetString); tree.Nodes.Add(tl); PrintOnScreen(one.left, tl); } if (one.right != null) { TreeNode tr = new TreeNode(one.right.GetString); tree.Nodes.Add(tr); PrintOnScreen(one.right, tr); } } }
public bool TryLaodFromString(string st) { bool res = true; Clear(); //Root = null; int pos = 0; Root = OneNode.NewNode(ref st, ref pos); //MessageBox.Show("In load tree "+ Root.ToString()); while (pos < st.Length && st[pos] == ' ') { ++pos; } if (pos != st.Length || Root == null) { //MessageBox.Show("Can't"); Clear(); res = false; } return(res); }
bool TryClear(ref OneNode R, ref int count) { bool res = true; if (R != null) { OneNode l = R.left; OneNode r = R.right; if (Root is SignNode) { if (l == null || r == null) { return(false); } Sign s = ((SignNode)R).Sign; // MessageBox.Show("Here, s = " + s.ToString()); if (l is IntNode && r is IntNode) { int left = ((IntNode)l).Finfo; int right = ((IntNode)r).Finfo; switch (s) { case Sign.mult: R = new IntNode(left * right); ++count; break; case Sign.add: R = new IntNode(left + right); ++count; break; case Sign.divide: if (right == 0) { return(false); } else { R = new IntNode(left / right); } ++count; break; case Sign.subst: R = new IntNode(left - right); ++count; break; default: return(false); } } else { switch (s) { case Sign.add: if (l is IntNode && ((IntNode)l).Finfo == 0) { R = R.right; ++count; TryClear(ref R, ref count); } else if (r is IntNode && ((IntNode)r).Finfo == 0) { R = R.left; ++count; TryClear(ref R, ref count); } else { TryClear(ref R.left, ref count); TryClear(ref R.right, ref count); } break; case Sign.divide: if (l is IntNode && ((IntNode)l).Finfo == 0) { R = new IntNode(0); ++count; } else if (r is IntNode) { if (((IntNode)r).Finfo == 0) { return(false); } else if (((IntNode)r).Finfo == 1) { R = R.left; ++count; TryClear(ref R, ref count); } } else { TryClear(ref R.left, ref count); TryClear(ref R.right, ref count); } break; case Sign.mult: if (l is IntNode) { if (((IntNode)l).Finfo == 0) { ++count; R = new IntNode(0); } else if (((IntNode)l).Finfo == 1) { R = R.right; ++count; TryClear(ref R, ref count); } } else if (r is IntNode) { if (((IntNode)r).Finfo == 0) { R = new IntNode(0); ++count; } else if (((IntNode)r).Finfo == 1) { R = R.left; ++count; TryClear(ref R, ref count); } } else { TryClear(ref R.left, ref count); TryClear(ref R.right, ref count); } break; case Sign.subst: if (r is IntNode && ((IntNode)r).Finfo == 0) { R = R.left; ++count; TryClear(ref R, ref count); } else { TryClear(ref R.left, ref count); TryClear(ref R.right, ref count); } break; default: return(false); } } } } return(res); }
public void Clear() { Root = new VarNode("X"); }
public BinaryTree() { Root = new VarNode("X"); }
public override bool LoadNodeFromString(ref string st, ref int pos) { bool res = true; if (pos >= st.Length) { res = false; } else if (st[pos] != '(') { res = false; } else { ++pos; while (pos < st.Length && st[pos] == ' ') { ++pos; } left = OneNode.NewNode(ref st, ref pos); if (left == null) { res = false; } else { while (pos < st.Length && st[pos] == ' ') { ++pos; } int p = Array.IndexOf(Sings, st[pos]); if (pos == st.Length || p == -1 || p == 4) { res = false; } else { sign = (Sign)p; // MessageBox.Show("Sign in reading = " + st[pos] + " p = " + p+" si = "+sign); ++pos; right = OneNode.NewNode(ref st, ref pos); if (right == null) { res = false; } else { while (pos < st.Length && st[pos] == ' ') { ++pos; } if (pos == st.Length || st[pos] != ')') { res = false; } else { ++pos; } // MessageBox.Show("res = " + res); } } } } return(res); }
public OneNode() { left = right = null; }