数据结构-树-学习笔记
来源:互联网 发布:简述单片机的发展趋势 编辑:程序博客网 时间:2024/05/29 14:31
http://interactivepython.org/courselib/static/pythonds/index.html
Problem Solving with Algorithms and Data Structures
树
- Examples of Trees
在计算机科学中有许多应用:
操作系统,数据库,计算机网络
文件系统
网页
- Vocabulary and Definitions
元素:
root,branches,leaves
特性:
hierarchical
在每个节点问一个问题,选择合适的答案的路径继续向下走
可以移动任意一个子树,不影响低层的结构
一个节点的children和另一个节点的children是独立的
因此在改变子节点的时候不会影响其他子节点
每个叶节点是唯一的
因此从根到叶节点有唯一的一条路径
概念:
node: name->key, additional information->payload
edge: 节点间的关系,一个节点只有一个incoming edge,有多个outgoing edge
root: 树里唯一一个没有incoming edge的节点
path,children,parent,sibling,subtree
leaf node:没有children的node
level: 从root到节点的path中edge的个数
height: 这个树里最大的level
binary tree: 树里的每个节点都最多只有两个children
定义:
Definition One: A tree consists of a set of nodes and a set of edges that connect pairs of nodes.
Definition Two: A tree is either empty or consists of a root and zero or more subtrees, each of which is also a tree. The root of each subtree is connected to the root of the parent tree by an edge.
- List of Lists Representation
a tree represented by a list of lists
creating a simple tree using a list
myTree = ['a',['b',['d',[],[]],['e',[],[]]],['c',['f',[],[]],[]]]print(myTree)print('left subtree =',myTree[1])print(myTree[1][1])print(myTree[1][1][1])
每个list的第一个元素是root,第二个元素是left subtree,第三个元素是right subtree
叶节点有一个root和两个空的list
def BinaryTree(r): return [r, [], []]#to insert a left childdef insertLeft(root,newBranch): t = root.pop(1) #1st level left subtree if len(t) > 1: #if null, len is 0 root.insert(1,[newBranch,t,[]]) #insert newBranch in left position else: root.insert(1,[newBranch, [], []]) return rootr = BinaryTree(3)print(r) #[3, [], []]print(len(r.pop(1))) #0insertLeft(r,4)print(r) #[3, [4, [], []]]insertLeft(r,5)print(r) #[3, [5, [4, [], []], []]]root = rnewBranch = 6t = root.pop(1)print(t) #[5, [4, [], []], []]print(len(t)) #3root.insert(1,[newBranch,t,[]])print(root) #[3, [6, [5, [4, [], []], []], []]]t = root.pop(1)print(t) #[6, [5, [4, [], []], []], []]print(len(t)) #3
def BinaryTree(r): return [r, [], []]#to insert a left childdef insertLeft(root,newBranch): t = root.pop(1) #1st level left subtree if len(t) > 1: #if null, len is 0 root.insert(1,[newBranch,t,[]]) #insert newBranch in left position else: root.insert(1,[newBranch, [], []]) return rootr = BinaryTree(3)insertLeft(r,4)insertLeft(r,5)def insertRight(root,newBranch): t = root.pop(2) if len(t)>1: root.insert(2,[newBranch,[],t]) else: root.insert(2,[newBranch,[],[]]) return rootinsertRight(r,6)print(r)insertRight(r,7)print(r)def getLeftChild(root): return root[1]def getRightChild(root): return root[2]print(getLeftChild(r))print(getRightChild(r))print(getRightChild(getLeftChild(r)))def getRootVal(root): return root[0]def setRootVal(root,newVal): root[0] = newVal return root[0]print(getRootVal(r))setRootVal(r,10)print(getRootVal(r))
0 0
- 数据结构-树-学习笔记
- 数据结构学习笔记 --- 树
- 数据结构学习笔记 --- 树
- 数据结构学习笔记--树
- 数据结构-stack-学习笔记
- 《Redis源码学习笔记》数据结构-字典
- 数据结构学习笔记--Huffman树
- 数据结构学习笔记--AVL树
- 数据结构学习笔记:二叉树
- 数据结构学习笔记 --- 二叉树
- 数据结构学习笔记 --- 树(习题)
- 数据结构学习笔记:二叉树
- 【学习笔记----数据结构09-树】
- 数据结构【树】 学习笔记随笔
- 数据结构学习笔记 --- 二叉树
- 数据结构学习笔记 --- 树(习题)
- 数据结构学习笔记:二叉树
- 数据结构学习笔记(七)--图
- Qt数据库(sqlite) — 总结
- 4.17:在4.2的基础上,增加两个判断复数是否相等的函数,一个为成员函数,一个为友元函数
- 深入了解vray材质的属性
- iOS 8 Auto Layout界面自动布局系列5-自身内容尺寸约束、修改约束、布局动画
- 移动web开发笔记(1)
- 数据结构-树-学习笔记
- 软考——计算机组成原理
- Xcode6之后增加pch文件并且常用设置属性
- 函数调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。
- golang 数组长度与定义长度(容量)
- PHP中的cURL的简单应用
- node Express安装和使用
- STM32中GPIO_ReadOutputDataBit及GPIO_ReadInputDataBit区别;上拉输入_IPU,下拉输入_IPD,模拟输入_AIN ;开漏输出_Out_OD, 推挽输出_O
- 【FastDev4Android框架开发】AndroidAnnnotations注入框架使用之最佳实践之Adapters和lists(十六)