线段树

原创 2015年11月21日 14:56:07
线段树

        线段树是一棵树,而且是二叉搜索树。它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点。
        主要应用:适用于和区间统计有关的问题,例如大数据的动态修改
及多次查询就比较适合使用这种树,效果比较好。

        性质:

         1. 树中的每一个结点表示了一个区间[a,b]。a,b通常是整数。
         2. 对于每一个非叶结点所表示的结点[a,b],其左儿子表示的区间为
         [a,(a+b)/2],右儿子表示的区间为[(a+b)/2,b]。
        3. 同一层的节点所代表的区间,相互不会重叠。
         4. 叶子节点的区间是单位长度,不可再分。

        构造过程:
         1. 构造节点node,对应的区间为[start,end]
         2. 构造node的左孩子,对应的区间为[start,start + (end - start) / 2]
         3. 构造node右孩子,对应的区间为[start + (end - start) / 2 + 1,end]

        线段树的构造过程就是二叉树递归的创建的过程,代码如下:

SegmentTreeNode* SegmentTree::GenerateTree(vector<int> &nums, int start, int end) {
    if(start > end) return nullptr;
    SegmentTreeNode *node = new SegmentTreeNode(start,end);
    if(start == end) {
        node->SetSum(nums[start]);
        return node;
    }
    int mid = start + (end - start) / 2;
    SegmentTreeNode *left = GenerateTree(nums,start,mid);
    SegmentTreeNode *right = GenerateTree(nums,mid+1,end);
    node->SetLeft(left);
    node->SetRight(right);
    node->SetSum(left->GetSum() + right->GetSum());
    return node;
}

        其中SegmentTreeNode 以及 SegmentTree为自己定义的类,最后附上整个的代码。

#include <vector>
using namespace std;

class SegmentTreeNode {
public:
    SegmentTreeNode(int a, int b);
    int GetStart();
    int GetEnd();
    void SetSum(int sum);
    int GetSum();
    void SetLeft(SegmentTreeNode* left);
    SegmentTreeNode* GetLeft();
    void SetRight(SegmentTreeNode* right);
    SegmentTreeNode* GetRight();

private:
    int start, end, sum;
    SegmentTreeNode* left;
    SegmentTreeNode* right;

};


class SegmentTree {
public:
    void BuildTree(vector<int> &nums, int start, int end);
    SegmentTreeNode *GetRoot();
    int ModifyTree(int i, int val);
    int QueryTree(int i, int j);

private:
    SegmentTreeNode *GenerateTree(vector<int> &nums, int start, int end);
    int _ModifyTree(int i, int val,SegmentTreeNode *node);
    int _QueryTree(int i, int j,SegmentTreeNode *node);
    SegmentTreeNode *root;
};


SegmentTreeNode::SegmentTreeNode(int a, int b):start(a),
                                               end(b),
                                               sum(0),
                                               left(nullptr),
                                               right(nullptr) {



};

int SegmentTreeNode::GetStart() {
    return start;
}
int SegmentTreeNode::GetEnd() {
    return end;
}
void SegmentTreeNode::SetSum(int sum) {
    this->sum = sum;
}
int SegmentTreeNode::GetSum() {
    return sum;
}

void SegmentTreeNode::SetLeft(SegmentTreeNode* left) {
    this->left = left;
}
SegmentTreeNode* SegmentTreeNode::GetLeft() {
    return left;
}
void SegmentTreeNode::SetRight(SegmentTreeNode* right) {
    this->right = right;
}

SegmentTreeNode* SegmentTreeNode::GetRight() {
    return right;
}

void SegmentTree::BuildTree(vector<int> &nums, int start, int end) {
    root = GenerateTree(nums, start, end);
}

SegmentTreeNode* SegmentTree::GenerateTree(vector<int> &nums, int start, int end) {
    if(start > end) return nullptr;
    SegmentTreeNode *node = new SegmentTreeNode(start,end);
    if(start == end) {
        node->SetSum(nums[start]);
        return node;
    }
    int mid = start + (end - start) / 2;
    SegmentTreeNode *left = GenerateTree(nums,start,mid);
    SegmentTreeNode *right = GenerateTree(nums,mid+1,end);
    node->SetLeft(left);
    node->SetRight(right);
    node->SetSum(left->GetSum() + right->GetSum());
    return node;
}

int SegmentTree::ModifyTree(int i, int val) {
    return _ModifyTree(i, val, root);
}

int SegmentTree::QueryTree(int i, int j) {
    return _QueryTree(i, j, root);
}

int SegmentTree::_ModifyTree(int i, int val,SegmentTreeNode *node) {
    if(node == nullptr) return 0;
    int diff;
    if(node->GetStart() == i && node->GetEnd() == i) {
        diff = val - node->GetSum();
        node->SetSum(val);
        return diff;
    }
    int mid = (node->GetStart() + node->GetEnd()) / 2;
    if(i > mid) {
        diff = _ModifyTree(i,val,node->GetRight());
    } else {
        diff = _ModifyTree(i,val,node->GetLeft());
    }
    node->SetSum(node->GetSum() + diff);
    return diff;
}

int SegmentTree::_QueryTree(int i, int j,SegmentTreeNode *node) {
    if(node == nullptr) return 0;
    if(node->GetStart() == i && node->GetEnd() == j) return node->GetSum();
    int mid = (node->GetStart() + node->GetEnd()) / 2;
    if(i > mid) return _QueryTree(i,j,node->GetRight());
    if(j <= mid) return _QueryTree(i,j,node->GetLeft());
    return _QueryTree(i,mid,node->GetLeft()) + _QueryTree(mid+1,j,node->GetRight());
}

        利用此代码在https://leetcode.com上AC过了两道题目。

Question 1: Given an integer array nums, find the sum of the elements
between indices i and j (i ≤ j), inclusive.

Example: Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note:
You may assume that the array does not change. There are many calls to
sumRange function.

Question 2: Given an integer array nums, find the sum of the elements
between indices i and j (i ≤ j), inclusive.

The update(i, val) function modifies nums by updating the element at
index i to val. Example: Given nums = [1, 3, 5]

sumRange(0, 2) -> 9 update(1, 2) sumRange(0, 2) -> 8 Note: The array
is only modifiable by the update function. You may assume the number
of calls to update and sumRange function is distributed evenly.

        源码的下载地址:https://github.com/FyhSky/SegmentTree

        转载请注明出处:http://blog.csdn.net/skynullcode

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

TOJ 4325 RMQ with Shifts(线段树的单点更新)

In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query ...

线段树代码

  • 2014-09-11 12:03
  • 26KB
  • 下载

Codevs 4927 线段树练习5

4927 线段树练习5 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 有n个数和5种操作 add a b c:把区间[a,...

线段树学习步骤

  • 2013-08-09 20:28
  • 773KB
  • 下载

线段树简介

  • 2013-11-23 20:43
  • 159KB
  • 下载

线段树模板--C++

无合并/* adrui's Segment Tree *///无区间合并的区间更新 & 查询 const int N = 100000 + 5;int ans[N << 2], lazy[N <> ...
  • ADjky
  • ADjky
  • 2016-12-21 19:25
  • 346

帮助理解的线段树

  • 2013-07-29 17:06
  • 166KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)