数据结构和算法
贾公子
talk is cheap,show me the code
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
二叉树的两种遍历方式
# 二叉树的遍历可分为两种 ''' 1.广度优先遍历 分层把元素放到 队列中,进行遍历 2.深度优先遍历 前序,中序,后序遍历,使用堆栈和递归的方式 ''' # 广度优先遍历一颗二叉树 def bfsTree(self): if sele.root =None: return None queue =[] queue.append(self.root) while queue: node=queue.pop(0) print(node.element,end =',') if.原创 2021-07-14 18:07:02 · 590 阅读 · 0 评论 -
二叉树的基础知识
二叉树转载 2021-07-14 17:10:33 · 174 阅读 · 0 评论 -
贪婪算法的集合覆盖问题
#贪婪算法 以寻找局部最优解来找全局最优解,是一种近似算法,基于集合 # 广播台分别覆盖的州 stations = {} stations["kone"] = set(["id", "nv", "ut"]) stations["ktwo"] = set(["wa", "id", "mt"]) stations["kthree"] = set(["or", "nv", "ca"]) stations["kfour"] = set(["nv", "ut"]) stations["kfive"] = set([原创 2021-07-14 16:29:40 · 303 阅读 · 0 评论 -
迪克斯特拉算法python有向无环图
#迪克斯特拉算法 #处理有向无环图,在加权途中寻找最短路径,不能将狄克斯特拉算法用于包含负权边的图 #查询消费最小的节点 def find_lowst_node(costs,process): lowest_cost = float('inf') lowest_cost_node = None for node in costs: cost = costs[node] if cost<lowest_cost and node not in process: lowest_co原创 2021-07-14 15:55:42 · 529 阅读 · 0 评论 -
广度优先搜索bfs
# 广度优先搜索 bfs #图的查找方法,可回答两种问题1.从A出发有前往B的路径吗?2.从A前往B的最短路径是哪条? # 寻找朋友中以m结尾的人 from collections import deque # who is the person def whoperson(name): return name[-1] ='m' # bfs # deque是一个双向链表,提供了两端都可以操作的序列 def bfs(graph,name): search_queue =deque() s原创 2021-07-14 15:33:58 · 166 阅读 · 0 评论 -
二分查找,选择排序,快速排序,归并排序,冒泡排序
# 二分查找 平均时间复杂度logn def bianryfind(list,item): low =0; high= len(list)-1 while low <= high: mid = int((low+high)/2) guess= list[mid] if guess == item: return guess else if guess>item: high = mid-1 else: low =mid+1 return None原创 2021-07-14 14:45:03 · 172 阅读 · 0 评论 -
基本排序算法的python 实现
(1)冒泡排序 def bubble_sort(arr): length = len(arr) for index in range(length): for j in range(length-index): if arr[j-1]>arr[j]: #交换位置 ar...原创 2018-06-11 13:58:14 · 253 阅读 · 0 评论 -
数据结构--树
(1)树的简介 树是一种非线性数据结构,是由n(n>=0)的结点组成的有限集合。 当n==0时,为空树 当n>0,树包含根节点和子树,根节点只有后继没有前驱。 ...原创 2018-07-27 10:06:51 · 227 阅读 · 0 评论
分享