A few general ideas:
Firstly, Patricia trie is probably the best known approach for improving memory consumption by trying - it combines paths in which all nodes have one child in one node and combine characters along the path. There is also a version in which you view data as a sequence of binary digits, which has the advantage that you always have no more than 2 child nodes, and is also easier to implement.
Secondly, memory consumption really depends on how you store the children of this node - do you support an array of 256 nodes? This is usually the most efficient direct search method, but also consumes most of the memory and slow if you need to sort through all the children. Other options:
Saving an array of pairs (letter, child node) is perhaps the most efficient memory, since it only stores those objects that you are really interested in, and also has good performance for iterating through all the children. However, you should check all pairs for a direct search - which is usually different from the root, but can be a problem near the root.
Keep some kind of dictionary inside each node that maps the letter to the child element of the node. This is the most balanced in terms of performance - it gives you good enough speeds for all operations and has some efficiency in working with memory.
In addition, if you build the entire collection forward and then simply query it, there is an approach for storing child links based on Tarjan tables , which will probably increase the build time, but later save the memory and query time.
source share