At first I had created each class in separate class files, but it was kind of weird how I was parsing the input and how I was building the tree. So I decided to move my Expression Parser class to be nested within my Expression Tree class. This would allow me to do all the parsing and building of the tree within one class file instead of two where it can get messy and confusing.
Here is my new code, thanks for reading:
using System; using System.Collections.Generic; namespace Assignment5 { class ExpressionTree { public TreeNode<string> root; private int node_count; public ExpressionTree(string input) { root = null; ExpressionParser parser = new ExpressionParser(this, input); } private class ExpressionParser { private Stack<TreeNode<string>> numberStack, operatorStack; public ExpressionParser(ExpressionTree tree, string input) { numberStack = new Stack<TreeNode<string>>(); operatorStack = new Stack<TreeNode<string>>(); Console.WriteLine(input.Length); string[] expressionArray = new string[input.Length]; expressionArray = input.Split(); BuildNodes(tree, expressionArray); } private void BuildNodes(ExpressionTree tree, string[] expressionArray) { foreach (string item in expressionArray) { int tempInt; if (Int32.TryParse(item, out tempInt)) { TreeNode<string> number_node = new TreeNode<string> { Data = tempInt.ToString() }; numberStack.Push(number_node); } else
{ TreeNode<string> operator_node = new TreeNode<string> { Data = item }; operatorStack.Push(operator_node); } } BuildTree(tree); } private void BuildTree(ExpressionTree tree) { tree.node_count = numberStack.Count + operatorStack.Count; while (operatorStack.Count != 0) { TreeNode<string> tempRoot = operatorStack.Pop(); tempRoot.Right = numberStack.Pop(); tempRoot.Left = numberStack.Pop(); numberStack.Push(tempRoot); } tree.root = numberStack.Pop(); } } public int NodeCount { get { return node_count; } } // TODO Finish Evaluate public void Evaluate(MyList<string> list, TreeNode<string> node) { if (node.Left != null) Evaluate(list, node.Left); if (node.Right != null) Evaluate(list, node.Right); throw new NotImplementedException(); } public string TraversePre(MyList<string> list, TreeNode<string> node) { list.Add(node.Data); if (node.Left != null) TraversePre(list, node.Left); if (node.Right != null) TraversePre(list, node.Right); return string.Join(", ", list); } public string TraverseIn(MyList<string> list, TreeNode<string> node) { if (node.Left != null) TraverseIn(list, node.Left); list.Add(node.Data); if (node.Right != null) TraverseIn(list, node.Right); return string.Join(", ", list); } public string TraversePost(MyList<string> list, TreeNode<string> node) { if (node.Left != null) TraversePost(list, node.Left); if (node.Right != null) TraversePost(list, node.Right); list.Add(node.Data); return string.Join(", ", list); } } }
No comments:
Post a Comment