How close cousins C++? and C#, can be debated, but I'll leave that for
some other time. Since you posted this in comp.lang.c++ you'll get a
C++ answer from me, how to convert this to C# is up to you.
On Jan 11, 10:21 am, "raylopez99" <raylope...@yahoo.com> wrote:
> What's the best way of implementing a multi-node tree in C++? What I'm
> trying to do is traverse a tree of possible chess moves given an intial
> position (at the root of the tree). Since every chess position has
> around 30 moves, it would mean every node of the tree would have 30
> branches (on average), which in turn themselves would average about 30
> branches each.
So, you want different kinds of moves and from each move you want a
list of all possible moves that can be performed after, and this will
of course give you a tree-structure.
> I can think of a variety of ways of implementing this, including a
> series of linked lists all pointing to the same header node at the
> root, but I would like to know if there's a practical 'best' way, since
> the tree will be traversed often, and it must be traversed quickly.
> There will be no additions to the tree besides making it grow bigger
> (longer, as move moves are added in a sequence). Certain branches will
> be pruned, but the tree does not have to be rebalanced after pruning
> (meaning the pruned branches will be simply marked as pruned but can
> stay where they are).
How you store the list of possible moves depend on a number of things,
like the variance of the number of moves, if 30 is max but average is 5
then perhaps you don't want to allocate memory for 30 in each node. If
you plan to have really many moves in the tree you might want to
actually remove pruned moves to make room for adding more, this and the
above would indicate a dynamic structure so a list is not a bad idea.
> Ideally I would like something already found in the .NET Standard
> Collection Classes or Generic Collection classes, which include:
> SortedList (key/value collection) and KeyedCollection, which are kinds
> of Sets/Maps it appears.
As I said you'll get C++ from me, and I'll go for a std::list, which is
a normal double-linked list. You should be able to find one in any
decent collections library.
> BTW, nearly every reference I look shows as the sole example of tree a
> red-black binary tree, which is not that helpful to me, though I
> realize probably as a mathematical matter you can parse any multnode
> tree into a red-black binary tree.
Red-black, balanced and whatnot trees are all good if you store values
with a key and want to be able to retrieve the value quickly given a
specific key. Your problem is not one of those.
Now for my solution:
First declare a class representing a move
class Move {
protected:
std::list<Move> moves; // list of possible moves after this
public:
void addMove(Move& m); // Adds a move
const std::list<Move> getMoves() const;
}
Now, you might want to create subclasses of this move representing each
of the possible moves (or subsections of possible moves), or you might
want to add the details describing a move straight in the Move-class,
either way should be fine as long as you follow Liskov's Substitution
Principle*.
So when you want to traverse the tree of moves you can define a
recursive function like so:
void traversMoves(Move& m) {
std::list<Move>::iterator iter, end;
end = m.getList().end();
iter = m.getList().begin();
for (; iter != end; ++iter) {
foo(*iter);
traverseMoves(*iter);
}
}
This will traverse the move-tree and call the function foo() with each
Move as a parameter, to make it more general you might want to create
functor-objects which you pass as an argument to the
traverseMoves()-function instead of hard-coding the foo()-function.
Hope that this will give you some idea on how to proceed.
*
http://www.objectmentor.com/resources/articles/lsp.pdf
--
Erik Wikström