Guest User

Untitled

a guest
Jul 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace infixExpressionToBinaryExpressionTree
  9. {
  10. class Program
  11. {
  12. internal class Node{
  13. public char Operand {get; set;} // operator sometimes;
  14. public Node Left;
  15. public Node Right;
  16.  
  17. public Node(char val)
  18. {
  19. Operand = val;
  20. }
  21. }
  22.  
  23. static void Main(string[] args)
  24. {
  25. //RunTestcase1();
  26. RunTestcase2();
  27. }
  28.  
  29. public static void RunTestcase1()
  30. {
  31. var node = InfixExpressionToBinaryExpressionTree("(1+2)");
  32. Debug.Assert(node.Operand == '+');
  33. }
  34.  
  35. public static void RunTestcase2()
  36. {
  37. var node = InfixExpressionToBinaryExpressionTree("((1+2)*(3-4))");
  38. Debug.Assert(node.Operand.CompareTo("*") == 0);
  39. }
  40.  
  41. public static string Operators = "+-*/";
  42. public static string Operands = "0123456789"; // make it simple, one digit only first
  43.  
  44. /// <summary>
  45. /// Time complexity: O(N), space complexity: using stack -> at most O(N)
  46. /// </summary>
  47. /// <param name="expression"></param>
  48. /// <returns></returns>
  49. public static Node InfixExpressionToBinaryExpressionTree(string expression)
  50. {
  51. if (expression == null || expression.Length == 0)
  52. return null;
  53.  
  54. var stack = new Stack<Object>();
  55.  
  56. for (int i = 0; i < expression.Length; i++)
  57. {
  58. var current = expression[i];
  59.  
  60. var isCloseBracket = current == ')';
  61.  
  62. if (!isCloseBracket)
  63. {
  64. stack.Push(current);
  65. }
  66. else
  67. {
  68. if (stack.Count < 4)
  69. return null;
  70.  
  71. var operand2 = stack.Pop();
  72. var operatorChar = stack.Pop();
  73. var operand1 = stack.Pop();
  74. var openBracket = (char)stack.Pop();
  75.  
  76. if (openBracket != '(' ||
  77. !checkOperand(operand2) ||
  78. !checkOperand(operand1) ||
  79. !checkOperator(operatorChar)
  80. )
  81. {
  82. return null;
  83. }
  84.  
  85. var root = new Node((char)operatorChar);
  86. root.Left = (operand1.GetType() == typeof(Node)) ? (Node)operand1 : new Node((char)operand1);
  87. root.Right = (operand2.GetType() == typeof(Node)) ? (Node)operand2 : new Node((char)operand2);
  88.  
  89. stack.Push(root);
  90. }
  91. }
  92.  
  93. if (stack.Count > 1 || stack.Count == 0)
  94. return null;
  95.  
  96. return (Node)stack.Pop();
  97. }
  98.  
  99. /// <summary>
  100. /// code review July 6, 2018
  101. /// </summary>
  102. /// <param name="operand"></param>
  103. /// <returns></returns>
  104. private static bool checkOperand(Object operand)
  105. {
  106. if (operand.GetType() == typeof(Node))
  107. return true;
  108.  
  109. var number = (char)operand;
  110. return Operands.IndexOf(number) != -1;
  111. }
  112.  
  113. private static bool checkOperator(Object operatorChar)
  114. {
  115. var arithmetic = (char)operatorChar;
  116.  
  117. return Operators.IndexOf(arithmetic) != -1;
  118. }
  119. }
  120. }
Add Comment
Please, Sign In to add comment