How can I construct a tree given its inorder and preorder traversal? I am just looking for an efficient algorithm.
Since this is homework, I won't give you a full answer, but hopefully, enough to get you moving.
Imagine you have preorder traversal of, say this tree.
The traversal gives you 2-7-2-6-5-11-5 ... etc. Notice that the 5 is actually the right child of the root.
Obviously, you can't tell that from just looking at the numbers, so either you will be told about the structure of the tree, or you need to store some additional data (ie, whether the a node is the left child or right child, for example).
Parsing the tree is simply a recursive function that takes the preorder traversal as input (think about your scope when you are passing the input). As I mentioned earlier, your preorder traversal should have some additional data attached.
Efficiency:
consider how many times each node is visited when you build this tree, but also consider the operation of reading the input. Is there a way to reorganize the input faster than you can build the tree? What structure would you have to use if you need to manipulate the data.
In Order: You will need the same idea to get you through it, so I won't cover it. I'm sure someone else will, if you are desperate for it.