0
struct avail
{
    int value;
    avail **child;
};
avail *n = new avail;
n->child = new avail*[25];
for (int i = 0; i < 25; i++)
        n->child[i] = new avail;

This is my solution to generating dynamictrees.But I need to specify the no at the start(25). But for further code I want this to be done dynamically something along the lines of

push(avail(n->child[newindex])) 

Or

n->child[29]=new avail;

I want to add nodes on a need basis and create proper hierarchy.I would have used stacks for this but I want parent child relation between the nodes. I want to avoid using vectors to complicate the code.

Jon Bovi
  • 53
  • 9
  • 2
    Why does `std::vector` complicate the code? It makes it much simpler. You are also not initializing your leaf pointers correctly. I don't follow with the later parts. What is `masks`? –  Nov 29 '18 at 20:04
  • `new avail*[25];` that compiles? – Borgleader Nov 29 '18 at 20:05
  • @Borgleader Yes, why shouldn't it? –  Nov 29 '18 at 20:07
  • Can we agree that it probably doesn't do what the asker has in mind? – user4581301 Nov 29 '18 at 20:14
  • Its meant to be child not masks edited. Vector will complicate it because I want a hierarchy like trees with parent child relation. Vectors as I believe will store the pointers in an array and it will imply serially obtaining the pointers. Btw yes new avail*[25] does work but it only generates 25 while i need to assign values dynamically – Jon Bovi Nov 29 '18 at 20:18
  • @JonBovi I don't understand what you mean. The child-parent relationship is given by the fact that the childs are located in the parent's `childs` vector. You can access an `std::vector`'s elements in whatever order you want. Fundamentally it is the same thing as a dynamic array with size tracking plus some additional utility methods. –  Nov 29 '18 at 20:20
  • @eukaryota oh, my bad. brain fart. – Borgleader Nov 29 '18 at 22:58
  • @JonBovi *I want to avoid using vectors to complicate the code.* -- [Huh?](http://coliru.stacked-crooked.com/a/5d3c2de65a5171c1). Your code using pointers is more complicated than using vectors. Why do you think that pointer /heap management is simple, while calling simple member functions (using vector) is difficult? It is actually the opposite -- pointer management is more difficult and complicated than vectors. – PaulMcKenzie Nov 29 '18 at 23:30
  • @PaulMcKenzie vectors can be apparently passed as parameter. I need to do more reasearch but this solution looks promising – Jon Bovi Nov 29 '18 at 23:41
  • @JonBovi Yes of course, you can pass any type by reference and any type with copy/move constructor by value. I sincerely hope you have not been limiting function parameters to primitive and pointer types so far. If you have please consider reading a good introductory book on C++: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list –  Nov 30 '18 at 07:27

0 Answers0