You are here:

C++/data structures



How are you? Thank you very much for taking questions.
I am experimenting with linked lists and trees in C++. I think I know how to declare nodes for trees and also how to connect them with pointers. However, I only know how to do it one node at a time. My program needs to automatically make unknown number new nodes during runtime. How do you make automatic nodes without having to give each node an individual name. Is an array of nodes the way to go? Or is there another automatic naming pattern that Iím unaware of.

 Below is my code:

class MyTree
   int *id, *count;
   MyTree *left, *right;

    MyTree(int a, int b)
      id = new int; count = new int;   *id = a; *count = b;
      left = NULL;
      right = NULL;
   ~MyTree(){delete id; delete count;}

int main()

MyTree first_node(1,1);
MyTree second_node(2,1);
MyTree onehundredth_node(100,1);

How could I create code that doesnít need to know the names and numbers of the nodes beforehand.

My second question concerns FP-trees. Do you know of any implementation tutorials for FP-trees?


Hi, Andres.  Hopefully I can answer your questions here.

Traditionally, linked lists and other similar data structures use dynamically allocated nodes.  By dynamically allocating the nodes, you can have a (theoretically) infinite number of nodes.  I would also suggest that you separate out the concept of the container and the node.

In your example tree code, you have a single class which represents both the container and the node.  While there isn't really anything wrong with this, keeping the two concepts separate allows you to define a nice interface class which you can use to interact with the list or tree.  For example, using your tree, I would recommend something like the following:

   struct myTreeNode
       int id;
       int count;

       myTreeNode* left;
       myTreeNode* right;

   class myTree
       myTreeNode* rootNode;

       myTree() { rootNode = NULL; }
       ~myTree() { // do proper cleanup here }

       addNode(int a, int b)
         myTreeNode* newNode = new myTreeNode;
         newNode->left = newNode->right = NULL;
         newNode->id = a;
         newNode->count = b;

         if (rootNode)
         // do whatever insert logic you need here
         rootNode = newNode;

Then whenever you need to add a node to the tree, you simply call the addNode function on the instance of the class.  In this way, you can call add node anywhere within your code with the appropriate data and it will create and add the node for you.

In looking for some aids for FP trees, I found the following lecture slides dealing with FP trees:

They obviously would be better if there were audio of the lecturer going over the slides, but they do convey the information fairly well.  Additionally, an academic paper dealing with FP trees may be found here:

Hopefully those deal enough with FP trees to help you out.  Please let me know if you have any further questions.


All Answers

Answers by Expert:

Ask Experts


Joseph Moore


I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.


I've been writing C++ code for 12 years, both on my own in my spare time and professionally.


Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2017 All rights reserved.