- Joined
- Jan 7, 2005
- Messages
- 7,550
Is anyone here experienced in Java and Binary Trees? I got my last university programming assignment and it is a tough one. I have got to build a tree in which words of a text will be inserted. So I made a nested leaf class where each leaf/node will store a final String that is the word and an integer occurrences, as well as an infix iterator (as in, it visits nodes by order: left subtree; root; right subtree) for the main tree class (which is theoretically working).
I believe I have the insertion and structural parts figured out, but the real problem is what follows:
How to create a method that constructs a list of words starting with a set pattern? As in, find all words that start with 'are'.
Of course I could iterate through the entire tree and find out, but that is terribly inefficient and wastes unnecessary time: if I'm searching for a pattern starting with 'za', for example, the tree is going to iterate all the way down from 'a' to 'z' before it finds the words, being O(n) at worst. O(n) is sorta bad here since it is a literary text: there will be a LOT of different words.
I can't really find a pattern here: for example inserting the words 'a', 'boar', 'boat', 'c', 'botox', 'bulldog'. If I try to find words starting with 'bo'...
...I wonder if I have to climb down the tree to find the first word that starts with 'bo', add the left subtree for iteration and then climb down to the right until I have found a word that doesn't start with 'bo', but add all the nodes to the left of that for iteration?
I believe I have the insertion and structural parts figured out, but the real problem is what follows:
How to create a method that constructs a list of words starting with a set pattern? As in, find all words that start with 'are'.
Of course I could iterate through the entire tree and find out, but that is terribly inefficient and wastes unnecessary time: if I'm searching for a pattern starting with 'za', for example, the tree is going to iterate all the way down from 'a' to 'z' before it finds the words, being O(n) at worst. O(n) is sorta bad here since it is a literary text: there will be a LOT of different words.
I can't really find a pattern here: for example inserting the words 'a', 'boar', 'boat', 'c', 'botox', 'bulldog'. If I try to find words starting with 'bo'...
...I wonder if I have to climb down the tree to find the first word that starts with 'bo', add the left subtree for iteration and then climb down to the right until I have found a word that doesn't start with 'bo', but add all the nodes to the left of that for iteration?