没有合适的资源?快使用搜索试试~ 我知道了~
20151910042-刘鹏-DSA实验11-查找表实验1
需积分: 0 0 下载量 97 浏览量
2022-08-08
19:52:13
上传
评论
收藏 56KB DOCX 举报
温馨提示
试读
15页
四、实验记录与实验结果分析1234567891011121314151617181920212223242526272829303132333435363738
资源详情
资源评论
资源推荐
云南大学数学与统计学院数学系信息与计算科学专业
第 1 页 共 15 页
云南大学数学与统计学院
上机实践报告
课程名称:数据结构与算法实验
年级:2015 级
上机实践成绩:
指导教师:陆正福
姓名:刘鹏
上机实践名称:查找树实验
学号:20151910042
上机实践日期:2017-05-14
上机实践编号:No.11
组号:
上机实践时间:上午 3、4 节
一、实验目的
1. 熟悉与查找树有关的数据结构与算法
2. 熟悉主讲教材 Chapter 11 的代码片段
二、实验内容
1. 与查找树有关的数据结构设计与算法设计
2. 调试主讲教材 Chapter 11 的 Python 程序
三、实验平台
Windows 10 1703 Enterprise 中文版;
Python 3.6.0;
Wing IDE Professional 6.0.5-1 集成开发环境。
四、实验记录与实验结果分析
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 11.6.2 Python Implementation
class Tree:
"""Abstract base class representing a tree structure."""
#------------------- nested Position class -------------------
class Position:
"""An abstraction representing the location of a single element."""
def element(self):
"""Return the element stored at this Position."""
raise NotImplementedError('must be implemented by subclass')
def __eq__(self,other):
"""Return True if other Positon represents the same location."""
raise NotImplementedError('must be implemented by subclass')
def __ne__(self,other):
"""Return True if other does not represent the same location."""
return not (self == other)
#---------- abstract methods that concrete subclass must support ----------
def root(self):
"""Return Position representing the tree's root (or None if empty)."""
raise NotImplementedError('must be implemented by subclass')
def parent(self,p):
云南大学数学与统计学院数学系信息与计算科学专业
第 2 页 共 15 页
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Return Position representing p's parent (or None if p is root)."""
raise NotImplementedError('must be implemented by subclass')
def num_children(self,p):
"""Return the number of children that Position p has."""
raise NotImplementedError('must be implemented by subclass')
def children(self,p):
"""Generate an iteration of Positions representing p's children."""
raise NotImplementedError('must be implemented by subclass')
def __len__(self):
"""Return the total number of elements in the tree."""
raise NotImplementedError('must be implemented by subclass')
#---------- concrete methods implemented in this class ----------
def is_root(self,p):
"""Return True if Position p represents the root of the tree."""
return self.root() == p
def is_leaf(self,p):
"""Return True if Positoin p does not have any children."""
return self.num_children == 0
def is_empty(self):
"""Return True if the tree is empty."""
return len(self) == 0
#---------- new methods in this section ----------
def depth(self,p):
"""Return the number of levels separating Positon p from the root."""
if self.is_root(p):
return 0
else:
return 1 + self.depth(self.parent(p))
def _height1(self): # works, but O(n^2) worst-case time
"""Return the height of the tree."""
return max(self.depth(p) for p in self.positions() if self.is_leaf(p))
def _height2(self,p):
"""Ruturn the height of the subtree rooted at Postion p."""
if self.is_leaf(p):
return 0
else:
return 1 + max(self._height2(c) for c in self.children(p))
def height(self,p=None):
"""Return the height of the subtree rooted ar Position p.
云南大学数学与统计学院数学系信息与计算科学专业
第 3 页 共 15 页
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
If p is None, return the height of the entire tree.
"""
if p is None:
p = self.root()
return self._height2(p) # start _height2 recursion
class BinaryTree(Tree):
"""Abstract base class representint a bianry tree structure."""
#--------------------- additional abstract methods ---------------------
def left(self,p):
"""Return a Position representing p's left child.
Return None if p does not have a left child.
"""
raise NotImplementedError('must be implemented by subclass')
def right(self,p):
"""Return a Position representing p's right child.
Return None if p does not have a right child.
"""
raise NotImplementedError('must be implemented by subclass')
#---------- concrete methods implemented in this class ----------
def sibling(self,p):
"""
Return a Position representing p's sibling
(or None if no sibling).
"""
parent = self.parent(p)
if parent is None: # p must be the root
return None # root has no sibling
else:
if p == self.left(parent):
return self.right(parent) # possibly None
else:
return self.left(parent) # possibly None
def children(self,p):
"""Generate an iteration of Positions representing p's children."""
if self.left(p) is not None:
yield self.left(p)
if self.right(p) is not None:
yield self.right(p)
class LinkedBinaryTree(BinaryTree):
"""Linked representation of a binary tree structure."""
剩余14页未读,继续阅读
陌陌的日记
- 粉丝: 11
- 资源: 319
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Picasso_v3.1 2.ipa
- chromedriver-mac-arm64.zip
- 蓝zapro.apk
- chromedriver-linux64.zip
- UCAS研一深度学习实验-MNIST手写数字识别python源码+详细注释(高分项目)
- 基于Python和PyTorch框架完成的一个手写数字识别实验源码(带MINIST手写数字数据集)+详细注释(高分项目)
- 基于Matlab在MNIST数据集上利用CNN完成手写体数字识别任务,并实现单层CNN反向传播算法+源代码+文档说明(高分项目)
- NVIDIA驱动、CUDA和Pytorch及其依赖
- 基于SVM多特征融合的微表情识别python源码+项目说明+详细注释(高分课程设计)
- html动态爱心代码一(附源码)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0