Better ideas or ways to approach?

  • Context: Python 
  • Thread starter Thread starter ChrisVer
  • Start date Start date
  • Tags Tags
    Approach Ideas
Click For Summary
SUMMARY

The discussion focuses on optimizing a Python function for retrieving subtrees from a nested tree structure. The user implemented a recursive function, tree_ref, which takes a tree and a list of indices to return the corresponding subtree. Suggestions for improvement include simplifying the return statement and considering a loop-based approach to enhance performance with larger trees. The current implementation utilizes a depth-checking function, depth, to prevent errors when accessing deeper subtrees than available.

PREREQUISITES
  • Understanding of Python programming, specifically recursion and data structures.
  • Familiarity with tree data structures and their traversal methods.
  • Knowledge of Python error handling and debugging techniques.
  • Experience with performance optimization in algorithms.
NEXT STEPS
  • Research iterative tree traversal techniques in Python.
  • Learn about Python's built-in data structures and their performance implications.
  • Explore optimization strategies for recursive functions in Python.
  • Investigate the use of memoization to enhance recursive function performance.
USEFUL FOR

Python developers, computer science students, and anyone interested in optimizing recursive algorithms for tree data structures.

ChrisVer
Science Advisor
Messages
3,372
Reaction score
465
I was trying to finish the assignments of this link
https://ocw.mit.edu/courses/electri...ce-fall-2010/assignments/MIT6_034F10_lab0.pdf

unfortunately they are not numbered, but I dealt with the Tree reference one. In particular, given a tree TREE and a list of indices, it will return the appropriate subtree. My python code looks like this:
Python:
def depth(x):
    if not isinstance(x,(list,tuple)): return 0
    maxdep=0
    for element in x:
        maxdep = max( maxdep , depth(element) )
    return maxdep+1

def tree_ref( tree, index):
    #Error if you ask for a deeper subtree than available
    if len(index)>depth(tree):
        return "ERROR \t tree_ref(): Your tree is smaller than what you asked"
    #main return
    if len(index)==1: return tree[index[0]]
    for i in range(len(index)):
        return tree_ref(tree[index[i]], index[i+1:])

TREE=(((1, 2), 3), (4, (5, 6)), 7, (8, 9, 10))
print  tree_ref(TREE, (1,1,1))
As far as I've checked this code seems to work...Are there more clear or even optimal ways to do the job? thanks.
 
Technology news on Phys.org
ChrisVer said:
Python:
    for i in range(len(index)):
        return tree_ref(tree[index[i]], index[i+1:])
This structure seems a little odd - I think it's equivalent to
Python:
return tree_ref(tree[index[0]],index[1:])
Also, you've used a recursive approach. I'd probably use a loop-based approach, since I expect it would handle larger trees without falling over.
 

Similar threads

  • · Replies 34 ·
2
Replies
34
Views
5K
Replies
55
Views
7K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K