473,382 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Initialed value lost !

Hi there,
I am trying to develop a compressed suffix trie.
But first I want to get the trie working.
The structure of the trie is like

class trie {
private:
bool last_node;
map<char, triechildren;

//methods
.....
.....
.....
}
The problem is that after inserting one word,the values held by the
private members of the nodes(other than the root is lost).
insert is a recursive function
new node is added like this
trie new_child;
this->children[alphabet]=new_child;
new_child.insert(word.substr(1));//insert is called again.
I had stepped through to debug
this is what happened.I had a pasteing the value of this ppointer and
the data member last_node

when I insert the first waord "ca"

//for the root node
entering this pointer 0xbfe254a0
pointer 0xbfe254a0 0//last_node is false

the another node is created added into the map and in that node i set
the last_node to true
this->last_node=true;

after setting
pointer 0xbfe25430 1 //last node set to true
now i insert the word "cab"
the root node is fine

entering this pointer 0xbfe254a0
pointer 0xbfe254a0 0

but then from the map based on the letter i go to the next node(to
node a).but then the last_node member is false even though i had
set it true before
pointer 0xbfe25430 0

could anyone tell what I am doing wrong?
Aug 9 '08 #1
10 1494
sa*********@gmail.com wrote:
Hi there,
I am trying to develop a compressed suffix trie.
Just a side node: It's called "tree".
But first I want to get the trie working.
The structure of the trie is like

class trie {
private:
bool last_node;
map<char, triechildren;

//methods
....
....
....
}
The problem is that after inserting one word,the values held by the
private members of the nodes(other than the root is lost).
insert is a recursive function
new node is added like this
trie new_child;
Ok, so you create a local instance of 'trie'.
this->children[alphabet]=new_child;
Then you copy it into your children map.
new_child.insert(word.substr(1));//insert is called again.
Now you change the local object, which doesn't affect the copy in your map
at all. At the end of the function, the local object and all the changes
you did to it will be lost.

Aug 9 '08 #2
sa*********@gmail.com wrote:
Hi there,
I am trying to develop a compressed suffix trie.

But first I want to get the trie working.
The structure of the trie is like

class trie {
private:
bool last_node;
map<char, triechildren;

//methods
....
....
....
}
The problem is that after inserting one word,the values held by the
private members of the nodes(other than the root is lost).
insert is a recursive function
new node is added like this
trie new_child;
Ok, so you create a local instance of 'trie'.
this->children[alphabet]=new_child;
Then you copy it into your children map.
new_child.insert(word.substr(1));//insert is called again.
Now you change the local object, which doesn't affect the copy in your map
at all. At the end of the function, the local object and all the changes
you did to it will be lost.

Aug 9 '08 #3
On Aug 9, 4:11 pm, Rolf Magnus <ramag...@t-online.dewrote:
sam.bark...@gmail.com wrote:
Hi there,
I am trying to develop a compressed suffix trie.

Just a side node: It's called "tree".
But first I want to get the trie working.
The structure of the trie is like
class trie {
private:
bool last_node;
map<char, triechildren;
//methods
....
....
....
}
The problem is that after inserting one word,the values held by the
private members of the nodes(other than the root is lost).
insert is a recursive function
new node is added like this
trie new_child;

Ok, so you create a local instance of 'trie'.
this->children[alphabet]=new_child;

Then you copy it into your children map.
new_child.insert(word.substr(1));//insert is called again.

Now you change the local object, which doesn't affect the copy in your map
at all. At the end of the function, the local object and all the changes
you did to it will be lost.
Ah makes sense.I was thinking like it was a pointer.So if I
interchange the lines
trie new_child;
new_child.insert(word.substr(1));//insert is
called again.

//after inserting the nodes then connect it
back to the parent
this->children[alphabet]=new_child;
I think this should be a correct solution.Please correct me if I am
wrong.
Aug 9 '08 #4
sa*********@gmail.com wrote:

>Now you change the local object, which doesn't affect the copy in your
map at all. At the end of the function, the local object and all the
changes you did to it will be lost.

Ah makes sense.I was thinking like it was a pointer.So if I
interchange the lines
trie new_child;
new_child.insert(word.substr(1));//insert is
called again.

//after inserting the nodes then connect it
back to the parent
this->children[alphabet]=new_child;
I think this should be a correct solution.Please correct me if I am
wrong.
Yes, this looks good. Another solution would be to let the map create a new
element and then use a reference to it. That way you can save the copying.
Something like that:

trie& new_child = this->children[alphabet];
new_child.insert(word.substr(1));//insert is called again.

The behavior of that differs from your solution if there is already an
element with that index in the map.

Aug 9 '08 #5
On Aug 9, 4:57 pm, Rolf Magnus <ramag...@t-online.dewrote:
sam.bark...@gmail.com wrote:
Now you change the local object, which doesn't affect the copy in your
map at all. At the end of the function, the local object and all the
changes you did to it will be lost.
Ah makes sense.I was thinking like it was a pointer.So if I
interchange the lines
trie new_child;
new_child.insert(word.substr(1));//insert is
called again.
//after inserting the nodes then connect it
back to the parent
this->children[alphabet]=new_child;
I think this should be a correct solution.Please correct me if I am
wrong.

Yes, this looks good. Another solution would be to let the map create a new
element and then use a reference to it. That way you can save the copying.
Something like that:

trie& new_child = this->children[alphabet];
new_child.insert(word.substr(1));//insert is called again.

The behavior of that differs from your solution if there is already an
element with that index in the map.
Yep.Thanks for your suggestion.
Cheers,
Sam
Aug 9 '08 #6
On Aug 9, 8:11 am, Rolf Magnus <ramag...@t-online.dewrote:
sam.bark...@gmail.com wrote:
Hi there,
I am trying to develop a compressed suffix trie.
Just a side node: It's called "tree".
No it's not. At least not if I've correctly understood what
he's doing. A trie is a special kind of tree.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 9 '08 #7
James Kanze wrote:
On Aug 9, 8:11 am, Rolf Magnus <ramag...@t-online.dewrote:
>sam.bark...@gmail.com wrote:
Hi there,
I am trying to develop a compressed suffix trie.
>Just a side node: It's called "tree".

No it's not.
Yes, I found that out right after posting. I tried canceling the post, but I
guess, it was too late already.
At least not if I've correctly understood what
he's doing. A trie is a special kind of tree.
http://en.wikipedia.org/wiki/Trie

Aug 9 '08 #8
Hi again,
class trie {
private:
bool last_node;
map<char, triechildren;

//methods
.....
.....
.....
}

I had changed the implementation for recursive insert to

This code is a part of recursive function.
trie new_child
new_child.insert(word.substr(1));//insert is called again.
//after inserting the nodes then connect it back to the parent
this->children[alphabet]=new_child;
......
.....
//if the map has an entry then
Trie next_child=this->children[letter];
child.insert(word.substr(1));

Now the node correctly points to the children.The problem is that the
values held by the children are lost as I insert multiple words into
the root.
debug messages

inserting pointer 0xbf861970 0//root-- setting the last_node to 0
//create a new node and set the last node to 1
inserting pointer 0xbf861900 1
....
now I insert another 2letter word.
check root 0xbf861970 0
next node 0xbf861900 0//the last node has been changed to zero...

Even though I go to the same memory location,the value of
last_node(private variable) has been changed.
@ James
Yep the the implementation is in c++(atleast parts of it are :)).I am
trying to starting off in C++.

Cheers,
Sam
Aug 10 '08 #9
On Aug 10, 3:30 am, sam.bark...@gmail.com wrote:
class trie {
private:
bool last_node;
map<char, triechildren;
As I said, this is undefined behavior. For starters, change
your code to avoid it (not that I think it's the cause of the
error you're observing).
//methods
....
....
....

}
I had changed the implementation for recursive insert to
This code is a part of recursive function.
trie new_child
new_child.insert(word.substr(1));//insert is called again.
//after inserting the nodes then connect it back to the parent
this->children[alphabet]=new_child;
.....
....
//if the map has an entry then
Trie next_child=this->children[letter];
child.insert(word.substr(1));
Now the node correctly points to the children.The problem is
that the values held by the children are lost as I insert
multiple words into the root.
If you think about it, you'll realize that nodes in a trie have
identity. You're trying to use them as values, which doesn't
work. For starters, you should declare a private copy
constructor and assignment operator in Trie (which really should
be named TrieNode), and not implement them. Do this, and your
code won't compile: you've moved the error from compile time to
runtime.

I said it before: you need dynamic allocation. You're problem
can't be solved otherwise.

(That is, of course, the meta-problem. The actual symptom is
caused by the fact that you replace the node in the trie with a
new node, so any contents of the previous node are lost.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 10 '08 #10
sa*********@gmail.com wrote:
Hi again,
class trie {
private:
bool last_node;
map<char, triechildren;

//methods
....
....
....
}

I had changed the implementation for recursive insert to

This code is a part of recursive function.
trie new_child
new_child.insert(word.substr(1));//insert is called again.
//after inserting the nodes then connect it back to the parent
this->children[alphabet]=new_child;
.....
....
//if the map has an entry then
Trie next_child=this->children[letter];
child.insert(word.substr(1));

Now the node correctly points to the children.The problem is that the
values held by the children are lost as I insert multiple words into
the root.
Well, each time, you create a new Trie object and insert that into the map.
Your code replaces any old object with a fresh one, so the old content is
lost. You could try the alternative with the reference that I mentioned,
which won't do that.

Aug 10 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nick | last post by:
I have the following code: var obj = {a:0, b:1, ...} function f() {...} obj.f = f // This will make a instance method? How to make a Class method for the class of obj? Or what's the...
20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
9
by: ckerns | last post by:
I want to loop thru an array of controls,(39 of them...defaults = 0). If value is null or non-numeric I want to assign the value of "0". rowString = "L411" //conrol name if (isNaN(eval...
4
by: Pieter Linden | last post by:
Hi, Hopefully it's just me missing something obvious (but at 2AM, not much is obvious, I guess...) I have a table of courses that people have taken (StudentID,CourseID) and the user selects a...
5
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and...
2
by: Ashish | last post by:
Hi All, I have a server runnable textbox control. I also have a server side button object but I hook up javascript to it to show a modal dialog. Upon return it places the value in the textbox...
2
by: summer00 | last post by:
Hi everyone, I found that the value of a variable(string type for example) is lost after the aspx page postback. E.G: private void Page_Load(object sender, System.EventArgs e) {
3
by: catweezle2010 | last post by:
Hello NG, I have three files (default.aspx, search.aspx and work.aspx). The way is: login on default (if session is newsession). The loginname I write into as sessionvariable (username). So I...
0
vikas251074
by: vikas251074 | last post by:
I am using Oracle 9i and ASP I have empno, empname, designation, category, dob. Category have two option 'S' or 'O' When I enter empno, empname, designation, category and dob and when I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.