“Scanning through an English dictionary is faster because, you directly go to the indexed character and scan through letter one-by-one to get to the exact text you are searching for”
Sorry for not updating for a long period of around 5 months. Really struck up with busy schedules. Now I forgot all the data structures and algorithms. So need to relearn and hence will revisit all my previous posts before going into medium difficulty content.
How would you design a search algorithm which does a search through the English dictionary in really fast manner? Manually, you can easily check a dictionary based on lexicographic ordering. But how would you design a data structure which can naturally allow you to do that? you can do this with a binary search tree.
In previous article, we learnt about Hash Tables(http://analgorithmaday.blogspot.in/2011/10/hash-tables-dictionaries.html) that it is an array index to data mapping. This is a direct mapping hash table which can be generically implemented using binary search trees. If you check the implementations of map and hash_map in C++ STLs, these are implemented using a kind of binary search trees.
We decide left or right of the binary search tree based on the value we need to search for. It reduces loops logarithmically.
Bonus: We can also check how to get the 4th smallest element out of the binary search tree. This can be done by having an extra field which can calculate the rank of the node.
Rank of a node in BST is number of nodes in the left sub-tree + 1. This is nothing but number of keys less than the current value. This rank is helpful to identify the successor and predecessor easily.
If space is not a constraint, then we should go with ranking the nodes. But if its a constraint, then you should identify the successor of each node
Searching a binary search tree (iteration)
Searching a binary search tree based on ranks
Will see this in next article about constructing binary search trees with ranks.