用python写一个线段树代码
时间: 2023-08-31 21:44:13 浏览: 185
### 回答1:
我可以给你一个简单的线段树的Python代码示例:# 定义线段树类
class SegmentTree:
def __init__(self, data):
self.data = data
# 初始化线段树
self.tree = [0] * (4 * len(data))
# 构建线段树
self.build(0, 0, len(data) - 1) # 构建线段树
def build(self, treeIndex, l, r):
# 如果是叶子节点
if l == r:
self.tree[treeIndex] = self.data[l]
return
# 计算出左右子树的索引
leftTreeIndex = 2 * treeIndex + 1
rightTreeIndex = 2 * treeIndex + 2 # 计算出中间位置
mid = (l + r) // 2
# 构建左子树
self.build(leftTreeIndex, l, mid)
# 构建右子树
self.build(rightTreeIndex, mid + 1, r)
# 计算出当前节点的值
self.tree[treeIndex] = self.tree[leftTreeIndex] + self.tree[rightTreeIndex] # 求区间和
def query(self, treeIndex, l, r, qL, qR):
# 如果查询区间完全包含了当前区间
if qL <= l and qR >= r:
return self.tree[treeIndex]
# 如果查询区间和当前区间没有交集
elif qL > r or qR < l:
return 0
# 其它情况,把当前区间一分为二,递归查询
else:
mid = (l + r) // 2
leftResult = self.query(2 * treeIndex + 1, l, mid, qL, qR)
rightResult = self.query(2 * treeIndex + 2, mid + 1, r, qL, qR)
return leftResult + rightResult
### 回答2:
线段树是一种常用的数据结构,用于解决区间查询问题。以下是使用Python编写的一个简单线段树代码示例:
```python
class SegmentTree:
def __init__(self, num_array):
self.n = len(num_array)
self.tree = [0] * (4 * self.n)
self.build(num_array, 0, self.n - 1, 0)
# 构建线段树
def build(self, num_array, start, end, tree_index):
if start == end:
self.tree[tree_index] = num_array[start]
return
mid = (start + end) // 2
left_child = 2 * tree_index + 1
right_child = 2 * tree_index + 2
self.build(num_array, start, mid, left_child)
self.build(num_array, mid + 1, end, right_child)
self.tree[tree_index] = self.tree[left_child] + self.tree[right_child]
# 更新线段树中某个元素的值
def update(self, index, value, start, end, tree_index):
if start == end:
self.tree[tree_index] = value
return
mid = (start + end) // 2
left_child = 2 * tree_index + 1
right_child = 2 * tree_index + 2
if index <= mid:
self.update(index, value, start, mid, left_child)
else:
self.update(index, value, mid + 1, end, right_child)
self.tree[tree_index] = self.tree[left_child] + self.tree[right_child]
# 查询线段树中某个区间的和
def query(self, query_start, query_end, start, end, tree_index):
if query_start <= start and query_end >= end:
return self.tree[tree_index]
if query_end < start or query_start > end:
return 0
mid = (start + end) // 2
left_child = 2 * tree_index + 1
right_child = 2 * tree_index + 2
left_sum = self.query(query_start, query_end, start, mid, left_child)
right_sum = self.query(query_start, query_end, mid + 1, end, right_child)
return left_sum + right_sum
```
这段代码实现了线段树的基本功能,包括建立线段树、更新某个元素的值以及查询某个区间的和。可以根据具体的需求对其进行加工和扩展。
### 回答3:
线段树是一种用于解决区间查询问题的数据结构,它可以高效地支持查询和更新操作。下面是一个使用Python编写的线段树代码示例:
```python
class SegmentTree:
def __init__(self, arr):
self.size = len(arr) # 输入数组的长度
self.tree = [0] * (2 * self.size) # 初始化线段树的数组
# 建立线段树
self.build_tree(arr, 0, 0, self.size - 1)
def build_tree(self, arr, tree_index, left, right):
if left == right:
self.tree[tree_index] = arr[left]
return
mid = (left + right) // 2
left_tree_index = 2 * tree_index + 1
right_tree_index = 2 * tree_index + 2
# 递归地建立左右子树
self.build_tree(arr, left_tree_index, left, mid)
self.build_tree(arr, right_tree_index, mid + 1, right)
# 根据左右子树的结果更新当前节点的值
self.tree[tree_index] = self.tree[left_tree_index] + self.tree[right_tree_index]
def query(self, tree_index, left, right, query_left, query_right):
if query_left > right or query_right < left: # 区间不相交
return 0
if query_left <= left and query_right >= right: # 查询区间完全覆盖当前节点所表示的区间
return self.tree[tree_index]
mid = (left + right) // 2
left_tree_index = 2 * tree_index + 1
right_tree_index = 2 * tree_index + 2
# 查询区间和当前节点区间部分相交,递归查询左右子树
left_result = self.query(left_tree_index, left, mid, query_left, query_right)
right_result = self.query(right_tree_index, mid + 1, right, query_left, query_right)
return left_result + right_result
def update(self, tree_index, left, right, update_index, value):
if left == right == update_index:
self.tree[tree_index] = value
return
mid = (left + right) // 2
left_tree_index = 2 * tree_index + 1
right_tree_index = 2 * tree_index + 2
if update_index <= mid:
self.update(left_tree_index, left, mid, update_index, value)
else:
self.update(right_tree_index, mid +1, right, update_index, value)
self.tree[tree_index] = self.tree[left_tree_index] + self.tree[right_tree_index]
```
以上是一个简单的线段树代码示例,其中包含了线段树的建立、查询和更新操作。可以根据具体的需求进行调用和使用。
阅读全文