I have made a tree structure using c and used it for storing some moves of a chess game.
my structure is
struct node{
NSString *comment;
NSString *move;
struct node *variationLink;
struct node *nextLink;
struct node *goBack;
};
i create node using malloc.When the game is changed or unloaded i want to release the tree is there any way to do it in dalloc or i have to make a function to reach each node and free it?
You need to create a function that recursively frees the structure, something like:
and then just call that from within your
deallocmethod:Other comments:
goBackpointer, but for variations. This will allow you to transverse back to the mainline of any node, as at the moment there is no way of performing full transversal of your tree.goBacktoprev,nextLinktonextandvariationLinktovariation, but that’s up to you really.NSString. The strings should only be generated during display (in the view’s draw method). This allows you to actually use the move data rather than having to parse the string again (very expensive) and doing the string conversion only during display allows you to change how the move string is generated based on user preferences (short algebraic notation, long algebraic notation, co-ordinate notation, using piece character fonts rather than letters, etc.).Edit after question from OP: In order to allow your tree to store multiple variations you need to create doubly-linked list of variations. Therefore the node will be part of two doubly-linked lists. Writing this in C++ will help, but I’ll show it in C, if that’s what you are using:
Here the
mainlinelink points to the previous variation, which isNULLif this is the mainline move.The moves 1.e4 e5 (1…Nf6 a4) (1…Nc6 b4) 2.Nc3 would be held using a tree like this (if links are not shown on a node then they are
NULL):I am using this approach in a chess program I am developing and it’s working very well. To re-iterate; I separate the data (this node) from the presentation (the string containing the move text which is displayed in the UI). The generated move strings should be held in a different way altogether; perhaps Core Text, but I am using a custom method.