Finding all paths from root to one specific leaf
I am trying to print all possible paths to get from the root a specific leaf. Each path needs to be represented by a list of "True" - if I went the positive option and "False" - if i went the negative way. The leaves of the tree are illnesses and each other node is a symptom. I am trying to do it by running with one list which holds the path and a second list which holds all possible paths if there is more than one. I keep getting an error :"AttributeError: 'NoneType' object has no attribute 'data'"
my code is divided into a main function and a helper function. Diagnoser is the class im working in.
def paths_to_illness(self, illness):
temp_lst =
final_lst =
self.path_helper(illness, temp_lst, final_lst)
return final_lst
def path_helper(self, illness, path_lst, total_paths_lst):
if illness == self.root.data and (not self.root.positive_child or
not self.root.negative_child):
total_paths_lst.append(path_lst)
path_lst.pop()
return
if not self.root.negative_child or not self.root.positive_child:
path_lst.pop()
if self.root.negative_child:
path_lst.append(False)
neg = Diagnoser(self.root.negative_child)
neg.path_helper(illness, path_lst, total_paths_lst)
else:
path_lst.append(True)
pos = Diagnoser(self.root.positive_child)
pos.path_helper(illness, path_lst, total_paths_lst)
thanks for the help.
python-3.x recursion tree
add a comment |
I am trying to print all possible paths to get from the root a specific leaf. Each path needs to be represented by a list of "True" - if I went the positive option and "False" - if i went the negative way. The leaves of the tree are illnesses and each other node is a symptom. I am trying to do it by running with one list which holds the path and a second list which holds all possible paths if there is more than one. I keep getting an error :"AttributeError: 'NoneType' object has no attribute 'data'"
my code is divided into a main function and a helper function. Diagnoser is the class im working in.
def paths_to_illness(self, illness):
temp_lst =
final_lst =
self.path_helper(illness, temp_lst, final_lst)
return final_lst
def path_helper(self, illness, path_lst, total_paths_lst):
if illness == self.root.data and (not self.root.positive_child or
not self.root.negative_child):
total_paths_lst.append(path_lst)
path_lst.pop()
return
if not self.root.negative_child or not self.root.positive_child:
path_lst.pop()
if self.root.negative_child:
path_lst.append(False)
neg = Diagnoser(self.root.negative_child)
neg.path_helper(illness, path_lst, total_paths_lst)
else:
path_lst.append(True)
pos = Diagnoser(self.root.positive_child)
pos.path_helper(illness, path_lst, total_paths_lst)
thanks for the help.
python-3.x recursion tree
Can you add all theDiagnoser
class? does he hasroot
attribute? who init it?
– dWinder
Dec 31 '18 at 15:34
add a comment |
I am trying to print all possible paths to get from the root a specific leaf. Each path needs to be represented by a list of "True" - if I went the positive option and "False" - if i went the negative way. The leaves of the tree are illnesses and each other node is a symptom. I am trying to do it by running with one list which holds the path and a second list which holds all possible paths if there is more than one. I keep getting an error :"AttributeError: 'NoneType' object has no attribute 'data'"
my code is divided into a main function and a helper function. Diagnoser is the class im working in.
def paths_to_illness(self, illness):
temp_lst =
final_lst =
self.path_helper(illness, temp_lst, final_lst)
return final_lst
def path_helper(self, illness, path_lst, total_paths_lst):
if illness == self.root.data and (not self.root.positive_child or
not self.root.negative_child):
total_paths_lst.append(path_lst)
path_lst.pop()
return
if not self.root.negative_child or not self.root.positive_child:
path_lst.pop()
if self.root.negative_child:
path_lst.append(False)
neg = Diagnoser(self.root.negative_child)
neg.path_helper(illness, path_lst, total_paths_lst)
else:
path_lst.append(True)
pos = Diagnoser(self.root.positive_child)
pos.path_helper(illness, path_lst, total_paths_lst)
thanks for the help.
python-3.x recursion tree
I am trying to print all possible paths to get from the root a specific leaf. Each path needs to be represented by a list of "True" - if I went the positive option and "False" - if i went the negative way. The leaves of the tree are illnesses and each other node is a symptom. I am trying to do it by running with one list which holds the path and a second list which holds all possible paths if there is more than one. I keep getting an error :"AttributeError: 'NoneType' object has no attribute 'data'"
my code is divided into a main function and a helper function. Diagnoser is the class im working in.
def paths_to_illness(self, illness):
temp_lst =
final_lst =
self.path_helper(illness, temp_lst, final_lst)
return final_lst
def path_helper(self, illness, path_lst, total_paths_lst):
if illness == self.root.data and (not self.root.positive_child or
not self.root.negative_child):
total_paths_lst.append(path_lst)
path_lst.pop()
return
if not self.root.negative_child or not self.root.positive_child:
path_lst.pop()
if self.root.negative_child:
path_lst.append(False)
neg = Diagnoser(self.root.negative_child)
neg.path_helper(illness, path_lst, total_paths_lst)
else:
path_lst.append(True)
pos = Diagnoser(self.root.positive_child)
pos.path_helper(illness, path_lst, total_paths_lst)
thanks for the help.
python-3.x recursion tree
python-3.x recursion tree
asked Dec 31 '18 at 15:05
Shulik KachkoShulik Kachko
11
11
Can you add all theDiagnoser
class? does he hasroot
attribute? who init it?
– dWinder
Dec 31 '18 at 15:34
add a comment |
Can you add all theDiagnoser
class? does he hasroot
attribute? who init it?
– dWinder
Dec 31 '18 at 15:34
Can you add all the
Diagnoser
class? does he has root
attribute? who init it?– dWinder
Dec 31 '18 at 15:34
Can you add all the
Diagnoser
class? does he has root
attribute? who init it?– dWinder
Dec 31 '18 at 15:34
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988842%2ffinding-all-paths-from-root-to-one-specific-leaf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988842%2ffinding-all-paths-from-root-to-one-specific-leaf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you add all the
Diagnoser
class? does he hasroot
attribute? who init it?– dWinder
Dec 31 '18 at 15:34