The Trie Data Structure: Examples in Java | Toptal
From the very first days in our lives as programmers, we've all dealt with data structures: Arrays, linked lists, trees, sets, stacks and queues are our everyday companions, and the experienced programmer knows when and why to use them. In this article we'll see how an oft-neglected data structure, the trie, really shines in application domains with specific features, like word games.
Word Games
For starters, let's consider a simple word puzzle: find all the valid words in a 4x4 letter board, connecting adjacent letters horizontally, vertically, or diagonally. For example, in the following board, we see the letters 'W', 'A', 'I', and 'T' connecting to form the word "WAIT".
The naive solution to finding all valids words would be to explore the board starting from the upper-left corner and then moving depth-first to longer sequences, starting again from the second letter in the first row, and so on. In a 4x4 board, allowing vertical, horizontal and diagonal moves, there are 12029640 sequences, ranging in length from one to sixteen characters.
Read full article from The Trie Data Structure: Examples in Java | Toptal
No comments:
Post a Comment