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,

Typically, a tree is defined as nodes.  The node has a left and right pointer as you have.  A class is defined with the tree as the base class - and this class adds the data about the node.  This way, the tree class has definitions for adding to and deleting from the tree and traversing the tree.

The tree class could have a static member which is the root of the tree.  The add function would set the root if the root is NULL.  You really need this because most functions need to use the root as a starting point.

To use any number of nodes, use new to allocate them.  A delete of the tree should traverse all the nodes deleting them.  Use a virtual destructor so that a node which is deleted will use the upper class destructor to all it to delete extra items in the node.

To show all the code for what I write here is a lot of work.  What you have is a good start.  I've described how to make it more reusable.  Remember, delete of a node should traverse the tree deleting child nodes and you can (or should) do this recursively.  This means you can delete a branch or the whole tree depending on where you start.



All Answers

Answers by Expert:

Ask Experts


Bill A


I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.


I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

BS Computer Engineering

©2017 All rights reserved.