ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want a getChildren function for an item in a decision tree. This function should return a list of all chirdren of the item. Even for children of children. The binary tree is represented in a tree of 0s and 1s. like the following:
Where is one means that at that index there is a child.
Could anyone please tell me how to write a recursive function for getChildren? My attempt is the following
Code:
def getChildren(T, item, children):
for x in range(0, len(T[item]), 1):
if T[item][x] == 1:
children.append(x)
getChildren(T, T[item][x], children)
return children
# This is a call for getting the children of the 3rd node
getChildren(T, 2, [])
This unfortunately does not seem to work. Any suggestions
What you are looking for is a reachability matrix of the graph (tree).
You can find algorithms for that in various texts and online, although the implementation is non-trivial and will vary greatly depending on the language used (how the graph data are stored, whether the language supports matrices directly or must use arrays, array types, etc...).
Several years ago my son wrote a set of both PHP and C++ classes which begin with tree structured data in a relational database and end with reachability and ordering (adjacency) lists and diagrams extracted from that data, which I still use but do not claim to fuly understand (He is much smarter than I am! ). While I cannot give a working example, I can point you in the right direction - graph theory and specifically, reachability matrix and related concepts.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.