Trie Tree Practise – SPOJ – PHONELST

Hello people..! In this post I will show you how to get started with solving Trie Tree based questions in competitive programming. Learning a data structure is different from solving competitive coding questions based on that data structure. In this post, I take up a very simple question so that your confidence is boosted.
We will look at the SPOJ problem – Phone List. It is a very straight forward problem. Just so that you don’t need to go to SPOJ in a new tab, I’m putting the problem statement here.

Problem Statement

Phone List Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:

• Emergency 911

• Alice 97 625 999

• Bob 91 12 54 26

In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.

Input

The first line of input gives a single integer, 1 <= t <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.

Output

For each test case, output “YES” if the list is consistent, or “NO” otherwise.

The Problem in terms of Trie Tree

We are supposed to check if any word is a prefix of any other or not. Now, there might be a hundred ways to solve this problem, but we will do this using a trie tree so that we can get confident in using the data structure, and so that we can attempt tougher ones based on a trie tree. All throughout my explanation, I will be referring to the implementation in my post on Trie Tree.
What we will do to solve this problem is that, we will insert the words into the trie tree one-by-one, and we will check for the prefix word criteria as we are inserting. Now, there are 2 cases.

Case – 1 – Prefix word is already inserted

This is the sample test case. Consider this test case –

Input
face
facebook

So, what I said we will do is that, we will be inserting the words one-by-one. So, when we insert the word “face”, no problems occur. But while inserting the word “facebook”, we would travel to the nodes F → A → C → E. And at the node E, we would have some indication that this node is a leaf node, that is, some word ends here. In my implementation, this can be indicated by –

if (temp->occurrences.size() == 0) {
	// not a leaf node
} else {
	// a leaf node, thus this is
	// the end of the prefix word
}

If we encounter this situation, we know that the result will be NO.

Case 2 – Prefix word is about to be inserted

This is just the opposite of the previous case, consider the test case –

Input
facebook
face

We won’t have any issues while inserting “facebook”. But when inserting “face”, we traverse F → A → C → E. And in the end of insertion, we see that there is a child node to the node E. Which means that there is a bigger word which ends somewhere deep down the trie tree. Which means that we just inserted the prefix word. We could check this by traversing the children array –

for (i = 0; i < ALPHABETS; ++i) {
	if (temp->children[i] != NULL) {
		// The newly inserted word is
		// prefix to another word
	}
}

In this case too, the answer would be a NO. For every other case, the answer would be a YES.

So, try to code this problem. Take your code of the trie tree, remove the unnecessary things first, like the delete function or print function or any others which we won’t need. As fas as I know, the insert function is all that you will need. And try to make the changes required for the test cases.
Your insert function could return a true or a false, depending on whether the insertion encountered a prefix test case or not. Take time to think about it and try to code it. If you get stuck, you can refer to my code below –

    

A word of caution –

  • Even if you did encounter a prefix word very early, don’t break out of the input, as you will disturb the input for the next test case.

I hope that you were able to solve this problem using a trie tree. It is simple and a prefect problem to start your trie tree streak in competitive coding. Feel free to comment if you have any doubts. If you have any bugs in your code, I’d be glad to help, but don’t comment your entire code in the comment, please leave Ideone or PasteBin links, or if you don’t want to show your code publicly, you can fill up the response form below to mail your code to me. I will respond as soon as I can. Keep practising… Happy Coding…! 😀

6 thoughts on “Trie Tree Practise – SPOJ – PHONELST

    • I’ve seen your code… You are not checking for the case when the prefix word is already inserted… When the prefix word is already inserted, you might encounter it while traversing

      if (curr->leaf) {
           result = true;
      }
      

      or, the word which you inserted was already inserted before… In which case, you must check for status of ‘leaf’ after traversing… Both these cases must be checked before leaving the procedure –

      if (curr->leaf || result) {
           return true;
      }
      

      You can see your code with the neccessary corrections on this link… And thanks a lot for the suggestion…. I’ll surely add more easy questions… 🙂

      • Hi thanks for your reply 🙂 I am so sorry but I can’t imagine why do we need to check for leaf condition ? Please if you can elaborate when you have some spare time.I will keep giving you links for the topics that you have in your blog.Hoping that would be helpful to you 🙂

        • There are 2 scenarios where we would have to check the leaf condition –

          • The same phone number is inserted twice – When the same number is inserted twice, we are supposed to print “NO”… Why..? You can’t call the second person with the same phone number. How do we check this condition, after inserting a word, let’s say “2015”, at node “5” we check if the “leaf” flag is already set to true or not. If “leaf” is true, then “2015” was already inserted.
          • Prefix word is already inserted – Suppose we are inserting “20152015”, when “2015” is already inserted in the trie tree… Then, we won’t be able to call the person with phone number “20152015”, because once we type “2015”, we are calling another person. So, in this case, the prefix word is already inserted. How do we check this condition…? While inserting “20152015”, at each node, I check if it is a leaf node. In this case, after 2 → 0 → 1 → 5, the node “5” is a leaf node. So in this case too the answer is NO.

          Think about it… You will definetly get it… 😉

Leave a Reply