1
$\begingroup$

Here is the problem, i have a graphical component that displays a tree. It takes as input an array of nodes and array of edges(eg:an edge has a source and target).

I will be performing complex tasks, such as building the tree dynamically from user input, removing nodes adding nodes, hiding a node's children, i would need to traverse certain paths and collect information, and all things of tree such as getting children of a node or get all leafs etc.

I have two solutions: either manipulate directly both arrays i already tried with hiding a node's children and it was fine.

Or map the two array into a tree structure and manipulate the tree to do what i want since there will lots of ready algorithms then map it back to the two arrays.

The first solution sounds better and easier at first, however if things get more complex i will implement each function i need (such getting leafs etc) and the diffculty is manupilating a tree in a non tree representstion.

The second solution is better since its a tree there are a lot of algorithms but there is more work on mapping back and forth on each addition or deletion.

What do you think is better? (A compromise between being it easier fof me and performance)

$\endgroup$
1
  • 1
    $\begingroup$ This sounds like a matter of opinion. What's your criteria for "better"? Better by what metrics? We're not looking for opinion polls. See help center. It sounds like you've identified a possible tradeoff; we can't tell you which to choose because that depends what is more important to you, or how you value ease of implementation vs performance. I think this site works best for objectively answerable technical questions about computer science. $\endgroup$
    – D.W.
    Commented Dec 10, 2020 at 20:36

1 Answer 1

1
$\begingroup$

In the unforgettable words of Donald Knuth

Premature optimization is the root of all evil.

Your best option is as follows. Do what is simplest for you, while maintaining a good abstraction with good encapsulation. A good abstraction (a well-defined and thought out API) will make it easy for you to change and improve the underlying implementation in the future while not having to change the rest of the application.

When (or really if) you discover that the application is too slow, then you start changing the underlying implementation, but only after a proper complexity analysis.

It is very difficult (if not impossible) to say in advance what will be the bottleneck of the application, so it's best to leave this decision until you actually know.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.