Home > OS >  Get subtrees from a starting encoded tree
Get subtrees from a starting encoded tree

Time:12-13

I have trees coded like this:

A ➜ B ➜ C ➜ -1 ➜ D

A ➜ B ➜ C ➜ -1 ➜ -1 ➜ D

When there is -1, it means that you have to go up one level, so in the first example, B has two children (C and D). In the second example, A has two children (B and D).

My goal is to remove one leaf at a time and get the remaining tree, referring to the first example, there are two leaves (C and D), so what I want to get is:

A ➜ B ➜ D

A ➜ B ➜ C

Referring to the second example, there are always two leaves (C and D), so what I want to get is:

A ➜ B ➜ -1 ➜ D

A ➜ B ➜ C

I've written some intricate code, but it doesn't work for all applications, do you have something in mind, or is there already some library that does this job?

CodePudding user response:

It seems to me that in your notation, a leaf is either a label at the very end of the description, or a label in the context ➜ [X] ➜ -1. In either case, you could remove the leaf:

  • if the label is at the end, remove it and the ➜ which precedes it (if there is one) and then repeatedly remove ➜ -1 if that appears at the end of the description.

  • If the label is in the context ➜ [X] ➜ -1, remove those four tokens.

  • Related