线段树

原创 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

线段树 PPT

  • 2017年11月19日 18:02
  • 630KB
  • 下载

线段树高级数据结构实现

  • 2016年05月26日 13:20
  • 2KB
  • 下载

【BZOJ】1493 [NOI2007]项链工厂 线段树

传送门:【BZOJ】1493 [NOI2007]项链工厂 题目分析:

线段树代码

  • 2014年09月11日 12:03
  • 26KB
  • 下载

hdu1166线段树(单点更新)

题目链接敌兵布阵 随便找一个blog都已经讲的够好了,直接上代码 #include #include using namespace std; const int maxn=50000; in...

竞选海报(线段树加离散化)

  • 2013年05月11日 18:25
  • 2KB
  • 下载

线段树学习步骤

  • 2013年08月09日 20:28
  • 773KB
  • 下载

HDU 1166 线段树模板&树状数组模板

HDU1166 上好的线段树模板&&树状数组模板 线段树:#include using namespace std; int cases,n,tree[200500],ql,qr; char s...

帮助理解的线段树

  • 2013年07月29日 17:06
  • 166KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:线段树
举报原因:
原因补充:

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