General Tree Implementation

Ilies Ait Hamouda
1 min readMay 7, 2021
Photo by Sean Brown on Unsplash

So for my last week as a software engineer at Company X, I had to implement a tree DS -__-!

Here’s how to do it (java) :

First you’ll need some interface (yay!) for the tree (probably for the node, but it’s up to you really…)

Next, you may (or not) want to add an abstract class that implements your tree interface (for the most common needs) as follow:

Then, you’ll need a class for the Node:

And of course you’ll need an implementation of your Tree interface, mine looks like this :

And that’s it.

oh oh no, that is not IT. Actually you’ll have to make sure to populate your tree properly AND your element contained within the node needs an implementation of the equals method in order to do a proper comparison when needed.

Ok now, you’re good to start coding!, I hope that helps and if you have any questions, comments or something’s wrong in my code, please leave a note in the comment section and if you liked the post or it helped, 👏

Thank you!

--

--