Python基础学习-数据结构
2026/6/1 22:47:32 网站建设 项目流程

一、数组(array)

把一组相同类型的数据,按照顺序连续存放在内存中的结构

Python:List

特点:1、数据类型相同 2、元素按顺序排列 3、内存空间连续

索引快,查询、插入、删除慢

# 数组定义 arr = [10, 20, 30, 40, 50] # 访问 print(arr[1]) # 20 # 查找 print(20 in arr) # True # 修改 arr[1] = 99 print(arr) # [10, 99, 30, 40, 50] # 添加 arr.append(100) print(arr) # [10, 99, 30, 40, 50, 100] # 插入 arr.insert(1, 54) # [10, 54, 99, 30, 40, 50, 100] print(arr) # 删除 arr.remove(99) print(arr) # [10, 54, 30, 40, 50, 100] arr.pop(5) print(arr) # [10, 54, 30, 40, 50] # 遍历 for x in arr: print(x) for i in range(len(arr)): print(arr[i])

二、链表(Linked List)

用一个一个“节点”把数据串起来,每个节点除了存数据,还存下一个节点的位置。

[val | next] -> [val | next] -> [val | next] -> [val | null]

特点:插入删除快,查找慢(不能直接通过下标访问),内存不要求连续。

# 链表节点Node class Node: def __init__(self, x): self.data = x # 存放数据 self.next = None # 指向下一个节点 # 链表 class LinkedList: def __init__(self): self.head = None # 尾部添加 def append(self, data): new_node = Node(data) if self.head is None: self.head = new_node return current = self.head while current.next is not None: current = current.next current.next = new_node # 删除指定值 def delete(self, data): current = self.head # 如果删除的是头节点 if current is not None and current.data == data: self.head = current.next return previous = None while current is not None and current.data != data: previous = current current = current.next # 没找到要删除的值 if current is None: print("没有找到要删除的元素") return # 删除节点 previous.next = current.next # 查找元素 def search(self, data): current = self.head while current is not None: if current.data == data: return True current = current.next return False # 打印链表 def display(self): current = self.head while current is not None: print(current.data, end=" -> ") current = current.next print("None") linked_list = LinkedList() linked_list.append(10) linked_list.append(20) linked_list.append(30) linked_list.append(40) linked_list.display() # 10 -> 20 -> 30 -> 40 -> None

三、哈希表(Hash Table)

通过“键 key”快速找到“值 value”的数据结构。

Python:Dictionary

特点:查询、插入、删除快,没有索引。

为什么哈希表查找快?:哈希表内部会通过一个哈希函数,把key转换成一个存储位置。

# 定义 table = {"apple": 5, "banana": 3} # 插入 table["orange"] = 4 print(table) # {'apple': 5, 'banana': 3, 'orange': 4} # 访问 print(table["orange"]) # 4 # 修改 table["orange"] = 8 print(table) # {'apple': 5, 'banana': 3, 'orange': 8} # 删除 del table["banana"] print(table) # {'apple': 5, 'orange': 8} # 查找 print("apple" in table) # True # 遍历 for key, value in table.items(): print(key, value)

四、队列(Queue)

先入先出:FIFO(First In First Out)

python:1、List:append();pop(0)(尾部加,头部出)

2、deque(双端队列):append和popleft;appendleft和pop

from collections import deque q = deque([1, 2, 3]) # 入队 q.append(4) print(q) # deque([1, 2, 3, 4]) q.appendleft(0) # deque([0, 1, 2, 3, 4]) print(q) # 出队 q.pop() print(q) # deque([0, 1, 2, 3]) q.popleft() print(q) # deque([1, 2, 3]) # 查找 print(q[1]) # 2 # 其他 print(max(q), min(q), len(q)) # 3 1 3

五、栈(stack)

后进先出:LIFO = Last In First Out

python:1、List:append();pop()(尾部加,尾部出)

2、deque(双端队列):append和pop;appendleft和popleft

六、堆(Heap)

堆(Heap)是一种特殊的树形数据结构,常用于实现优先队列,堆中父节点和子节点之间有大小关系。

1、小堆:父节点≤子节点,堆顶是最小值

2、大堆:子节点≤父节点,堆顶是最大值

import heapq # 默认实现小堆 a = [99, 54, 87] # 将列表转化为堆 heapq.heapify(a) print(a) # [54, 99, 87] # 入堆 heapq.heappush(a, 2) print(a) # [2, 54, 87, 99] # 出堆 heapq.heappop(a) print(a) # [54, 99, 87] # 前n个最大值 print(heapq.nlargest(2,a)) # [99, 87] # 前n个最小值 print(heapq.nsmallest(2,a)) # [54, 87] # 实现大堆:把a里面每个值乘以-1

七、树(Tree)

树(Tree)是一种常见的非线性数据结构。由一个根节点开始,向下分出多个子节点,形成类似“树枝”的结构。(父子关系)

根节点:树最上边的节点

叶子节点:没有子节点的节点

高度:某个节点到叶子节点的最长路径

深度:从根节点到某个节点的边数

层:根节点为第一层,依次递加

1、二叉树:每个节点最多有两个子节点

满二叉树:除了叶子节点,每个节点都有两个子节点

完全二叉树:对树中的节点从上到下从左到右编号,节点的编号与满二叉树节点的编号相同

遍历:

A
/ \
B C
/ \
D E

a、前序遍历:当前节点→左节点→右节点 ABDEC

b、中序遍历:左节点→当前节点→右节点 DBEAC

c、后序遍历:左节点→右节点→当前节点 DEBCA

19
/ \
5 4
/ \
8 7

# 定义 class TreeNode: def __init__(self, value): self.value = value self.left = None self.right = None root = TreeNode(19) root.left = TreeNode(5) root.right = TreeNode(4) root.left.left = TreeNode(8) root.left.right = TreeNode(7) # 前序遍历 def preorder(root): if root is None: return print(root.value) preorder(root.left) preorder(root.right) # 中序遍历 def inorder(root): if root is None: return inorder(root.left) print(root.value) inorder(root.right) # 后序遍历 def postorder(root): if root is None: return postorder(root.left) postorder(root.right) print(root.value)

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询