DEV Community

Discussion on: How often You Have to use the Concepts of Trees in your algo

Collapse
 
hkrogstie profile image
Håvard Krogstie • Edited

It really depends on the program, but even then you rarely build trees yourself, even in algorithms competitions I find. Save for when the algorithm input itself is a tree, or when a Binary Segment Tree is useful.

But I do use trees, just without it being obvious. The set datastructure has O(log n) complexity for insertion, deletion and search, AND it's sorted! Super useful in a plethora of tasks, also outside of the world of algorithms competitions. Sets use an underlying tree in the C++ STL. Sure you could use a linked list and achieve O(1) insertion and deletion, but the cache misses and memory fragmentation is awful.

Point is, know your data structures and you'll probably use trees when applicable.

Sidenote: I dont know how your language implements sets and maps, but it may use a hash map, where the data isn't sorted.
Also heeps are sorta implemented as trees, used by every priority queue I've ever known.