Trie/Tree addWord() method

Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Reply

Join Date: Jun 2005
Posts: 14
Reputation: nisaa15 is an unknown quantity at this point 
Solved Threads: 0
nisaa15 nisaa15 is offline Offline
Newbie Poster

Trie/Tree addWord() method

 
0
  #1
Nov 21st, 2005
Hi All,

Im trying to implement a trie in java(which is basically a tree used to store words). I have the following method to add a word but am unsure how i would go about check to see if the prefix of a word already exists. Heres my code:

public void add(String word){
for (int i = 0; i<=word.length(); i++){
char c = word.charAt(i);
if(c==..............(trieEdge(c))){ //unsure about this, should check whether an edge with the character c exists in the ith position.
i++;//should go to the next character and node if an edge exists
} else{
word.makeEdge(c);
}

//hereafter I check to see if the end of the word has been reached in wich case the makeNode/edge would place a terminal node at the end rather than a normal node.

If anyone can give me suggestions as to how I can check to see if the edge already exists in correspondance to the ith position this will be very much appreciated

Cheers
Last edited by nisaa15; Nov 21st, 2005 at 7:53 pm. Reason: spelling mistake
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Trie/Tree addWord() method

 
0
  #2
Nov 22nd, 2005
startsWith(String prefix)
endsWith(String postfix)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 14
Reputation: nisaa15 is an unknown quantity at this point 
Solved Threads: 0
nisaa15 nisaa15 is offline Offline
Newbie Poster

Re: Trie/Tree addWord() method

 
0
  #3
Nov 22nd, 2005
how would i know which node im talking about, i want to do it so that the program checks individual characters rather than prefixes. so i would have to check a nodes children but how can i determine which node the program should look at. It should start by looking at the rott nodes children and if the edge exists here follow the edge to the next node and check to see if the next character is amonst the new nodes children and so forth.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Trie/Tree addWord() method

 
0
  #4
Nov 23rd, 2005
What do you mean you don't know what node to look at?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 14
Reputation: nisaa15 is an unknown quantity at this point 
Solved Threads: 0
nisaa15 nisaa15 is offline Offline
Newbie Poster

Re: Trie/Tree addWord() method

 
0
  #5
Nov 23rd, 2005
Originally Posted by server_crash
What do you mean you don't know what node to look at?
Well I want to start the search from the root node. How can I represent the edges that the node has? once the computer knows if the edge exists if will follow this edge through to it destination node. My problem is - how do I represent the edges one node has?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Trie/Tree addWord() method

 
0
  #6
Nov 23rd, 2005
Originally Posted by nisaa15
Well I want to start the search from the root node. How can I represent the edges that the node has? once the computer knows if the edge exists if will follow this edge through to it destination node. My problem is - how do I represent the edges one node has?
Well, if you are using Java, this is pretty darn easy to do. Each node could have a list of node references (or pointers, in other languages) within its structure. For example:

class Node {
    //  Note that this is public for example only, you should write your own setting and accessing methods for the nodes.
    public Node childOne, childTwo;

    public Node() {
        childOne=null;
        childTwo=null;
    }
}

This example class could be a node in a tree. Note that childOne and childTwo are just references to Node objects, and are set to null in the constructor. childOne and childTwo can represent the children of that particular node. Each node will actually represet a sub tree using this method. So, you can just do this in your tree structure:

class Tree {
    public Node root;
}

Of course, you can have more than just two node references in your node class. You could just have one node (which would be a linked list), or you could even have a list of nodes (which would be a full blown tree). For your example you probably want each node to have two child nodes, and create a binary search tree from that. Google would be a good place to start for finding out more.

-Fredric
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4468 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Computer Science
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC