0

I've created a node class and a tree class. From main, I call suffixTree t = new suffixTree(string); It is on a while loop so it will always be variable t.

The problem is, I want to read an input file and create a new tree for each string. Apparently, it won't create a new instance.

The variable "t" is the same in each interaction but it should be a new instance everytime it creates it. The tree constructor, has a Node root = new Node();

It is a copied code, the only thing I did was to read from input and traverse the tree.

The problem is, if I type mississippi$ then acacdcacd$ it adds to the same tree and give a wrong result as I traverse it.

Thanks in advance

d0pe
  • 573
  • 4
  • 9
  • 23
  • Please be more specific. What isn't new instance? – Jan Krakora Dec 14 '12 at 15:42
  • 1
    Paste the relevant code here. Tell us what the code is supposed to do, and what it does instead. – JB Nizet Dec 14 '12 at 15:42
  • 1
    Don't show the code from somebody else, show us your code, the one which is problematic. – PhiLho Dec 14 '12 at 15:42
  • Ok, I added the code. Basically it's the same as the author but, with Traverse function and reading from stdin. – d0pe Dec 14 '12 at 15:52
  • You create a new local variable t1, initialize it with a new SuffixTree, and then forget about the created object completely. You should probebly do something with t1, but only you know what. – JB Nizet Dec 14 '12 at 15:58
  • I changed the code so I use in fact t1, I traverse it after creating it. Now, I want to be able to reuse it on next loop, so I don't need to create an array wating for garbage collector to do it's work. I want a simple free(t1) after I traverse the tree – d0pe Dec 14 '12 at 16:20

1 Answers1

0

You are creating new instances but you are discarding them right after creation because they are not stored anywhere and as such they are unreachable and marked as ready for garbage collection.

have a look at the following:

// this is where the SuffixTree instances will end up
List<SuffixTree> suffixes = new ArrayList<SuffixTree>();


String[] strings = new String[3];
for (String string : strings) {
    // A new suffix tree is created here but if you don't do anything
    // with it then it is marked as garbage collectable when the closed
    // curly brace is reached
    SuffixTree t = new SuffixTree(string);


    // Now I'm pushing the object into the suffixes list: this will prevent
    // the loss of the object currently stored in the t variable
    suffixes.add(t);
}
  • I am aware of it but the point is, I won't need that suffix tree after, I want to make it, traverse and print the result. I don't need to store it cause I will never use it again. However, I wan't to make a new tree based on a new string, using the same variable. You can just make new SuffixTree(string), I won't use t1 nor any result from SuffixTree as I'm printing after traversing it – d0pe Dec 14 '12 at 16:04
  • What I truly want is pretty much a FREE on t1, I want garbage collector to trash the tree so I can use it again on the same variable. I've tried System.gc() but it won't help. I'm considereing changing to c++ just so I can call free(t1) after and I am ableto use it on next loop – d0pe Dec 14 '12 at 16:15
  • So why are you creating it? – Roberto Lo Giacco Dec 14 '12 at 16:17
  • In Java the free(t1) is done implicitly when the closed curly brace is reached. Calling System.gc() you are suggesting the JVM that now is a good moment to perform garbage collection and free up the heap space used by t1. – Roberto Lo Giacco Dec 14 '12 at 16:19
  • The problem is, t1 is on a while loop, it won't free inside the while cycle as I wanted it to. Is that impossible to accomplish? – d0pe Dec 14 '12 at 16:23
  • When do you want to create a new SuffixTree to replace t1? – Roberto Lo Giacco Dec 14 '12 at 16:26
  • Bascially, I'm reading a list of strings, building the tree then traversing it, doing this till I run out of input strings. However every string is different and needs a new SuffixTree. I want to create it in the same while loop where I created it for the first time. – d0pe Dec 14 '12 at 16:29
  • You are creating a completely new SuffixTree replacing t1 value every time you enter that branch of the while loop. If you want to create a new SuffixTree every time the while loop cycles then move the declaration and instantiation at the beginning of the loop, outside of the else and the nested if statements. – Roberto Lo Giacco Dec 14 '12 at 16:30
  • I wish I was, but I'm not.... I don't know why but after a string search, like asdasd$, if I make another search zxczxc$ it will add to the last suffix tree created since when I traverse the first time, it gives the right result but, the second time, it still appears asdasd in there when it shouldn't, it should only appear the zxczxc sequence. – d0pe Dec 14 '12 at 16:36
  • You are not building a tree at all because when you create a SuffixTree the root of that suffix tree is set to a new Node and when you call the traverse method you are using that new node as root. – Roberto Lo Giacco Dec 14 '12 at 16:36
  • @d0pe: trust the garbage collector. If it needs to, it will garbage collect your trees. If you have a bug, it's in YOUR code, and has nothing to do with the GC not collecting your objects. – JB Nizet Dec 14 '12 at 16:38
  • 1
    That is because you never cleaned up the str variable!!!!! Add the following after the call to traverse: `str = "";` – Roberto Lo Giacco Dec 14 '12 at 16:40
  • Fcking yeah! So dumb -.- So much time wasting and was such a simple solution. Thanks alot mate – d0pe Dec 14 '12 at 16:42