Trees show up in:
1. Efficiently searching data (e.g. Autocomplete)
2. Artificial Intelligence (Game Trees/ the minimax algorithm)
3. Machine Learning (Decision Tress for categorizing information)
4. Computational Biology (suffix trees for finding a DNA sequence in a larger genome, phylogenetic trees for representing the relationship between different species)
5. Programming languages (syntax and expression trees)
6. Operating systems (File struture)
[3, [1], [2, [1], [1]]]------ tree(3, [tree(1), tree(2, [tree(1), tree(1)])])
# Constructor# # takes in a label and a list of trees or branches, returns a tree with that label and those branches, label on a tree will give you a label, braches will give you a list of branches, is_tree checks of the tree, is_leaf checks of the leaf. #
def tree(label, branches=[]):
for branch in branches:
assert is_tree(branch)
return [label] + list(branches)
# Selectors#
def label(tree):
return tree[0]
def branches(tree):
return tree[1:]
def is_tree(tree):
if type(tree) != list or len(tree)<1:
return False
for branch in branches(tree):
if not is_tree(branch):
return False
return True
def is_leaf(tree):
return not branches(tree) # the list of branches is empty then it's a leaf and if the list of branches has sth then this will be false then it's not a leaf #
Tree processing uses recursion
1, the base case is the smallest version of the problem, if its a leaf
2, the recursive call happens on smaller subproblems, which tend to be branches
3, we use the recursive calls with some type of aggregation afterwards to get our final solution
Count nodes in a tree (recursively)
# total number of nodes in this tree are equal to all of the nodes in each branches +1 on the top#
def count_nodes(t):
if is_leaf(t):
return 1
total = 0
for b in branches(t): # getting all the branches and returning a list of all the branches#
total += count_nodes(b) # count_nodes returns how many nodes as branch has#
return total + 1
# lst = [count_nodes(b) for b in branches(t)]
return sum(lst, 1)#
Collect the leaves
def collect_leaves(t):
if is_leaf(t):
return [label(t)]
lst=[]
for b in branches(t):
lst += collect_leaves(b)
return lst
#lst = [collect_leaves(b) for b in branches(t)]
return sum(lst,[])#
def print_tree(t, indent=0):
if is_leaf(t):
print(' '*indent, label(t))
else:
print(' '*indent, label(t))
for b in branches(t):
print_tree(b, indent + 1)
def print_calls(name, f):
def new_f(t):
print('Name:', name)
print('Inputted Tree:')
print_tree(t)
input()
ret=f(t)
print('Returned:', ret)
return ret
return new_f
collect_leaves = print_calls('collect_leaves', collect_leaves)
def square_tree(t):
if is_leaf(t):
return tree(label(t)**2)
lst=[]
for b in branches(t):
lst += [square_tree(b)]
return tree(label(t)**2, lst)
def fib_tree(n):
if n<=1:
return tree(n)
left = fib_tree(n-2)
right = fib_tree(n-1)
return tree(label(left) + label(right), [left, right])
sum用法
sum([['A'],['B'],['C']], []) ---- ['A', 'B', 'C']
sum([1,2,3,4]) ---- 10
sum([1,2,3,4],10) ------20
sum( [ [1],[2],[3],[4] ]) ----- error 0 + [1] + [2] + [3] + [4]
sum( [ [1],[2],[3],[4]],[] ) ------ [1, 2, 3, 4] = ( [ ]+ [1] + [2] + [3] + [4]) # combine into one list #
sum( ['a', 'b', 'c'], '' ) ---- errir you can't sum over strings
use join to concatenate strings
' ' . join (['a','b','c','d']) ---- 'abcd'
sum(1,2) --- error, cannot iterate 1 (should use add instead)
from operator import add
add(1,2)---3
max and min iterable 用法
min ( [-4,-2,-1,0,1,2,3,4], key= lambda x: abs(x) ) ---- 0
min ( [-2,-1, 0, 1, 2], key= lambda x: abs(x) ) or min ( [-2,-1, 0, 1, 2], key= abs) ---- 0
f = lambda x: abs(x)
f(-2) ----2
bool([1]), bool('a'), bool(1) ----true
all([1,2,3,4])----true = [true,true,true,true]
all([1,2,0,4])-----False