0

I have a non binary tree with the following structure:
[

NOTE I think the way I have structured structure is not totally accurate/standard since the format in which I receive the data is the following (which should be considered the true data):

Item Sub Item Qty of Sub Item in Item
A B 2
A C 2
A D 1
B E 4
C F 4
C G 1
F E 1
G D 1
G E 1
G H 3
E null null
D null null
H null null

That is, whether A -> B was A = 2B or A = 7B, the 4E would always be referring to B = 4E.

What I want is to define all nodes in the form of base layer components/children; that is, the components that have no children. (They are pre-known in the data due to them having the nulls in the data.)

I want the following output:

Item Count E Count D Count H
A (8 + 8 + 2) = 18 (2 + 1) = 3 1
B 4 0 0
C 4 1 1
G 1 1 1

(The parantheses are there just to explain the calculation... wouldn't be desired in the final table... just the numerical value).

Also, ideally the code would be dynamic, such that its execution would be able to pick up on different orientations of the variables in each iteration if need be.

This question seems somewhat similar, but since I am looking to do addition/multiplication to count the iterations of multiple end points, rather than just the number of them overall and the fact that the same child 'type' (ie variable label) can exist under different parent nodes (D is a component of A directly, and also of G) means I can't follow the exact python code.

How would I go about doing it? Also, would python be the best medium for this?

2
  • You need to post the code you've written, along with an example of how it isn't working properly. Commented Sep 30, 2022 at 2:27
  • I don't know how to begin to write this. I am not familiar with tree data structures. Commented Sep 30, 2022 at 2:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.