What was the purpose of creating the tree of the knowledge of good and evil? This is not a “why-did-God-allow-man-to-sin” question.
This is not as simple a question as it might seem. Various writers and speakers have, I believe, contributed confusion rather than clarity on this question.
The first thing we must realize is that God does not confuse material and spiritual things. This is illustrated by the Lord Jesus’ discussion with His disciples regarding the washings imposed by the Pharisees (see Mk. 7:14–23). God gives the natural world as a parable to instruct us, but it is a mistake to confuse lessons it teaches with actual effectual action. The idea that the material and spiritual worlds are connected so that one actually affects the other is an idea from mystical philosophy. So it is with the tree of knowledge. The tree was a real tree of some sort. Just like the garden was a real garden and Adam and Eve were real people. But the tree did not contain some kind of “vitamin” or “virus” which, when ingested, caused a physical or mental change in Adam and Eve.
If you simply relabeled the tree of knowledge and the tree of life in the garden the result would have been exactly the same. The effective agent, if you will, was in the commandment. Consequently, they gained the knowledge of good and evil experientially by disobeying the commandment of God. Their disobedience immediately put a breach between themselves and their Creator, and it awakened their conscience. This was a very real change, but it was a spiritual change, not a physical effect.
Now, to answer the question as stated, the existence of the tree of knowledge was necessary to provide mankind with a moral environment. Mankind was a moral creature from the beginning. That is part of our necessary being. This seems to be a consequence of being a spiritual being created “in the image of God.” But, for that moral nature to have any relevance, it must be in an environment where moral choice is possible. So the existence of the tree of knowledge in the garden was necessary to provide a moral context to Adam and Eve as moral creatures. Adam and Eve had the choice of trusting the goodness of God and obeying, or distrusting God and seeking to obtain some benefit on their own.
Notice that this view is contrary to the common idea that before the fall mankind was innocent in the sense that he was without any moral sensibility. In this case, he would have gained something by his disobedience. This line of reasoning could be used to imply that God was not perfectly good because Adam and Eve would have gained some important knowledge on their own by disobedience. In fact, if Adam and Eve had just thought about what the decision to take or not to take implied, it is conceivable that they could have gained a certain (can we say “vicarious”?) knowledge of good and evil without taking of the tree at all. It was distrust of the goodness of God that led to disobedience to the commandment of God; this is the essence of sin (1 Jn. 3:4).