Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8160571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:16:58+00:00 2026-06-06T18:16:58+00:00

I have made a tree structure using c and used it for storing some

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:17:00+00:00Added an answer on June 6, 2026 at 6:17 pm

    You need to create a function that recursively frees the structure, something 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 then just call that from within your dealloc method:

    - (void)dealloc
    {
        free_nodes(_root_node);
        [super dealloc];
    }
    

    Other comments:

    • You’ll want a link back to the mainline of a variation, which is equivalent to the goBack pointer, 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.
    • I would rename goBack to prev, nextLink to next and variationLink to variation, but that’s up to you really.
    • You need to store the move using an internal format, not as an 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:

    typedef struct node
    {
        Move move;    // Holds the move (this can be done using a 32-bit unsigned integer).
        struct node *prev;
        struct node *next;
        struct node *variation;
        struct node *mainline;
        NSString *comment;
    } Node;
    

    Here the mainline link points to the previous variation, which is NULL if 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):

    enter image description here

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a multiplayer game using the GameKit Framework where 2 iPhones/iPods can
I have a large Quad Tree structure, made from generic lists ( List<T> )
I have made an application (for my self) for feeds reading, using SyndicationFeed ,
I have made a game which is nearly identical to Popcap's Atomica. ( http://www.popcap.com/gamepopup.php?theGame=atomica
I have made an application using Java/Hibernate/MySQL, but whenever I try running it, it
i have made an binary tree, and im trying to make an postorder traversal.
I have made some custom attached properties that enable me to create a pop
I'm writing a program that builds up a tree structure made up of classes
I am using Delphi 2009. I have a very simple data structure, with 2
I am having a tree data structure that can have different types of nodes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.