Image

Imagelone_wolf225 wrote in Imagecpp

The world of arithmetic trees

Made an edit.

My assignment is to construct a class that's an arithmetic tree. I'm probably using the Node class, so that'll be how I point to everything.

I have to take as input from the user, a fully parenthesized, arithmetic expression, and convert that into a binary tree.

Like -- ((3+5)*2)

*
/ \
+ 2
/ \
3 5

(Ok, if the picture doesn't make sense, I'm sorry. Basically, it has the * operator as a root, with the + operator and 2 as children. The + operator has 3 and 5 as children, so the expression would evaluate as what I put before the picture.)

Basically, a method that creates something like that, for any expression the user enters. It's then supposed to evaluate it and etc.

"The input should be a string that contains only plus '+', minus '-', times '*', divide '/', left parentheses '(', right parentheses ')', and numbers that consist of one or more digits. You do not need to handle the case where the string contains variables, letters, or any other characters. Display the tree by visiting and printing the nodes of the tree in each of these orders: (i) pre-order, (ii) post-order, and (iii) level-order traversal. Each traversal should yield an output that contains only plus '+', minus '-', times '*', divide '/', numbers that consist of one or more digits, and blanks used as separators. Finally, compute the value of the arithmetic expression using a post-order traversal."

I can hopefully handle the whole evaluating part of it hopefully without too much difficulty. But the main problem I have is inserting the correct values and operators into the correct places. I know I should take it as a string, and then start somewhere with a root, then in some way build off from there. Can anyone help me as far as taking this string, and inserting the values and operators (based on things like parentheses of course) in the places they need to go (and also may need help on outputting the function)?

Thanks in advance.

EDIT: I think I have something to start with, not sure how much it would work though.

Oh, and as a note. Because I'm either going to get operators or characters (which will be changed to integers), I made Nodes have four parts. A character for operators, a integer (after changing it from a character), then a left and right pointer.

 arithTree(string s){
		int count= 0;
		int i;
		for (i = 0; i < s.length(); i++){
			if (s[i] == '(')
				count++;
		}
		if (count == 0){
			i = 0;
			Node funn = new Node((s[i] - '0'), NULL, NULL, NULL);
			return;
                }
		if (count == 1){
			i = 0;
			Node fun1 = new Node((s[i+1] - '0'), NULL, NULL, NULL);
			Node fun2 = new Node((s[i+3] - '0'), NULL, NULL, NULL);
			Node fun3 = new Node(NULL, s[i+2], fun1, fun2);
			return;
		}
		for (i = 0; i < s.length(); i++){
			if (count == 1)
				break;
			if (s[i] == ')')
				count--;
		}
		i++;
		string left, right;
		for (int j=1; j < i; j++)
			left = left + s[j];
		for (int j=(i+1); j < s.length()-1; j++)
			right = right + s[j];
		Node end = new Node(NULL, s[i], arithTree(left), arithTree(right));
		root = end;
	} 


Basically, have a counter for all the '(' parentheses to see how many there are. Then subtract one for each ')' I find in the second loop. When count hits 1, it means that a dividing place is found. Then I just make two new strings, where I end up leaving off the two outside parentheses, breaking down the function. When it gets to finding only one '(' in the whole string, it means I'm at a simple arithmetic expression like (3+8). I just then make the + operator a node that points down to 3 on the left, and 8 on the right. Recursively call this of course.

I'm not even sure if this is right, but it's all I have to go on right now. I've just been freaking out about this thing, it's so confusing to think about sometimes.

EDIT 2:::

Node end = new Node(NULL, s[i], arithTree(left), arithTree(right));

That line is a problem for me. I'm trying to do things recursively, but it won't take those as Nodes, obviously. Can't think of a way around it yet.