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?