'this' as parameter

like in doubly linked list

Page 1 of 1

6 Replies - 1586 Views - Last Post: 22 May 2006 - 09:17 PM Rate Topic: -----

#1 babasmith  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-April 06

'this' as parameter

Posted 21 May 2006 - 02:58 PM

Hi;

I'm trying to implement some data structure similar to a tree (not sorted!) where each node has a poinert ot its father (its creator) and another 4 pointers to its sons as myNode * children[4].

The problem is that when node a calls the node calss constructor it has to pass a poinetr to itself and I'm not sure how to do it.

Is it myNode::myNode(int val,myNode * pred_) where inside the constructor to write pred=pred_ so to create a son for a to write (inside a) :

child[val]=new myNode(val,this)?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: 'this' as parameter

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: 'this' as parameter

Posted 21 May 2006 - 09:28 PM

I'm not quite sure what you mean, maybe post the node class or structure code to get a better solution.
Also an object cannot point to itself or contain itself, these are the golden rules of programming.
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: 'this' as parameter

Posted 21 May 2006 - 10:54 PM

No you would not refer to it using the 'this' param. And to answer your question depends on which node in the list you are creating.

Is it the first node of the list? A node in the middle, or the last node in the list?

As William_Wilson stated, you really need to post your code, to better answer your question.

This post has been edited by jayman9: 21 May 2006 - 11:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 babasmith  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-April 06

Re: 'this' as parameter

Posted 22 May 2006 - 04:09 AM

Hi;
First, thanks for answering. I'll post the code later since its full with debugging lines so its not quite readable....

What I'm trying to implement is a date stracture similar to a binary tree but with the difference that it's not sorted and each node can have up to 4 sons. in adition I need the ability to traverse it by-level and with much consideration on performance (its for a bio-informatic project).

The tree,or better say its creation and extension, represents the reconstruction of a very large string over an alphabet of size 4 (A,C,G,T which I represent as 0,1,2,3) from its substrings of length k (up to 4^k such substrings). In each step a node tries to extend itself by checking the list of substrings for those that have the same k-1 first letters like the k-1 from that node and up. If it finds one possible extension it creates a new node as its child. If more than one is possible (lets say 4 options-where we know only 1 coresponds to that position and the others to other positions and we dont know who's the correct) then it creates 4 sons and goes on, hopefully that the incorrect paths will reach later a dead end and "die" before reaching the target length.

Since the tree isn't sorted I cant 'ask' the tree to extend itself cause it will take alot of time to find the node to extend- so let the node to extend itself in that position, so when node x1 wants to create node x2 as its child it has to call for the node calss constructos which needs also a poiter to x1 to put in x2 so, x2 can access it predecessors, and this is where I think my bug is.

In my code for class Node there is a member 'Node * pred;' and in the constructor I've 'Node * pred_' in the signature and inside the ctor the assignment 'pred=pred_;'. In adition there is a method 'Node::makeSon(int val,int name)' that when the reconstruction algorithm gets to node x1 and decides to extend that end with (lets say) val=1 it does 'x1->makeSon(1,x2);' (1 is the val for the new node and x2 its name) and then x1 should create a new node called x2 with val=1, to set it as its son and also give it (to x2) a poiter to itself (to x1, its creator) so in 'makeSon' I wrote 'children[val]=new Node(1,x2,this)' so x2 will contain its val (=1), its name (=x2) and a poiter to its creator (x1).

The problem is that after creating the first node (the root) and calling it to create a child, I get that the root pointer and the child pointer have the same address. For all the others that comes after its all okay so besides that the data isnt consistent with the input I get a bug while destructing the tree cause the (real) root doesnt destructs and the faked root get destructed as the child of the real root and after that as the (faked) root.

I hope I clarified what I'm trying to do so if you have any suggestions or if you have something like that already implemented I'll be grateful.

**The main reason why I dont implement it like regular trees (with the needed extensions) is that they all require that the tree class gets to add nodes instead of each node creating and adding its sons,so for a tree with O(4^20,000) nodes, findind the needed node to be extended is very very very expensive (20,000 is a length for a target DNA sequence to be reconstructed).

Thanks alot.
Was This Post Helpful? 0
  • +
  • -

#5 sigmazero13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 10-May 06

Re: 'this' as parameter

Posted 22 May 2006 - 07:58 AM

Well, here's just a rough example.

Let's say here's a VERY rough version of the Tree Node structure:
class TreeNode
{
private:
 TreeNode * children[4];
 TreeNode * parent;
 int value;

public:
 TreeNode(TreeNode * newparent, newvalue); // constructor
 TreeNode * addNode(value);
};

TreeNode::TreeNode(TreeNode * newparent, int newvalue)
{
 parent = newparent; // THIS will set the PARENT pointer to the parameter;
 value = newvalue;
}

Treenode * TreeNode::addNode(int value);
{
  int branch;
  TreeNode * newNode;

  // SET BRANCH TO WHICHEVER CHILD YOU WANT TO USE
  branch = something_or_other; // pseudo-code
  
  newNode = new TreeNode(this, value);// See below
  children[branch] = newNode;

  return newNode;
}



The "newNode = new TreeNode(this);" is the key. The "THIS" parameter will cause the NEW node to be constructed, and a pointer to the CURRENT node will be passed into it; thus, the NEW node's parent pointer will point to the CURRENT node (the CURRENT node being the one running addNode.

I hope this helps, and I hope I'm not misunderstanding the question.
Was This Post Helpful? 0
  • +
  • -

#6 babasmith  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-April 06

Re: 'this' as parameter

Posted 22 May 2006 - 01:47 PM

Well, you got me right and what I did is exactly what you wrote so the problmem isnt about the pointer for sure.

After alot of tests I'm pretty sure (->100%) the problem is within the destructor.

Is there any chance you take a look? Do you have an email I can send the code to?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 sigmazero13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 10-May 06

Re: 'this' as parameter

Posted 22 May 2006 - 09:17 PM

I'll send you a PM with my email address. However, you could post it here (just put it in "code" tags), so that way you can get more than one person looking at it :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1