
python数据结构
Leohfan
这个作者很懒,什么都没留下…
展开
-
Python-树
树节点类 TreeNode作为最简单的树节点,我们只需要3个基本属性name: 当前节点的名字(使用str来保存) parent: 父节点对象(对根节点来说,该值为Null) child: 字节点对象们(使用dict来保存)代码如下:class TreeNode(object): """The basic node of tree structure""" d...转载 2019-01-09 09:09:16 · 385 阅读 · 0 评论 -
python-检索
def sequentialSearch(alist,item): pos = 0 found = False while pos < len(alist) and not found: if alist[pos] == item: found = True else: pos = po...转载 2019-01-09 09:09:04 · 521 阅读 · 0 评论 -
python-排序
def bubbleSort(alist): for passnum in range(len(alist)-1,0,-1): for i in range(passnum): if alist[i]>alist[i+1]: #temp = alist[i] #alis...转载 2019-01-09 09:08:48 · 429 阅读 · 0 评论 -
python-队列
from pythonds.basic.queue import Queueimport randomdef hotPotato(namelist,num): simqueue = Queue() for name in namelist: simqueue.enqueue(name) while simqueue.size()>1: ...转载 2019-01-09 09:07:19 · 275 阅读 · 0 评论 -
python-双端队列
from pythonds.basic.deque import Dequeclass Deque: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def addFront(self, item): self...转载 2019-01-09 09:07:10 · 282 阅读 · 0 评论 -
python-链表(无序链表、有序链表)
class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next ...转载 2019-01-09 09:06:37 · 578 阅读 · 0 评论 -
python-树(树的遍历)
#列表表示树def BinaryTree(r): return [r, [], []]def insertLeft(root,newBranch): t = root.pop(1) if len(t) > 1: root.insert(1,[newBranch,t,[]]) else: root.insert(1,[ne...转载 2019-01-09 09:06:59 · 508 阅读 · 0 评论 -
python-二叉堆
class BinHeap: def __init__(self): self.heapList = [0] self.currentSize = 0 def percUp(self,i): while i // 2 > 0: if self.heapList[i] < self.hea...转载 2019-01-09 09:09:42 · 235 阅读 · 0 评论 -
python-查找树
class BinarySearchTree: def __init__(self): self.root=None self.size=0 def length(self): return self.size def __len__(self): return self.size de...转载 2019-01-09 09:10:09 · 508 阅读 · 0 评论 -
python-图(邻接表)
class Vertex: def __init__(self,key): self.id=key self.connectedTo={} def addNeighbor(self,nbr,weight=0): self.connectedTo[nbr]=weight def __str__(self):...转载 2019-01-09 09:10:32 · 4101 阅读 · 0 评论 -
python-文件I/O与异常处理
open函数Python内置的open()函数打开一个文件,创建一个file对象,相关的辅助方法才可以调用它进行读写。语法为:file object = open(file_name [, access_mode][, buffering])各个参数的细节如下:1、file_name:file_name变量是一个包含了你要访问的文件名称的字符串值。2、access_mode:a...转载 2019-01-09 09:21:17 · 901 阅读 · 0 评论 -
python-类
创建类类(Class): 用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例。 使用class语句来创建一个新类,class之后为类的名称并以冒号结尾,如下实例:class ClassName: '类的帮助信息' #类文档字符串 class_suite #类体下面代码就创建了一个名为Employee的类.clas...转载 2019-01-09 09:06:49 · 242 阅读 · 0 评论 -
python-栈
from pythonds.basic.stack import Stackdef parChecker(symbolString): s=Stack() balanced=True index=0 while index<len(symbolString) and balanced: symbol=symbolString[index]...转载 2019-01-09 09:08:10 · 246 阅读 · 0 评论 -
Python国内外在线参考材料
在线参考材料数据结构与算法(基于 Python 语言)课程(北京大学数学学院) 在线教材:Problem Solving with Algorithms and Data Structures 许多 Python 学习材料,包括算法与数据结构材料 廖雪峰老师的 Python 教程 伯乐在线一些有关 Python 的文章 一位清华研究生有关 Python 数据结构和算法的网页 一个有...转载 2019-01-10 11:40:58 · 781 阅读 · 0 评论