# Finding all RShape children recursively (Geomerative)

**URL:** <https://discourse.processing.org/t/finding-all-rshape-children-recursively-geomerative/10241>\
**Category:** Libraries\
**Created:** [April 13, 2019, 5:35pm UTC](https://discourse.processing.org/t/finding-all-rshape-children-recursively-geomerative/10241 "2019-04-13T17:35:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdaein](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@cdaein](https://discourse.processing.org/u/cdaein)\
**Post date:** [April 13, 2019, 5:35pm UTC](https://discourse.processing.org/t/finding-all-rshape-children-recursively-geomerative/10241/1 "2019-04-13T17:35:45Z")

</div>

I have an svg file with multiple levels of hierarchy. `<g>` within `<g>`, and different elements such as `<polygon>`, `<polyline>`, etc within each group. My goal is to create a function that will return me _all_ children elements as `RShape`s in a simple single array format.

I think I will need to do this recursively by creating a function:

```auto
RShape[] getAllChildren(RShape shp) { }

```

and check whether `shp` has any children, and if so, run the function recursively until there’s no child left to find:

```auto
int numChildren = shp.countChildren();
if (numChildren != 0) {
  // run getAllChildren() on each child
} else {
  // return RShape
}

```

Then, return each shape one level up, store them all in a single array and return it. It sort of makes sense to me conceptually but I’m having difficulty implementing it.

I know that there are some recursion examples like drawing a tree, but I feel just running functions like `line()` recursively and having to _return_ some data and track them like I’m trying here are somewhat different.

If you have any example or suggestions, I’d appreciate it.

---

<div class="post-metadata">

**Author:** ![cdaein](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@cdaein](https://discourse.processing.org/u/cdaein)\
**Post date:** [April 13, 2019, 7:02pm UTC](https://discourse.processing.org/t/finding-all-rshape-children-recursively-geomerative/10241/2 "2019-04-13T19:02:33Z")

</div>

Tried a few different things and came up with this function:

```auto
RShape[] getAllChildren(RShape shp) {
  ArrayList<RShape> result = new ArrayList<RShape>();
  int numChildren = shp.countChildren();
  if (numChildren != 0) {
    for (int i = 0; i < numChildren; i++) {
      RShape[] children = getAllChildren(shp.children[i]);
      for (int j = 0; j < children.length; j++) {
        result.add(children[j]);
      }
    }
  } else {
    result.add(shp);
  }
  return result.toArray(new RShape[result.size()]);
}

```

I did a few tests and it seems working but if anyone has a better solution, please let me know!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 13, 2019, 7:14pm UTC](https://discourse.processing.org/t/finding-all-rshape-children-recursively-geomerative/10241/3 "2019-04-13T19:14:43Z")

</div>

can you give a link to an example svg? Or upload a file?

You are on a good path. Just go on.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 13, 2019, 7:15pm UTC](https://discourse.processing.org/t/finding-all-rshape-children-recursively-geomerative/10241/4 "2019-04-13T19:15:48Z")

</div>

my reply was late due to my bad internet connection

apologies
