public int FindChildNodes()
{
if (RightNode == null && LeftNode == null)
{
return 1;
}
else
{
return RightNode.FindChildNodes() + LeftNode.FindChildNodes();
}
}
Finding child Nodes of a node in a tree
Page 1 of 113 Replies - 242 Views - Last Post: 01 January 2013 - 12:22 PM
#1
Finding child Nodes of a node in a tree
Posted 31 December 2012 - 01:12 PM
Replies To: Finding child Nodes of a node in a tree
#2
Re: Finding child Nodes of a node in a tree
Posted 31 December 2012 - 01:13 PM
#3
Re: Finding child Nodes of a node in a tree
Posted 31 December 2012 - 01:19 PM
public int FindChildNodes()
{
if (RightNode == null)
{
return LeftNode.FindChildNodes() + 1;
}
else if (LeftNode == null)
{
return RightNode.FindChildNodes() + 1;
}
else if (LeftNode == null && RightNode == null)
{
return 1;
}
else
{
return RightNode.FindChildNodes() + LeftNode.FindChildNodes();
}
}
#4
Re: Finding child Nodes of a node in a tree
Posted 31 December 2012 - 01:19 PM
Quote
... what happens when you run it?
#5
Re: Finding child Nodes of a node in a tree
Posted 31 December 2012 - 01:20 PM
By the way- why aren't you testing your code?
#6
Re: Finding child Nodes of a node in a tree
Posted 31 December 2012 - 01:22 PM
#7
Re: Finding child Nodes of a node in a tree
Posted 31 December 2012 - 01:57 PM
And you aren't really finding the child nodes, you are finding the count of the descendant nodes.
#8
Re: Finding child Nodes of a node in a tree
Posted 31 December 2012 - 02:08 PM
#9
Re: Finding child Nodes of a node in a tree
Posted 31 December 2012 - 05:17 PM
#10
Re: Finding child Nodes of a node in a tree
Posted 31 December 2012 - 06:10 PM
public int CountChildren( Node Current )
{
if( Current == null ) return 0;
/* etc */
}
Edit:
Depth-First Yielding of nodes.
Public IEnumerable< INode<T> > YieldChildren< T >(this Current : INode<T> )
{
if( Current == null ) return Enumerable.Empty< T >();
foreach( n in Current.LHS.YieldChildren() )
Yield n;
Yield Current;
foreach( n in Current.RHS.YieldChildren() )
Yield n;
}
Now you can use LINQ over the tree.
MyTree.TopNode.YieldChildren().Count()
This post has been edited by AdamSpeight2008: 31 December 2012 - 11:00 PM
#11
Re: Finding child Nodes of a node in a tree
Posted 01 January 2013 - 12:20 AM
This post has been edited by Momerath: 01 January 2013 - 12:22 AM
#12
Re: Finding child Nodes of a node in a tree
Posted 01 January 2013 - 12:51 AM
class Node
{
public Node LeftNode;
public Node RightNode;
}
class SomeClassThatUsesATree
{
public int CountChildren(Node node)
{
:
}
}
But if you look at it from the more modern object oriented view where classes are have state and behavior, then you can have:
class Node
{
public Node LeftNode;
public Node RightNode;
public int CountChildren()
{
:
}
}
#13
Re: Finding child Nodes of a node in a tree
Posted 01 January 2013 - 02:05 AM
There is also the case where you convert a 'forest' into a single tree and traditionally, right nodes are at the same level so wouldn't be considered child nodes.
This post has been edited by Momerath: 01 January 2013 - 02:28 AM
#14
Re: Finding child Nodes of a node in a tree
Posted 01 January 2013 - 12:22 PM
Momerath, on 01 January 2013 - 08:20 AM, said:
Yep! It would but the example was more of a demonstration of using a parameter.
Skydiver I would use an Interface based Node Design.
|
|

New Topic/Question
Reply



MultiQuote









|