This Question is Answered

15 "helpful" answers available (3 pts)
9 Replies Last post: May 21, 2008 7:00 AM by cbarber

Tree traversal

May 21, 2008 4:18 AM

Click to view URPradhan's profile BlackBelt URPradhan 167 posts since
Mar 6, 2008
Hi

I have extended the DefaultTreeNode class and added my own customized index to each TreeNode as a field to the derived class.
Now I want to implement a corresponding method to search for the particular tree node based on its above defined unique index.
How to search for that tree node based upon my own index?

What I'm doing is that, first of all I'm trying to get the TreeModel
out of the tree. Then trying to use the TreeModel.walk-nodes with the
proc-type{TreeNode}:bool .
but I'm not able to implement it properly.

Any pointer or other apporoach?
Click to view Duke's profile Curl Duke 179 posts since
Oct 17, 2007
1. Re: Tree traversal May 20, 2008 10:27 PM
There is no post above, so I guess something went wrong. But from an RSS feed, I found your original question.

"Hi

I have extended the DefaultTreeNode class and added my own customized index to each TreeNode as a field to the derived class.
Now I want to implement a corresponding method to search for the particular tree node based on its above defined unique index.
How to search for that tree node based upon my own index?

What I'm doing is that, first of all I'm trying to get the TreeModel out of the tree. Then trying to use the TreeModel.walk-nodes with the proc-type{TreeNode}:bool .
but I'm not able to implement it properly.

Any pointer or other apporoach?"

but I also notice this did show up in another thread, so this duplicate thread can be ignored.
Click to view friedger's profile MVP friedger 108 posts since
Jan 13, 2008
2. Re: Tree traversal May 21, 2008 12:37 AM
in response to: Duke

Here is a simple example of walk-nodes


 
{def model = {TreeModel root = 
                 {DefaultTreeNode node-data = 0,
                     {DefaultTreeNode node-data = 1,
                         {DefaultTreeNode node-data = 2}
                     },
                     {DefaultTreeNode node-data = 3},
                     {DefaultTreeNode node-data = 4}
                 }
             }
 
}
 
{value def node = 
    {model.walk-nodes
        {proc {n:TreeNode}:bool
            {return n.node-data == 3}
        }
 
    }
    {if-non-null n = node then
        node.node-data
     else
        "nothing found"
    }
}
 
 
Click to view URPradhan's profile BlackBelt URPradhan 167 posts since
Mar 6, 2008
3. Re: Tree traversal May 21, 2008 1:18 AM
in response to: friedger
Hi friedger

I was asking about how to make use of MyTreeNode::walk-nodes which has been derived form DefaultTreeNode and added an extra field for custom indexing.
And in the proc-type I want to compare my custom index to a passed index value...like...

{define-class public IndexTreeNode {inherits DefaultTreeNode}
field constant public node-index:int
{constructor public {default node-data:any = null, node-index:int}
set self.node-index = node-index
{construct-super node-data = node-data}
}

{method public {get-node-from-node-index nodeindex:int}:any
||Search for the node with the node-index and return

||First get the TreeModel for this sub tree
let tm:#TreeModel = self.tree-model
{if tm == null then return null}

let tn:#IndexTreeNode = {tm.walk-nodes
{proc {t:IndexTreeNode}:bool
{return t.node-index == nodeindex}
}
}
{return tn}
}
}

The above code is just for sample and I want to compare for my node-index inside the proc-type of TreeModel.walk-nodes.

hope you got my problem.

BTY, how you are able to post the code in a text area with proper alignment. I was trying with Rich Text posting but all my white spaces are truncated.

Click to view friedger's profile MVP friedger 108 posts since
Jan 13, 2008
4. Re: Tree traversal May 21, 2008 2:10 AM
in response to: URPradhan

I am not sure whether I got your problem. What do you want to compare in the walk procedure?

I guess the comparison has nothing to do with the node you call get-node-from-node-index

If you just want to find a node with a given node-index use something like that

 
 
 
 {proc {t:TreeNode}:bool
                                    {return (t asa IndexTreeNode).node-index == nodeindex}
                                }
 
 
 


Altogether it looks like:

 
 
 
 
 
 
 
{define-class public IndexTreeNode {inherits DefaultTreeNode}
  field constant public node-index:int  
  {constructor public {default node-data:any = null, node-index:int, ...:IndexTreeNode}
    set self.node-index = node-index
    {construct-super node-data = node-data, {splice ...}}
  }
 
 
  {method public {get-node-from-node-index nodeindex:int}:#IndexTreeNode
    ||Search for the node with the node-index and return
 
    ||First get the TreeModel for this sub tree
    let tm:#TreeModel = self.tree-model
    {if tm == null then return null}
 
    let tn:#TreeNode = {tm.walk-nodes 
                                {proc {t:TreeNode}:bool
                                    {return (t asa IndexTreeNode).node-index == nodeindex}
                                }
                            }
    {return tn asa #IndexTreeNode}
  }
}
 
{def model = {TreeModel root = 
                 {IndexTreeNode node-data = 0, 10,
                     {IndexTreeNode node-data = 1, 20,
                         {IndexTreeNode node-data = 2, 30}
                     },
                     {IndexTreeNode node-data = 3, 40},
                     {IndexTreeNode node-data = 4, 50}
                 }
             }
 
}
 
{value def node =
    {(model.root asa IndexTreeNode).get-node-from-node-index 30}
    {if-non-null n = node then
        node.node-data
     else
        "nothing found"
    }
}
 
 
 
 
 


Friedger

For indention use the word "code" in curly brackets before and after the code. That even works in "Rich Text"

Click to view URPradhan's profile BlackBelt URPradhan 167 posts since
Mar 6, 2008
5. Re: Tree traversal May 21, 2008 3:05 AM
in response to: friedger
Thank you. Even I had tried to, do that method inside the proc-type.

But I'm totally confused with the exception that I'm getting from begining that .....

 
 
 
NullDereferenceException: Attempt to use <b>null</b> where a non-<b>null</b> value is required at (0x081BE2C4). 
 
 
#0 IndexTreeNode.get-node-from-node-index (0x081be2c3+87) at file:///d:/Sample Apps/My Tree/indextreenode.scurl:??
#1 build-tree-model (0x041bdd45+465) at file:///d:/Sample Apps/My Tree/start.curl:??
#2 [evaluator <b>for</b> <unnamed>] (0x088c011e+34) at file:///d:/Sample Apps/My Tree/start.curl:??
#3 EvalTopLevelEnv.finish-and-run-aux (0x02116375+613)
#4 EvalTopLevelEnv.finish-and-run (0x021160e2+142)
#5 IncrementalEvaluatorFile.evaluate-next (0x029ce875+557)
#6 IncrementalEvaluator.evaluate-next (0x029ce5a7+65)
#7 Applet.load-next-aux (0x029ccd51+737)
#8 Applet.load-next (0x029cc8c4+156)
#9 [http://{proc} inside Applet.load-start|http://{proc} inside Applet.load-start] (0x029cc814+16)
#10 closure_raw_handler (0x0037efda+26)
#11 AlarmQueue.dispatch-alarms (0x02589061+289)
#12 EventQueue.dispatch-events (0x01be02bf+175)
#13 dispatch-events (0x01be01d1+53)
#14 event-loop (?) (0x01be0197+9)
#15 run-applet (0x023c56f8+136)
#16 ProcessRunnable.run-bound-proc (0x01bc834e+1238)
#17 ProcessRunnable.run (0x01bc7e72+10)
#18 curl-thread-really-really-call-run-in-runnable (0x01bc7e4c+18)
#19 curl-thread-really-call-run-in-runnable (0x01bc3d59+233) 
 
 


And the following code is in my start.curl file

 
 
 
let mytreenode:IndexTreeNode = {IndexTreeNode node-data = "My Tree", 0}
{mytreenode.append {IndexTreeNode node-data = "ABC", 1}}
{mytreenode.append {IndexTreeNode node-data = "XYZ", 2}}
 
 
let n:#IndexTreeNode = {mytreenode.get-node-from-node-index 1} || I want to get the node ABC here as its node-index is 1 
 
 
Click to view friedger's profile MVP friedger 108 posts since
Jan 13, 2008
6. Re: Tree traversal May 21, 2008 3:06 AM
in response to: URPradhan

Oh, there are curly brackets missing in

<code>get-node-from-node-index</code>

!

 
 
 
{if tm == null then
        {return null}
}
 
 

Before the if did nothing.


Note that the nodes should be contained in a tree model

Friedger

Click to view URPradhan's profile BlackBelt URPradhan 167 posts since
Mar 6, 2008
7. Re: Tree traversal May 21, 2008 3:40 AM
in response to: friedger

Thank you very much Mr friedger .

You can not believe, that pair of curly brackets for return statement had already griended my brain since morning and I were unable to find that silly mistake.

Thank you again.

Click to view friedger's profile MVP friedger 108 posts since
Jan 13, 2008
8. Re: Tree traversal May 21, 2008 4:39 AM
in response to: URPradhan

Maybe the compiler should have thrown an error when it sees the return without brackets. (With an option to allow return without brackets).

Friedger

Click to view cbarber's profile Curl cbarber 124 posts since
Sep 27, 2007
9. Re: Tree traversal May 21, 2008 7:00 AM
in response to: friedger
The problem is that syntaxes in Curl are first-class objects, so 'return' without curly braces is a reference to the 'CompilerSyntax' object that implements the 'return' syntax. There is not much that users can do with these objects, but you can create an alias using 'def', which can be useful when writing macros that extend existing syntaxes or just to provide shorthand for verbose syntaxes. For example, you can write:

   def uninitialized = uninitialized-value-for-type
 
   let s:String = {uninitialized String}


Obviously, this idiom should be used sparingly since it could create a lot of confusion.

So regarding your case, the only error the compiler would be able to produce would be that the value 'return' is not used. We have sometimes talked about adding a compile-time error or warning for unused values, but have not done so to date.