吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1659|回复: 7
收起左侧

[Java 转载] 【笔记】数据结构与算法之线段树

[复制链接]
QingYi. 发表于 2020-11-29 22:04
本帖最后由 QingYi. 于 2020-11-29 22:06 编辑
Segment线段树的定义及其方法

[Java] 纯文本查看 复制代码
public class SegmentTree<E> {
    private E[] data;
    private E[] tree;
    private Merger<E> merger;
    public SegmentTree(E[] arr,Merger<E> merger){
        this.merger = merger;
        int n = arr.length;
        data = (E[])new Object[arr.length];
        for (int i = 0; i < n; i++) {
            data[i] = arr[i];
        }
        tree = (E[])new Object[4 * arr.length];
        buildSegmentTree(0, 0, arr.length - 1);
    }
    public E query(int l , int r){
        if (l < 0 || l >= data.length || r < 0 || r >= data.length || l > r) {
            throw new IllegalArgumentException("index is illegal");
        }
        return query(0,0,data.length-1,l,r);
    }
    //在l到r 的区间里 搜索queryl  到 queryr的值
    private E query(int index, int l, int r, int queryL, int queryR) {
        if(l==queryL && r == queryR){
            return tree[index];
        }
        int mid = (l+r)>>1;
        int leftIdx =  leftChild(index);
        int rightIdx =  rightChild(index);
        if (queryL>=mid+1){
            return query(rightIdx,mid+1,r,queryL,queryR);
        }else if (queryR<=mid){
            return query(leftIdx,l,mid,queryL,queryR);
        }
        E leftRes = query(leftIdx, l, mid, queryL, mid);
        E rightRes = query(rightIdx, mid+1, r, mid+1, queryR);
        E merge = merger.merge(leftRes, rightRes);
        return merge;


    }

    public void set(int idx,E e){
        if (idx<0 || idx>=data.length) {
            throw new IllegalArgumentException("index is illegal");
        }
        data[idx] = e;
        set(0,0,data.length-1,idx,e);
    }
    //在treeIdx中为根的线段树中更新index的值为e
    public void set(int treeIdx,int l ,int r,int index,E e){
        if (l==r){
            tree[treeIdx] = e;
        }
        int mid = (l+r)>>1;
        int leftTreeIndex = leftChild(treeIdx);
        int rightTreeIndex = rightChild(treeIdx);
        if (index>=mid+1){
            set(rightTreeIndex,mid+1,r,index,e);
        }else {
            set(leftTreeIndex,l,mid,index,e);
        }
        tree[treeIdx] = merger.merge(tree[leftTreeIndex],tree[rightTreeIndex]);

    }
    private void buildSegmentTree(int treeIndex, int l, int r){

        if(l == r){
            tree[treeIndex] = data[l];
            return;
        }

        int leftTreeIndex = leftChild(treeIndex);
        int rightTreeIndex = rightChild(treeIndex);

        // int mid = (l + r) / 2;
        int mid = l + (r - l) / 2;
        buildSegmentTree(leftTreeIndex, l, mid);
        buildSegmentTree(rightTreeIndex, mid + 1, r);

        tree[treeIndex] = merger.merge(tree[leftTreeIndex], tree[rightTreeIndex]);
    }

    public int getSize(){
        return data.length;
    }
    public E get(int idx){
        if (idx<0 || idx>=data.length){
            throw new IllegalArgumentException("idx is illegal");
        }
        return data[idx];
    }
    private int leftChild(int idx){
        return 2*idx+1;
    }
    private int rightChild(int idx){
        return 2*idx+2;
    }
    @Override
    public String toString(){
        StringBuilder sb  = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < tree.length; i++) {
            if (tree[i]!=null) {
                sb.append(tree[i]);
            }else {
                sb.append("null");
            }
            if (i!=tree.length-1){
                sb.append(",");
            }
        }
        sb.append(']');
        return sb.toString();
    }
}

Main方法测试类

[Java] 纯文本查看 复制代码
public class Main {
    public static void main(String[] args) {
        Integer[] nums = {-2,0,3,-5,2,-1};
        SegmentTree<Integer> segTree = new SegmentTree<>(nums,
                (a, b) -> a + b);
        System.out.println(segTree.query(0, 2));
        System.out.println(segTree.query(2, 5));
        System.out.println(segTree.query(0, 5));
    }
}


Merger合并接口

[Asm] 纯文本查看 复制代码
public interface Merger<E> {
    E merge(E a, E b);
}



@alittlebear 进来学习了



免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
alittlebear + 1 + 1 霍,完全看不懂呀

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

sizhubiao 发表于 2020-11-29 23:38
感谢楼主分享,学习了!
冰水混合物 发表于 2020-11-30 00:09
alittlebear 发表于 2020-11-30 01:16
image.png

看不懂,但是勉强还是能运行的。。吧

大佬好厉害
 楼主| QingYi. 发表于 2020-11-30 08:29
alittlebear 发表于 2020-11-30 01:16
看不懂,但是勉强还是能运行的。。吧

大佬好厉害

我也是研究了两天,debug一步一步看结运行果,才搞明白的
wzh202 发表于 2020-11-30 12:39
这个是数组类型定义的线性表的合并方法吗
 楼主| QingYi. 发表于 2020-11-30 18:06
wzh202 发表于 2020-11-30 12:39
这个是数组类型定义的线性表的合并方法吗

线段树不是属于线性表哦
 楼主| QingYi. 发表于 2020-11-30 18:07
wzh202 发表于 2020-11-30 12:39
这个是数组类型定义的线性表的合并方法吗

我把合并去掉给你看下
[Java] 纯文本查看 复制代码
class NumArray {    int [] data;
    int [] tree;
    public NumArray(int[] nums) {
        int n = nums.length;
        if (nums.length>0){
            data = new int[n];
            for (int i = 0; i < n; i++) {
                data[i] = nums[i];
            }
            tree = new int[4*n];
            buildTree(0,0,n-1);
        }
    }

    private void buildTree(int treeIdx, int l, int r) {
        if (l==r){
            tree[treeIdx] = data[l];
            return;
        }
        int mid = (l+r)>>1;
        int leftIdx = leftChild(treeIdx);
        int rightIdx = rightChild(treeIdx);
        buildTree(leftIdx,l,mid);
        buildTree(rightIdx,mid+1,r);
        tree[treeIdx] = tree[leftIdx]+tree[rightIdx];
    }

    private int rightChild(int treeIdx) {
        return treeIdx*2 +2;
    }

    private int leftChild(int treeIdx) {
        return treeIdx*2+1;
    }

    public void update(int i, int val) {
        update(0,0,data.length-1,i,val);
    }

    private void update(int treeIdx ,int l, int r, int target, int val) {
        if (l==r){
            tree[treeIdx] = val;
            return;
        }
        int mid = (l+r)>>1;
        int leftIdx = leftChild(treeIdx);
        int rightIdx = rightChild(treeIdx);
        if (target>=mid+1){
            update(rightIdx, mid+1, r, target, val);
        }else {
            update(leftIdx, l, mid, target, val);
        }
        tree[treeIdx] = tree[leftIdx]+tree[rightIdx];
    }

    public int sumRange(int i, int j) {
        return query(0, 0, data.length - 1, i, j);
    }

    private int query(int treeIdx, int l, int r, int queryL, int queryR) {
        if (queryL < 0 || queryL >= data.length || queryR < 0 || queryR >= data.length || queryL > queryR) {
            throw new IllegalArgumentException();
        }
        if (l==queryL && r==queryR){
            return tree[treeIdx];
        }
        int mid = (l+r)>>1;
        int leftTreeIndex = leftChild(treeIdx);
        int rightTreeIndex = rightChild(treeIdx);
        if (queryR<=mid){
            return query(leftTreeIndex, l, mid, queryL, queryR);
        } else if (queryL >= mid + 1) {
            return query(rightTreeIndex, mid+1, r, queryL, queryR);
        }
        int  leftRes = query(leftTreeIndex, l, mid, queryL, mid);
        int  rightRes = query(rightTreeIndex, mid+1, r, mid+1, queryR);
        return leftRes + rightRes;
    }
}

在新标签打开所有链接复制所有链接URL复制所有链接URL(反向)复制所有链接标题 + URL复制所有链接标题 + URL (MD)复制所有链接标题 + URL (BBS)复制所有链接标题 + URL (筛选)复制所有链接标题 + URL (设置复制格式)在新标签页打开所有图片链接在一个标签页显示所有图片链接
复选框 - 选中
复选框 - 取消
复选框 - 反选
单选框 - 选中
单选框 - 取消
特殊单选框 - 选中
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 21:14

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表