ios - How to release a tree structure in xcode project? -
i have made tree structure using c , used storing moves of chess game. structure is
struct node{ nsstring *comment; nsstring *move; struct node *variationlink; struct node *nextlink; struct node *goback; }; i create node using malloc.when game changed or unloaded want release tree there way in dalloc or have make function reach each node , free it?
you need create function recursively frees structure, like:
void free_nodes(struct node *n) { if (n != null) { free_nodes(n->nextlink); free_nodes(n->variationlink); [n->comment release]; [n->move release]; free(n); } } and call within dealloc method:
- (void)dealloc { free_nodes(_root_node); [super dealloc]; } other comments:
- you'll want link mainline of variation, equivalent
gobackpointer, variations. allow transverse mainline of node, @ moment there no way of performing full transversal of tree. - i rename
gobackprev,nextlinknext,variationlinkvariation, that's really. - you need store move using internal format, not
nsstring. strings should generated during display (in view's draw method). allows use move data rather having parse string again (very expensive) , doing string conversion during display allows change how move string generated based on user preferences (short algebraic notation, long algebraic notation, co-ordinate notation, using piece character fonts rather letters, etc.).
edit after question op: in order allow tree store multiple variations need create doubly-linked list of variations. therefore node part of 2 doubly-linked lists. writing in c++ help, i'll show in c, if that's using:
typedef struct node { move move; // holds move (this can done using 32-bit unsigned integer). struct node *prev; struct node *next; struct node *variation; struct node *mainline; nsstring *comment; } node; here mainline link points previous variation, null if mainline move.
the moves 1.e4 e5 (1...nf6 a4) (1...nc6 b4) 2.nc3 held using tree (if links not shown on node null):

i using approach in chess program developing , it's working well. re-iterate; separate data (this node) presentation (the string containing move text displayed in ui). generated move strings should held in different way altogether; perhaps core text, using custom method.
Comments
Post a Comment