Require help with trees

Whenever we come across a problem which uses Trees in CP , how should we implement it ?
Should we make a Tree data structure using Struct/Class or go for stacks /queues as used in DFS/BFS?

I will tell you how I do it. I generally use arrays, vector to do any sort of representation of tree. So far I have implemented almost all the basic and advanced data structures using arrays only. I prefer to do it in this way because it avoids using pointers, . operators, new keywords. The time taken to code in this way is faster.

i agree that while using struct/class the code has more clarity and it is easily understandable by other coders who go through your code.

There are certain requirements which forces us to represent the node by struct/class in that case we have to. For example in a segment tree problem if we need to merge two nodes and all this happening in a recursive call.

I have recently implemented persistent trie using only arrays, vectors. Here is the link(GPD).

2 Likes