Masturbation has been shown to mollify depression and trauma, but unfortunately, for a very short time. So next time you have any problem, try to deal with it right away and not to let it affect your life. In short, if you try to live a more happy life, it's likely that you'll not miss the euphoric feeling masturbation can provide. That's a way to prevent addiction to it.
Also, whenever you have that urge, DO NOT do anything that can arouse you more. Instead, try to do something that requires lots of focusing: reading, watching action or thriller movie, or even gaming. Hurting your **** (you know what it is) enough so that the urge disappear is also suggestible, but do not overdo it :)
However, the most important thing is not what to do, but how to stop yourself from succumbing to that sort of desire. Train yourself to say "No" everytime you want to do it, no matter how appealing it may seem. That's it. Believe me it'll only take about 1 week to deal with your problem.
Good luck :)