Binary Tree Node Location and Helper Dictionary
up vote
1
down vote
favorite
Thanks for your help
Background
I have a question about locating nodes in a tree.
I have a simple binary tree. Each node has a piece of data in it. Lets say it looks like this:
a
/
b c
Where a=root
, b=root.left
, c=root.right
The tree is not created manually. Let's say I get a request to add new_data
to node c.
I'm confused about how to know where c is without explicitly writing root.right.data=new_data
.
My first thought is to create some type of helper dictionary with references to the node locations, like:
helper =
'a'= root,
'b'= root.left,
'c'= root.right
So that when I get a request, I can go to the helper and say something to the effect of:
helper.get('c').data=new_data
Questions
Am I in the right ballpark here? Recursively searching an entire tree repeatedly seems a bit much - this helper could be updated occasionally when the tree changes its node structure.
I'm confused about how to actually return my location for each node when I do recursively crawl the tree. How can I create this helper?
python recursion binary-tree binary-search-tree
add a comment |
up vote
1
down vote
favorite
Thanks for your help
Background
I have a question about locating nodes in a tree.
I have a simple binary tree. Each node has a piece of data in it. Lets say it looks like this:
a
/
b c
Where a=root
, b=root.left
, c=root.right
The tree is not created manually. Let's say I get a request to add new_data
to node c.
I'm confused about how to know where c is without explicitly writing root.right.data=new_data
.
My first thought is to create some type of helper dictionary with references to the node locations, like:
helper =
'a'= root,
'b'= root.left,
'c'= root.right
So that when I get a request, I can go to the helper and say something to the effect of:
helper.get('c').data=new_data
Questions
Am I in the right ballpark here? Recursively searching an entire tree repeatedly seems a bit much - this helper could be updated occasionally when the tree changes its node structure.
I'm confused about how to actually return my location for each node when I do recursively crawl the tree. How can I create this helper?
python recursion binary-tree binary-search-tree
1
You might have a look at this answer: stackoverflow.com/questions/2598437/…
– Ivonet
Nov 11 at 19:52
Why you need the tree anyway ? why not just put your data in a dictionary and that's all ?
– Schidu Luca
Nov 11 at 19:55
@SchiduLuca I've simplified the example dramatically. The tree provides critical links between parent and child objects which can act on each other in different ways. The tree provides a very nice structure for this. I don't believe a dictionary would be productive.
– JW2
Nov 11 at 20:11
@JW2Recursively searching an entire tree repeatedly seems a bit much
. If you decided to put your data in such a form then it's ok to traverse recursively. If you think it's too much just don't use the tree at all..
– Schidu Luca
Nov 11 at 20:20
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Thanks for your help
Background
I have a question about locating nodes in a tree.
I have a simple binary tree. Each node has a piece of data in it. Lets say it looks like this:
a
/
b c
Where a=root
, b=root.left
, c=root.right
The tree is not created manually. Let's say I get a request to add new_data
to node c.
I'm confused about how to know where c is without explicitly writing root.right.data=new_data
.
My first thought is to create some type of helper dictionary with references to the node locations, like:
helper =
'a'= root,
'b'= root.left,
'c'= root.right
So that when I get a request, I can go to the helper and say something to the effect of:
helper.get('c').data=new_data
Questions
Am I in the right ballpark here? Recursively searching an entire tree repeatedly seems a bit much - this helper could be updated occasionally when the tree changes its node structure.
I'm confused about how to actually return my location for each node when I do recursively crawl the tree. How can I create this helper?
python recursion binary-tree binary-search-tree
Thanks for your help
Background
I have a question about locating nodes in a tree.
I have a simple binary tree. Each node has a piece of data in it. Lets say it looks like this:
a
/
b c
Where a=root
, b=root.left
, c=root.right
The tree is not created manually. Let's say I get a request to add new_data
to node c.
I'm confused about how to know where c is without explicitly writing root.right.data=new_data
.
My first thought is to create some type of helper dictionary with references to the node locations, like:
helper =
'a'= root,
'b'= root.left,
'c'= root.right
So that when I get a request, I can go to the helper and say something to the effect of:
helper.get('c').data=new_data
Questions
Am I in the right ballpark here? Recursively searching an entire tree repeatedly seems a bit much - this helper could be updated occasionally when the tree changes its node structure.
I'm confused about how to actually return my location for each node when I do recursively crawl the tree. How can I create this helper?
python recursion binary-tree binary-search-tree
python recursion binary-tree binary-search-tree
edited Nov 11 at 20:47
martineau
65.3k987176
65.3k987176
asked Nov 11 at 19:40
JW2
336
336
1
You might have a look at this answer: stackoverflow.com/questions/2598437/…
– Ivonet
Nov 11 at 19:52
Why you need the tree anyway ? why not just put your data in a dictionary and that's all ?
– Schidu Luca
Nov 11 at 19:55
@SchiduLuca I've simplified the example dramatically. The tree provides critical links between parent and child objects which can act on each other in different ways. The tree provides a very nice structure for this. I don't believe a dictionary would be productive.
– JW2
Nov 11 at 20:11
@JW2Recursively searching an entire tree repeatedly seems a bit much
. If you decided to put your data in such a form then it's ok to traverse recursively. If you think it's too much just don't use the tree at all..
– Schidu Luca
Nov 11 at 20:20
add a comment |
1
You might have a look at this answer: stackoverflow.com/questions/2598437/…
– Ivonet
Nov 11 at 19:52
Why you need the tree anyway ? why not just put your data in a dictionary and that's all ?
– Schidu Luca
Nov 11 at 19:55
@SchiduLuca I've simplified the example dramatically. The tree provides critical links between parent and child objects which can act on each other in different ways. The tree provides a very nice structure for this. I don't believe a dictionary would be productive.
– JW2
Nov 11 at 20:11
@JW2Recursively searching an entire tree repeatedly seems a bit much
. If you decided to put your data in such a form then it's ok to traverse recursively. If you think it's too much just don't use the tree at all..
– Schidu Luca
Nov 11 at 20:20
1
1
You might have a look at this answer: stackoverflow.com/questions/2598437/…
– Ivonet
Nov 11 at 19:52
You might have a look at this answer: stackoverflow.com/questions/2598437/…
– Ivonet
Nov 11 at 19:52
Why you need the tree anyway ? why not just put your data in a dictionary and that's all ?
– Schidu Luca
Nov 11 at 19:55
Why you need the tree anyway ? why not just put your data in a dictionary and that's all ?
– Schidu Luca
Nov 11 at 19:55
@SchiduLuca I've simplified the example dramatically. The tree provides critical links between parent and child objects which can act on each other in different ways. The tree provides a very nice structure for this. I don't believe a dictionary would be productive.
– JW2
Nov 11 at 20:11
@SchiduLuca I've simplified the example dramatically. The tree provides critical links between parent and child objects which can act on each other in different ways. The tree provides a very nice structure for this. I don't believe a dictionary would be productive.
– JW2
Nov 11 at 20:11
@JW2
Recursively searching an entire tree repeatedly seems a bit much
. If you decided to put your data in such a form then it's ok to traverse recursively. If you think it's too much just don't use the tree at all..– Schidu Luca
Nov 11 at 20:20
@JW2
Recursively searching an entire tree repeatedly seems a bit much
. If you decided to put your data in such a form then it's ok to traverse recursively. If you think it's too much just don't use the tree at all..– Schidu Luca
Nov 11 at 20:20
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
If you have a reference to node c, you can use it directly, like so c.data=new_data
.
Otherwise, if what you get is e.g. a string, then:
- Is your tree sorted, or can it be sorted? (i.e. is it a BST?) If so, use it.
- If it's not, then is there any other property of the tree/nodes that you can use to prune the search? (i.e. you have some information to limit where to search). If so, use it. If you don't know if you can use it, you should specify whatever properties your data has in your question.
- If the node can be anywhere, then you pretty much do have to search the whole tree (since the node you want can be anywhere). If that's the case though, is there a purpose to the tree structure? Maybe a hash table might be useful instead. (You could index the tree after you get it)
btw. Note that searching the tree should be O(n) in size of the tree, up to you if that's excessive or not.
it is a BST. Each node does have a unique identifier. I think you're right - whether I'm searching a helper dictionary for that keyhelper.get(key)
or I'm traversing the tree I am still doing a search. It's not immediately evident to me how though to return the instance of a node when I search for it.
– JW2
Nov 11 at 20:14
Ah, in that case, you can refer to _find() method in this answer stackoverflow.com/a/28864021/272515 that Ivonet linked (note that it doesn't actually use self except to reference the _find method recursively. So it should be trivial to convert to a function for your purpose)
– Eric
Nov 11 at 21:03
You're right. Thanks for the help! See below
– JW2
Nov 11 at 21:54
add a comment |
up vote
0
down vote
Answer
The answer is to recursively search. My thoughts of helper dict stem from my unfamiliarity w/ BST. I added the following to my nodes to search the tree preorder:
def find_node(self, start, id):
if start:
if start.id == id:
return start
else:
node = self.find_node(start.left, id)
if node:
return node
else:
node = self.find_node(start.right, id)
if node:
return node
else:
return None
This video was super helpful in helping me understand BST. I realized instead of just printing the traversal I could return the actual instance of the class itself when I found the right id
.
add a comment |
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',
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%2f53252490%2fbinary-tree-node-location-and-helper-dictionary%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
If you have a reference to node c, you can use it directly, like so c.data=new_data
.
Otherwise, if what you get is e.g. a string, then:
- Is your tree sorted, or can it be sorted? (i.e. is it a BST?) If so, use it.
- If it's not, then is there any other property of the tree/nodes that you can use to prune the search? (i.e. you have some information to limit where to search). If so, use it. If you don't know if you can use it, you should specify whatever properties your data has in your question.
- If the node can be anywhere, then you pretty much do have to search the whole tree (since the node you want can be anywhere). If that's the case though, is there a purpose to the tree structure? Maybe a hash table might be useful instead. (You could index the tree after you get it)
btw. Note that searching the tree should be O(n) in size of the tree, up to you if that's excessive or not.
it is a BST. Each node does have a unique identifier. I think you're right - whether I'm searching a helper dictionary for that keyhelper.get(key)
or I'm traversing the tree I am still doing a search. It's not immediately evident to me how though to return the instance of a node when I search for it.
– JW2
Nov 11 at 20:14
Ah, in that case, you can refer to _find() method in this answer stackoverflow.com/a/28864021/272515 that Ivonet linked (note that it doesn't actually use self except to reference the _find method recursively. So it should be trivial to convert to a function for your purpose)
– Eric
Nov 11 at 21:03
You're right. Thanks for the help! See below
– JW2
Nov 11 at 21:54
add a comment |
up vote
0
down vote
If you have a reference to node c, you can use it directly, like so c.data=new_data
.
Otherwise, if what you get is e.g. a string, then:
- Is your tree sorted, or can it be sorted? (i.e. is it a BST?) If so, use it.
- If it's not, then is there any other property of the tree/nodes that you can use to prune the search? (i.e. you have some information to limit where to search). If so, use it. If you don't know if you can use it, you should specify whatever properties your data has in your question.
- If the node can be anywhere, then you pretty much do have to search the whole tree (since the node you want can be anywhere). If that's the case though, is there a purpose to the tree structure? Maybe a hash table might be useful instead. (You could index the tree after you get it)
btw. Note that searching the tree should be O(n) in size of the tree, up to you if that's excessive or not.
it is a BST. Each node does have a unique identifier. I think you're right - whether I'm searching a helper dictionary for that keyhelper.get(key)
or I'm traversing the tree I am still doing a search. It's not immediately evident to me how though to return the instance of a node when I search for it.
– JW2
Nov 11 at 20:14
Ah, in that case, you can refer to _find() method in this answer stackoverflow.com/a/28864021/272515 that Ivonet linked (note that it doesn't actually use self except to reference the _find method recursively. So it should be trivial to convert to a function for your purpose)
– Eric
Nov 11 at 21:03
You're right. Thanks for the help! See below
– JW2
Nov 11 at 21:54
add a comment |
up vote
0
down vote
up vote
0
down vote
If you have a reference to node c, you can use it directly, like so c.data=new_data
.
Otherwise, if what you get is e.g. a string, then:
- Is your tree sorted, or can it be sorted? (i.e. is it a BST?) If so, use it.
- If it's not, then is there any other property of the tree/nodes that you can use to prune the search? (i.e. you have some information to limit where to search). If so, use it. If you don't know if you can use it, you should specify whatever properties your data has in your question.
- If the node can be anywhere, then you pretty much do have to search the whole tree (since the node you want can be anywhere). If that's the case though, is there a purpose to the tree structure? Maybe a hash table might be useful instead. (You could index the tree after you get it)
btw. Note that searching the tree should be O(n) in size of the tree, up to you if that's excessive or not.
If you have a reference to node c, you can use it directly, like so c.data=new_data
.
Otherwise, if what you get is e.g. a string, then:
- Is your tree sorted, or can it be sorted? (i.e. is it a BST?) If so, use it.
- If it's not, then is there any other property of the tree/nodes that you can use to prune the search? (i.e. you have some information to limit where to search). If so, use it. If you don't know if you can use it, you should specify whatever properties your data has in your question.
- If the node can be anywhere, then you pretty much do have to search the whole tree (since the node you want can be anywhere). If that's the case though, is there a purpose to the tree structure? Maybe a hash table might be useful instead. (You could index the tree after you get it)
btw. Note that searching the tree should be O(n) in size of the tree, up to you if that's excessive or not.
answered Nov 11 at 19:56
Eric
1,06221124
1,06221124
it is a BST. Each node does have a unique identifier. I think you're right - whether I'm searching a helper dictionary for that keyhelper.get(key)
or I'm traversing the tree I am still doing a search. It's not immediately evident to me how though to return the instance of a node when I search for it.
– JW2
Nov 11 at 20:14
Ah, in that case, you can refer to _find() method in this answer stackoverflow.com/a/28864021/272515 that Ivonet linked (note that it doesn't actually use self except to reference the _find method recursively. So it should be trivial to convert to a function for your purpose)
– Eric
Nov 11 at 21:03
You're right. Thanks for the help! See below
– JW2
Nov 11 at 21:54
add a comment |
it is a BST. Each node does have a unique identifier. I think you're right - whether I'm searching a helper dictionary for that keyhelper.get(key)
or I'm traversing the tree I am still doing a search. It's not immediately evident to me how though to return the instance of a node when I search for it.
– JW2
Nov 11 at 20:14
Ah, in that case, you can refer to _find() method in this answer stackoverflow.com/a/28864021/272515 that Ivonet linked (note that it doesn't actually use self except to reference the _find method recursively. So it should be trivial to convert to a function for your purpose)
– Eric
Nov 11 at 21:03
You're right. Thanks for the help! See below
– JW2
Nov 11 at 21:54
it is a BST. Each node does have a unique identifier. I think you're right - whether I'm searching a helper dictionary for that key
helper.get(key)
or I'm traversing the tree I am still doing a search. It's not immediately evident to me how though to return the instance of a node when I search for it.– JW2
Nov 11 at 20:14
it is a BST. Each node does have a unique identifier. I think you're right - whether I'm searching a helper dictionary for that key
helper.get(key)
or I'm traversing the tree I am still doing a search. It's not immediately evident to me how though to return the instance of a node when I search for it.– JW2
Nov 11 at 20:14
Ah, in that case, you can refer to _find() method in this answer stackoverflow.com/a/28864021/272515 that Ivonet linked (note that it doesn't actually use self except to reference the _find method recursively. So it should be trivial to convert to a function for your purpose)
– Eric
Nov 11 at 21:03
Ah, in that case, you can refer to _find() method in this answer stackoverflow.com/a/28864021/272515 that Ivonet linked (note that it doesn't actually use self except to reference the _find method recursively. So it should be trivial to convert to a function for your purpose)
– Eric
Nov 11 at 21:03
You're right. Thanks for the help! See below
– JW2
Nov 11 at 21:54
You're right. Thanks for the help! See below
– JW2
Nov 11 at 21:54
add a comment |
up vote
0
down vote
Answer
The answer is to recursively search. My thoughts of helper dict stem from my unfamiliarity w/ BST. I added the following to my nodes to search the tree preorder:
def find_node(self, start, id):
if start:
if start.id == id:
return start
else:
node = self.find_node(start.left, id)
if node:
return node
else:
node = self.find_node(start.right, id)
if node:
return node
else:
return None
This video was super helpful in helping me understand BST. I realized instead of just printing the traversal I could return the actual instance of the class itself when I found the right id
.
add a comment |
up vote
0
down vote
Answer
The answer is to recursively search. My thoughts of helper dict stem from my unfamiliarity w/ BST. I added the following to my nodes to search the tree preorder:
def find_node(self, start, id):
if start:
if start.id == id:
return start
else:
node = self.find_node(start.left, id)
if node:
return node
else:
node = self.find_node(start.right, id)
if node:
return node
else:
return None
This video was super helpful in helping me understand BST. I realized instead of just printing the traversal I could return the actual instance of the class itself when I found the right id
.
add a comment |
up vote
0
down vote
up vote
0
down vote
Answer
The answer is to recursively search. My thoughts of helper dict stem from my unfamiliarity w/ BST. I added the following to my nodes to search the tree preorder:
def find_node(self, start, id):
if start:
if start.id == id:
return start
else:
node = self.find_node(start.left, id)
if node:
return node
else:
node = self.find_node(start.right, id)
if node:
return node
else:
return None
This video was super helpful in helping me understand BST. I realized instead of just printing the traversal I could return the actual instance of the class itself when I found the right id
.
Answer
The answer is to recursively search. My thoughts of helper dict stem from my unfamiliarity w/ BST. I added the following to my nodes to search the tree preorder:
def find_node(self, start, id):
if start:
if start.id == id:
return start
else:
node = self.find_node(start.left, id)
if node:
return node
else:
node = self.find_node(start.right, id)
if node:
return node
else:
return None
This video was super helpful in helping me understand BST. I realized instead of just printing the traversal I could return the actual instance of the class itself when I found the right id
.
answered Nov 11 at 21:53
JW2
336
336
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53252490%2fbinary-tree-node-location-and-helper-dictionary%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
1
You might have a look at this answer: stackoverflow.com/questions/2598437/…
– Ivonet
Nov 11 at 19:52
Why you need the tree anyway ? why not just put your data in a dictionary and that's all ?
– Schidu Luca
Nov 11 at 19:55
@SchiduLuca I've simplified the example dramatically. The tree provides critical links between parent and child objects which can act on each other in different ways. The tree provides a very nice structure for this. I don't believe a dictionary would be productive.
– JW2
Nov 11 at 20:11
@JW2
Recursively searching an entire tree repeatedly seems a bit much
. If you decided to put your data in such a form then it's ok to traverse recursively. If you think it's too much just don't use the tree at all..– Schidu Luca
Nov 11 at 20:20