0

In my college, I learned how to define data structures like binary trees and graphs. We were made to write c++ programs that used to run and added nodes to the tree( that we told the compiler to add) . And once we were done adding nodes to the tree, we used to do lookups of different values to make sure our program was working... The code looked similar to the standard code for a binary search tree you could find on geeksforgeeks. But now I'm trying to build a website that helps students in finding details about the teachers of our college... The key here will be the numeric employee id of the teacher and I want to implement a binary search tree on the backend... And I can't figure out how to implement it.

One option that appears in front of me is to run a js script that defines a new tree from the linear array of teachers that I have right at the time when a request comes to the backend asking for a particular teacher's data. I will run the search function defined in my code after the tree is done being constructed and give the returned value as response to user...

But won't this be inefficient? Having to define a tree from array again and again just to return the result of one persons query? Isn't there a way to permanently store the tree and keep it ready to go for quickly answering every person's query?

Is there a way to store a tree in a quick access file or some way to keep the server ready to search entries?

How do I make a tree persist so that it stays ready for lookups when different users request data from it instead of having to run the tree construction code again and again?

How are these efficient data structures actually implement in applications like websites, software, etc. ?

  • "*But now I'm trying to build a website that helps students in finding details about the teachers of our college... The key here will be the numeric employee id of the teacher and I want to implement a binary search tree on the backend*" - no, you don't. You want to use a database - which has this (and many more efficient algorithms) implemented for you. – Bergi Apr 09 '20 at 16:38
  • "*But won't this be inefficient?*" - Yes. "*Isn't there a way to permanently store the tree and keep it ready to go for quickly answering every person's query?*" - sure there is. How are you doing it with the "linear array of teachers", how is that stored? – Bergi Apr 09 '20 at 16:39
  • It will just be an array of java script object comprising of child JavaScript objects... Its just a minified json object. I'm talking about how the linear array is stored – Vaibhav Chopra Apr 09 '20 at 16:42
  • And you're reading that JSON file from disk every time a request is served? I hope not. – Bergi Apr 09 '20 at 16:43
  • Yeah... I am... And I am looking for some faster alternative – Vaibhav Chopra Apr 09 '20 at 16:44
  • I dont have a very deep knowledge of how servers work...but isn't there a way to keep a defined data structure ready in the backend and to just keep querying information from this data structure instead of having to resurrect it again and again on each request. I want to how these data structures are actually used in applications by companies. I don't have a lot of knowledge in this domain. – Vaibhav Chopra Apr 09 '20 at 16:46
  • Well, read it only once, convert it to the tree structure, then keep the object in memory to server *multiple* requests with it. Can you post your code, please? – Bergi Apr 09 '20 at 16:47
  • "*I dont have a very deep knowledge of how servers work...but isn't there a way to keep a defined data structure ready in the backend*" - yes, there is. It's in fact the default in the most common modern server frameworks. Only oldish [cgi scripts](https://en.wikipedia.org/wiki/Common_Gateway_Interface) did not keep state but operated by running a full program on every single request. – Bergi Apr 09 '20 at 16:51
  • So on a hosting platform like heroku, no special instructions have to be given to make sure that variables and data structures don't have to be defined or constructed again and again? – Vaibhav Chopra Apr 09 '20 at 16:54
  • Isn't a binary search tree simply nested objects? Would be simple enough to have a toString function that iterates over the entire tree building up a string representation that could then be saved to a file. Implementing some sort of smart save would be essential for cutting down inefficiencies in re saving the entire tree ever time. – b.stevens.photo Apr 09 '20 at 16:54
  • Yeah , but building up from string representation would also take some setup time... According to @Bergi, from what I understand, after deploying to newer hosting sites, a tree will only be constructed when the server starts, after that each new request just draws from the previously set up variables and data structures. This is what I understood, I could be wrong though – Vaibhav Chopra Apr 09 '20 at 16:58
  • @VaibhavChopra Yes. (It depends on your runtime framework though, and Heroku offers many. Given this is tagged [javascript], I assume you are using Node.js?) – Bergi Apr 09 '20 at 16:59
  • Yes I'm using node js – Vaibhav Chopra Apr 09 '20 at 16:59
  • @bryanstevens314 No, saving the tree to a file and then loading that file on every request does *not* solve the problem. Besides, the binary tree example doesn't matter for the core question anyway. – Bergi Apr 09 '20 at 17:00
  • Yes I'm not interested in just a binary search tree. I was looking for a generic approach for setting up complex data structures in practical real life examples like a website that requires searching from a large set of entries – Vaibhav Chopra Apr 09 '20 at 17:01
  • Maybe have a look at https://stackoverflow.com/questions/51495928/how-does-node-process-concurrent-requests? – Bergi Apr 09 '20 at 17:03
  • @VaibhavChopra Real life examples of websites that search large data sets all use databases :-) However, a database is not magic, it is implemented using clever algorithms and data structures as well, and also serves many queries at once. – Bergi Apr 09 '20 at 17:03
  • I don't wanna use a database for some set of data that will only be read from and not be modified for several months. I know this is is not a valid argument against using a database. But this once I want to do it with just javascript. I want to use all those complex data structures we wrote long pseudocodes for in exams for once :) – Vaibhav Chopra Apr 09 '20 at 17:07
  • And doing this successfuly without a db might actually give better performance in my use case... Because there might be some overhead in establishing a connection to a database as well... If I take the db route, then I will most probably end up using mongo DB atlas for it and the cluster will be located in a different location than the heroku cluster – Vaibhav Chopra Apr 09 '20 at 17:09

0 Answers0