伯克利 CS61A 课堂笔记 10 —— Trees

本系列为加州伯克利大学著名 Python 基础课程 CS61A 的课堂笔记整理,全英文内容,文末附词汇解释。

目录

01 Trees 树

Ⅰ Tree Abstraction

Ⅱ Implementing the Tree Abstraction

02 Tree Processing 建树过程

Ⅰ Fibonacci tree

Ⅱ Tree Processing uses recursion

Ⅲ Creating Trees

03 Example: Printing Trees

04 Example: Summing Paths

05 Example: Counting Paths

附:词汇解释


01 Trees 树

Ⅰ Tree Abstraction

Recursive description (wooden trees):

        A tree has a root label and a list of branches.

        Each branch is a tree.

        A tree with zero branches is called a leaf.

Relative description (family trees):

        Each location in a tree is called a node.

        Each node has a label that can be any value.

        One node can be the parent/child of another.

Ⅱ Implementing the Tree Abstraction

>>> tree(3, [tree(1), tree(2, [tree(1), tree(1)])])
[3, [1], [2, [1], [1]]]
#Treesdef tree(label, branches=[]):#Verifies the tree definitionfor branch in branches:assert is_tree(branch)return [label] + list(branches)def label(tree):return tree[0]def branches(tree):return tree[1:]def is_leaf(tree):return not branches(tree)def is_tree(tree):#Verifies the tree definitionif type(tree) != list or len(tree) < 1:return falsefor branch in branches(tree):if not is_tree(branch):return falsereturn true
>>> tree(1)
[1]
>>> is_leaf(tree(1))
true>>> t = tree(1, [tree(5, [tree(7)]), tree(6)])
>>> t
[1, [5, [7]], [6]]
>>> label(t)
1
>>> branches(t)
[[5, [7]], [6]]
>>> branches(t)[0]
[5, [7]]
>>> is_tree(branches(t)[0])
true
>>> label(branches(t)[0])
5

02 Tree Processing 建树过程

Ⅰ Fibonacci tree
def fib_tree(n):if n <= 1:return tree(n)else:left, right = fib_tree(n-2), fib_tree(n-1)return tree(label(left)+label(right), [left, right])
>>> fib_tree(0)
[0]
>>> fib_tree(1)
[1]
>>> fib_tree(2)
[1, [0], [1]]
>>> fib_tree(4)
[3, [1, [0], [1]], [2, [1], [1, [0], [1]]]]
>>> label(fib_tree(4))
3
Ⅱ Tree Processing uses recursion

Processing a leaf is often the base of a tree processing function.

The recursive case typically makes a recursive call on each branch, then aggregates the results.

def count_leaves(t):"""Count the leaves of a tree."""if is_leaf(t):return 1else:#寻找分支的叶子return sum([count_leaves(b) for b in branches(t)])
>>> count_leaves(fib_tree(4))
5

>>> count_leaves(fib_tree(10))
89

Implement leaves, which returns a list of the leaf of a tree.

Hint: If you sum a list of lists, you get a list containing the elements of those lists.

>>> sum([[1], [2, 3], [4]], [])
[1, 2, 3, 4]
>>> sum([[1]], [])
[1]
>>> sum([[[1]], [2]], [])
[[1], 2]
def leaves(tree):"""Return a list containing the leaf labels of tree.>>> leaves(fib_tree(4))[0, 1, 1, 0, 1]"""if is_leaf(tree):return [label(tree)]else:#寻找分支的叶子return sum([leaves(b) for b in branches(tree)], [])
Ⅲ Creating Trees

A function that creates a tree from another tree is typically also recursive.

def increment_leaves(t):"""Return a tree like t but with leaf labels incremented."""if is_leaf(t):return tree(label(t) + 1)else:return tree(label(t), [increment_leaves(b) for b in branches(t)])def increment(t):"""Return a tree like t but with all labels incremented."""return tree(label(t) + 1, [increment_leaves(b) for b in branches(t)])

03 Example: Printing Trees

#原始版
def print_tree(t):print(label(t))for b in branches(t):print_tree(b)
>>> print_tree(fib_tree(4))
3
1
0
1
2
1
1
0
1
#升级版
def print_tree(t, indent=0){print(' ' * indent + str(label(t)))for b in branches(t):print_tree(b, indent + 1)
}
>>> print_tree(fib_tree(4))
310121101

04 Example: Summing Paths

def fact(n):"Return n * (n-1) * ... * 1"if n == 0:return 1else:return n * fact(n - 1)def fact_times(n, k):"Return k * n * (n-1) * ... * 1"if n == 0:return kelse:return fact_times(n - 1, k * n)def fact_plus(n):return fact_times(n, 1)
>>> fact_plus(4)
24
from tree import *numbers = tree(3, [tree(4), tree(5, [tree(6)])])haste = tree('h', [tree('a', [tree('s'),tree('t')]),tree('e')])def print_sums(t, so_far):so_far = so_far + label(t)if is_leaf(t):print(so_far)else:for b in branches(t):print_sums(b, so_far)
>>> print_sums(numbers, 0)
7
14
>>> print_sums(haste, '')
has
hat
he

05 Example: Counting Paths

Count paths that have a total label sum.

def count_paths(t, total):"""Return the number of paths from the root to any node in tree tfor which the labels along the path sum to total.>>> t = tree(3, [tree(-1), tree(1, [tree(2, [tree(1)]), tree(3)]), tree(1, [tree(-1)])])>>> count_paths(t, 3)2>>> count_paths(t, 4)2>>> count_paths(t, 5)0>>> count_paths(t, 6)1>>> count_paths(t, 7)2"""if label(t) == total:found = 1else:found = 0return found + sum(count_paths(b, total - label(t)) for b in branches(t))

附:词汇解释

verify 证明、definition 定义、aggregate / ˈæɡrɪɡət / 合计、hint / hɪnt / 提示、increment / ˈɪŋkrəmənt / 增长、indent / ɪnˈdent / 缩进、factorial / fækˈtɔːriəl / 阶乘

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/20296.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STL —— 洛谷字符串(string库)入门题(蓝桥杯题目训练)(一)

目录 一、B2109 统计数字字符个数 - 洛谷 算法代码&#xff1a; 1. 引入库和命名空间 2. 主函数 3. 读取输入 4. 变量初始化 5. 遍历字符串 6. 输出结果 7. 返回值 总结 评测记录&#xff1a; 二、B2110 找第一个只出现一次的字符 - 洛谷 方法一&#xff1a;算法代…

【数据分析】1 认识数据分析

一、课程核心内容结构 1. 课程定位 商业数据分析导论课&#xff1a;旨在为初学者奠定扎实的基础&#xff0c;介绍数据分析的基本概念、方法和应用场景。后续模块&#xff1a;包括职业发展路径、技能要求等深入内容&#xff0c;帮助学习者规划未来的职业道路。目标群体&#x…

【Prometheus】prometheus结合domain_exporter实现域名监控

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全…

【分果果——DP(困难)】

题目 分析 分果果题解参考&#xff0c;下面是补充https://blog.csdn.net/AC__dream/article/details/129431299 关于状态 设f[i][j][k]表示第i个人取到的最后一个糖果编号是j&#xff0c;第i-1个人取到的最后一个糖果编号小于等于k时的最大重量的最小值 关于转移方程 关于 j …

大白话实战Sentinel

Sentinel是SpringCloudAlibaba提供的用来做服务保护的框架,而服务保护的常见手段就是限流和熔断降级。在大型分布式系统里面,由于微服务众多,所以服务之间的稳定性需要做特别关注,Sentinel的核心包就提供了从多个维度去保护服务稳定的策略,而且这些保护策略都可以连接上Se…

解锁机器学习核心算法 | 支持向量机:机器学习中的分类利刃

一、引言 在机器学习的庞大算法体系中&#xff0c;有十种算法被广泛认为是最具代表性和实用性的&#xff0c;它们犹如机器学习领域的 “十大神器”&#xff0c;各自发挥着独特的作用。这十大算法包括线性回归、逻辑回归、决策树、随机森林、K - 近邻算法、K - 平均算法、支持向…

vue脚手架开发打地鼠游戏

游戏设计&#xff1a; 规划游戏的核心功能&#xff0c;如场景、随机出现的地鼠、计分系统、游戏时间限制等。简单设计游戏流程&#xff0c;包括开始界面、游戏进行中、关卡设置&#xff08;如不同关卡地鼠出现数量、游戏时间等&#xff09;、关卡闯关成功|失败、游戏结束闯关成…

Web 后端 请求与响应

一 请求响应 1. 请求&#xff08;Request&#xff09; 客户端向服务器发送的HTTP请求&#xff0c;通常包含以下内容&#xff1a; 请求行&#xff1a;HTTP方法&#xff08;GET/POST等&#xff09;、请求的URL、协议版本。 请求头&#xff08;Headers&#xff09;&#xff1a;…

Linux的基础指令和环境部署,项目部署实战(下)

目录 上一篇&#xff1a;Linxu的基础指令和环境部署&#xff0c;项目部署实战&#xff08;上&#xff09;-CSDN博客 1. 搭建Java部署环境 1.1 apt apt常用命令 列出所有的软件包 更新软件包数据库 安装软件包 移除软件包 1.2 JDK 1.2.1. 更新 1.2.2. 安装openjdk&am…

DeepSeek在linux下的安装部署与应用测试

结合上一篇文章&#xff0c;本篇文章主要讲述在Redhat linux环境下如何部署和使用DeepSeek大模型&#xff0c;主要包括ollama的安装配置、大模型的加载和应用测试。关于Open WebUI在docker的安装部署&#xff0c;Open WebUI官网也提供了完整的docker部署说明&#xff0c;大家可…

罗德与施瓦茨ZNB20,矢量网络分析仪9KHz-20GHz

罗德与施瓦茨ZNB20矢量网络分析仪9KHz-20GHz R&SZNB20矢量网络分析仪 产品型号: ZNB20 产品品牌&#xff1a;罗德与施瓦茨 R&S 产品名称: 矢量网络分析仪 频率范围&#xff1a;9kHz - 20GHz R&S ZNB 矢量网络分析仪 良好的测量速度、动态范围和操作方便性&am…

axios post请求 接收sse[eventsource]数据的

axios 接收sse数据的 axios 接收sse数据的 EventSource什么 基于 HTTP 协议实现&#xff0c;通过与服务器建立一个持续连接&#xff0c;实现了服务器向客户端推送事件数据的功能。在客户端&#xff0c;EventSource 对象通过一个 URL 发起与服务器的连接。连接成功后&#xff0…

Python----数据结构(双向链表:节点,是否为空,长度,遍历,添加,删除,查找,循环链表)

一、双向链表 1.1、概念 双向链表是一种链表数据结构&#xff0c;每个节点除了包含指向下一个节点的指针外&#xff0c;还包含指向前一个节点的指针。这种特性使得在双向链表中&#xff0c;可以从任意一个节点开始&#xff0c;向前或向后遍历链表。 1.2、特点 • 既可以从…

VScode内接入deepseek包过程(本地部署版包会)

目录 1. 首先得有vscode软件 2. 在我们的电脑本地已经部署了ollama&#xff0c;我将以qwen作为实验例子 3. 在vscode上的扩展商店下载continue 4. 下载完成后&#xff0c;依次点击添加模型 5. 在这里可以添加&#xff0c;各种各样的模型&#xff0c;选择我们的ollama 6. 选…

投资组合风险管理

投资组合风险管理 市场风险 信用风险流动性风险风险指标收益率波动率最大回撤 α \alpha α&#xff08;詹森指数&#xff09;&#xff0c; β \beta β卡玛比率月胜率上/下行捕获比夏普比率索提诺比率经风险调整的收益率&#xff08;&#x1d440;2&#xff09;特雷诺比率信息…

Mongodb数据管理

Mongodb数据管理 1.登录数据库&#xff0c;查看默认的库 [rootdb51~]# mongo> show databases; admin 0.000GB config 0.000GB local 0.000GB> use admin switched to db admin > show tables system.version > admin库&#xff1a;admin 是 MongoDB 的管理…

GTP3 大模型

GTP3 大模型 模型架构训练核心思想 GTP3 : OpenAI 在 2020 年 5 月发布 GPT-3&#xff0c;发表 Language Models are Few-Shot Learner理念&#xff1a;Few-Shot 思想 , 用少量样本微调&#xff0c;让模型更准确 参数 : 最大模型 : 1750 亿参数多头 Transformer : 96 层Head…

神经网络实验——MLP

目录 1 目的 2 方法 3 源代码 4 结果 1 目的 ①熟悉 Python 的输入输出流; ②学会使用 matplotlib进行图像可视化; ③掌握神经网络的基本原理&#xff0c;学会使用 sklearn 库中的 MLPClassifier 函数构建基础的多层感知机神经网络分类器; ④学会使用网格查找进行超参数优…

Cursor 无限续杯

最近DeepSeek官网无法访问&#xff0c;导致DeepSeekCLine绑定的API Key也无法使用了。那么&#xff0c;除了DeepSeek&#xff0c;还有没有其他好用的AI编程工具呢&#xff1f;答案当然是Cursor&#xff01;不过&#xff0c;由于各种原因一直没有用上Cursor&#xff0c;也不知道…

Windows本地部署DeepSeek

文章目录 一、准备工作1、准备服务器2、准备APP 二、部署deepseek-r11、脚本部署2、脚本部署 三、ChatBox集成 一、准备工作 1、准备服务器 本案例使用Windows电脑 2、准备APP Download Ollama Download Chatbox 二、部署deepseek-r1 1、脚本部署 双击安装完Ollama,默认…