LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2017, 06:11 PM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Rep: Reputation: 37
getChildren Binary Tree Python


Hello,

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:
Code:
T = [[0, 1, 0, 0, 0],
    [0, 0, 0, 0, 1],
    [0, 0, 1, 0, 0],
    [1, 0, 0, 0, 0],
    [0, 0, 0, 1, 0]]
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
 
Old 07-26-2017, 02:00 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Could you please explain this T array, how does it represent a binary tree?
 
Old 07-26-2017, 02:56 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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...).

Here is a chapter which describes the basic problem.

Here is a stackexchange discussion of related problems (getting all descendents, i.e. children involves most of these ideas).

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.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the difference strict binary tree nad extended binary tree . tushar_pandey Programming 1 07-18-2012 11:30 AM
binary tree hollyj Programming 9 12-17-2011 03:29 PM
Binary search tree in C Zeno McDohl Programming 3 01-27-2008 05:07 PM
Binary Search Tree in Python remissed Programming 3 05-07-2006 11:28 PM
Binary tree in C spank Programming 20 04-25-2006 10:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration