public BinNode <int> BuildTreePre(string[] preOrder, string[] inOrder) { int i = 0; BinNode <int> newTree = new BinNode <int>(int.Parse(preOrder[0])); while (preOrder[0] != inOrder[i]) { i++; } if (i > 0) { string[] preOrderL = new string[i]; string[] inOrderL = new string[i]; for (int j = 0; j < i; j++) { preOrderL[j] = preOrder[j + 1]; inOrderL[j] = inOrder[j]; } newTree.SetLeft(BuildTreePre(preOrderL, inOrderL)); } if (i < inOrder.Length - 1) { string[] preOrderR = new string[preOrder.Length - i - 1]; string[] inOrderR = new string[preOrder.Length - i - 1]; for (int j = i + 1; j < inOrder.Length; j++) { preOrderR[j - i - 1] = preOrder[j]; inOrderR[j - i - 1] = inOrder[j]; } newTree.SetRight(BuildTreePre(preOrderR, inOrderR)); } return(newTree); }
public void ShowPost(BinNode <t> node) { if (node != null) { ShowPost(node.GetLeft()); ShowPost(node.GetRight()); MessageBox.Show(node.GetValue() + " "); } }
public bool IsAllPos(BinNode <int> node) { if (node == null) { return(true); } if (node.GetValue() <= 0) { return(false); } return(IsAllPos(node.GetLeft()) && IsAllPos(node.GetRight())); }
public int CountHalves(BinNode <t> node) { if (node == null) { return(0); } if (node.GetLeft() == null & node.GetRight() != null || node.GetLeft() != null & node.GetRight() == null) { return(1 + CountHalves(node.GetLeft()) + CountHalves(node.GetRight())); } return(CountHalves(node.GetLeft()) + CountHalves(node.GetRight())); }
public double Result(BinNode <string> node, double x, double y) { if (node == null) { return(0); } if (node.GetValue() == "+") { return(Result(node.GetLeft(), x, y) + Result(node.GetRight(), x, y)); } if (node.GetValue() == "-") { return(Result(node.GetLeft(), x, y) - Result(node.GetRight(), x, y)); } if (node.GetValue() == "*") { return(Result(node.GetLeft(), x, y) * Result(node.GetRight(), x, y)); } if (node.GetValue() == "/") { return(Result(node.GetLeft(), x, y) / Result(node.GetRight(), x, y)); } if (node.GetValue() == "^") { return(Math.Pow(Result(node.GetLeft(), x, y), Result(node.GetRight(), x, y))); } if (node.GetValue() == "sin") { return(Math.Sin(Result(node.GetLeft(), x, y))); } if (node.GetValue() == "cos") { return(Math.Cos(Result(node.GetLeft(), x, y))); } if (node.GetValue() == "tan") { return(Math.Tan(Result(node.GetLeft(), x, y))); } if (node.GetValue() == "sqrt") { return(Math.Sqrt(Result(node.GetLeft(), x, y))); } if (node.GetValue() == "x") { return(x); } if (node.GetValue() == "y") { return(y); } return(double.Parse(node.GetValue())); }
public bool IsRightLeft(BinNode <t> node) { if (node == null) { return(true); } if (node.GetLeft() == null & node.GetRight() == null) { return(true); } if (node.GetLeft() == null && node.GetRight() != null) { return(false); } return(IsRightLeft(node.GetLeft()) && IsRightLeft(node.GetRight())); }
public void SetRight(BinNode <t> newNode) { right = newNode; }
public void SetLeft(BinNode <t> newNode) { left = newNode; }
public BinNode(t value, BinNode <t> left, BinNode <t> right) { this.value = value; this.left = left; this.right = right; }
private void CalcButton_Click(object sender, EventArgs e) { string[] function = stringToArray(textBox1.Text); minHeight = 0; maxHeight = 0; #region 2D if (!do3D.Checked) { if (function != null) { graphPoints = new Point[(int)res * 2 + 1]; binTree = ops.ToTree(function); int i = 0; for (double x = -res; x <= res; x++) { double mathX = x * (canvas.Width / zoom) / res; double mathY = ops.Result(binTree, mathX, 1); if (!double.IsInfinity(mathY) && mathY < 5000 && mathY > -5000) { int drawX = (int)(mathX * zoom); int drawY = (int)(mathY * zoom); drawPoint.Y = canvas.Height - drawY; drawPoint.X = drawX; drawPoint.X += canvas.Width / 2; drawPoint.Y -= canvas.Height / 2; graphPoints[i] = drawPoint; } else { drawPoint.Y = 5001; graphPoints[i] = drawPoint; } i++; } canvas.Refresh(); } else { MessageBox.Show("Couldn't analyze input"); } } #endregion #region 3D else { if (function != null) { xx = new double[(int)res3D * 2 + 1, (int)res3D * 2 + 1]; yy = new double[(int)res3D * 2 + 1, (int)res3D * 2 + 1]; zz = new double[(int)res3D * 2 + 1, (int)res3D * 2 + 1]; binTree = ops.ToTree(function); //for (double y = -range3D / res3D; y <= range3D / res3D; y++) for (double y = -res3D; y <= res3D; y++) { for (double x = -res3D; x <= res3D; x++) { double mathX = x / res3D * range3D; double mathY = y / res3D * range3D; double mathZ = ops.Result(binTree, mathX, mathY); if (mathZ < minHeight) { minHeight = mathZ; } else if (mathZ > maxHeight) { maxHeight = mathZ; } if (!double.IsInfinity(mathZ) && mathZ < 5000 && mathZ > -5000) { xx[(int)(x + res3D), (int)(y + res3D)] = mathX; yy[(int)(x + res3D), (int)(y + res3D)] = mathY; zz[(int)(x + res3D), (int)(y + res3D)] = mathZ; } } } canvas.Refresh(); } else { MessageBox.Show("Couldn't analyze input"); } } #endregion }
public BinNode <string> ToTree(string[] input) { // closing if input is empty if (input.Length == 0) { return(null); } // turning all letters to lower case for (int j = 0; j < input.Length - 1; j++) { input[j] = input[j].ToLower(); } BinNode <string> newTree = new BinNode <string>(); int parenthesesDepth = 0; int i; #region searching operators in order for (int round = 0; round < 3; round++) { for (i = input.Length - 1; i >= 0; i--) { // updating how many parentheses are open if (input[i] == ")") { parenthesesDepth += 1; } if (input[i].Contains("(")) { parenthesesDepth -= 1; } #region parentheses stuff if (i == 0) { if (parenthesesDepth == 0 && input[input.Length - 1] == ")") { string[] newInput = new string[input.Length - 2]; if (input[0] == "(") { for (int j = 0; j < input.Length - 2; j++) { newInput[j] = input[j + 1]; } input = newInput; round = -1; break; } else if (input[0] == "sin(") { for (int j = 0; j < input.Length - 2; j++) { newInput[j] = input[j + 1]; } newTree.SetLeft(ToTree(newInput)); newTree.SetValue("sin"); return(newTree); } else if (input[0] == "cos(") { for (int j = 0; j < input.Length - 2; j++) { newInput[j] = input[j + 1]; } newTree.SetLeft(ToTree(newInput)); newTree.SetValue("cos"); return(newTree); } else if (input[0] == "tan(") { for (int j = 0; j < input.Length - 2; j++) { newInput[j] = input[j + 1]; } newTree.SetLeft(ToTree(newInput)); newTree.SetValue("tan"); return(newTree); } else if (input[0] == "sqrt(") { for (int j = 0; j < input.Length - 2; j++) { newInput[j] = input[j + 1]; } newTree.SetLeft(ToTree(newInput)); newTree.SetValue("sqrt"); return(newTree); } } } #endregion #region add / sub if ((round == 0 && (input[i] == "+" || input[i] == "-")) && parenthesesDepth == 0) { newTree.SetValue(input[i]); string[] inputL = new string[i]; for (int j = 0; j < i; j++) { inputL[j] = input[j]; } newTree.SetLeft(ToTree(inputL)); string[] inputR = new string[input.Length - i - 1]; for (int j = i + 1; j < input.Length; j++) { inputR[j - i - 1] = input[j]; } newTree.SetRight(ToTree(inputR)); return(newTree); } #endregion #region mult / div if ((round == 1 && (input[i] == "*" || input[i] == "/")) && parenthesesDepth == 0) { newTree.SetValue(input[i]); string[] inputL = new string[i]; for (int j = 0; j < i; j++) { inputL[j] = input[j]; } newTree.SetLeft(ToTree(inputL)); string[] inputR = new string[input.Length - i - 1]; for (int j = i + 1; j < input.Length; j++) { inputR[j - i - 1] = input[j]; } newTree.SetRight(ToTree(inputR)); return(newTree); } #endregion #region power if (round == 2 && input[i] == "^" && parenthesesDepth == 0) { newTree.SetValue("^"); string[] inputL = new string[i]; for (int j = 0; j < i; j++) { inputL[j] = input[j]; } newTree.SetLeft(ToTree(inputL)); string[] inputR = new string[input.Length - i - 1]; for (int j = i + 1; j < input.Length; j++) { inputR[j - i - 1] = input[j]; } newTree.SetRight(ToTree(inputR)); return(newTree); } #endregion } } #endregion // if no operators were found just return the value newTree.SetValue(input[0]); return(newTree); }