I just completed a Data structures and Algorithms course in my university. I know how data structures work and how different DS are used to solve specific and to improve efficiency. I have also started to work with databases to develop CRUD applications.

My question is: If I have to for example, use a tree data structure (like a max heap) or maybe a linked list, would I just store it in a plain table and then fetch it and re form that data structure? This seems like a lot of work when you have larger data sets, and the same goes with appending data to that data structure and saving it in database.

I have seen a lot of online explanations here and on stackoverflow, but I couldn't get a satisfactory answer to clear my understanding. I would be grateful if anyone answers it thoroughly and doesn't marks it duplicate. Thanks :)


1 Answer 1


The data structures and algorithms you learnt are majorly dealing with a specific way of storing and accessing data which is stored in the main memory.

When you move to, secondary memory the same can't be applied as it wouldn't be efficient, simply because the cpu/main memory is much faster than the secodnary memory, and the OS is designed in such a way that anything it needs to access first need to be available in the main memory. This is done so that the cpu doesnt need to have to make an I/O call everytime it needs some data from the disk(main memory can be thought of as a cache of secondary memory). So the focus on data structures while dealing with secondary memory is to reduce calls and the time complexity to retrieve data since we are now dealing with a much higher volume of data.

To answer your question:If you are storing a data structure inside of a database, I am assuming its some sort of dump of a particular instance of that data structure. This is just adding to the existing complexity of the database and isn't actually very useful. For example, Relational databases are built upon the concepts of B+ trees, so why would you want to explicitly do the same thing. You could intelligently run queries instead which would give you the same functionality without having to get your hands dirty. Of course you can experiment and do anything but chances are, using the database functionalities efficiently will get more of your work done than you trying to do it.

  • $\begingroup$ Right, so just to get this straight, I would fetch data from database using its built in queries, then load it into my preferred data structure? (sorry if this is dumb) $\endgroup$
    – Ali Ahmed
    Dec 20, 2021 at 15:27
  • $\begingroup$ Yes. It entirely depends on what you want to do with it though, if you want to work with a stack, instead of fetching data and inserting into your own stack, try simulating a stack with the table. Its not too difficult to write queries which can perform the functionality of a stack. Similarly, a linked list could also be done. You can think of the table as an array, a matrix or whatever abstraction you like and perform operations accordingly. SQL has become pretty mature and is turing complete considering recent implementations, so you can pretty much do a lot with it apart from just CRUD. $\endgroup$
    – Rinkesh P
    Dec 21, 2021 at 5:14

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.