文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Java数据结构之线段树详解

2024-04-02 19:55

关注

介绍

线段树(又名区间树)也是一种二叉树,每个节点的值等于左右孩子节点值的和,线段树示例图如下

以求和为例,根节点表示区间0-5的和,左孩子表示区间0-2的和,右孩子表示区间3-5的和,依次类推。

代码实现


public class SegmentTree<E> {

  private Node[] data;
  private int size;

  private Merger<E> merger;

  public SegmentTree(E[] source, Merger<E> merger) {
    this.merger = merger;
    this.size = source.length;
    this.data = new Node[size * 4];
    buildTree(0, source, 0, size - 1);
  }

  public E search(int queryLeft, int queryRight) {
    if (queryLeft < 0 || queryLeft > size || queryRight < 0 || queryRight > size
        || queryLeft > queryRight) {
      throw new IllegalArgumentException("index is illegal");
    }
    return search(0, queryLeft, queryRight);
  }

  
  private E search(int treeIndex, int queryLeft, int queryRight) {
    Node treeNode = data[treeIndex];
    int left = treeNode.left;
    int right = treeNode.right;
    if (left == queryLeft && right == queryRight) {
      return elementData(treeIndex);
    }
    int leftTreeIndex = leftChild(treeIndex);
    int rightTreeIndex = rightChild(treeIndex);
    int middle = left + ((right - left) >> 1);
    if (queryLeft > middle) {
      return search(rightTreeIndex, queryLeft, queryRight);
    } else if (queryRight <= middle) {
      return search(leftTreeIndex, queryLeft, queryRight);
    }
    E leftEle = search(leftTreeIndex, queryLeft, middle);
    E rightEle = search(rightTreeIndex, middle + 1, queryRight);
    return merger.merge(leftEle, rightEle);
  }

  public void update(int index, E e) {
    update(0, index, e);
  }

  
  private void update(int treeIndex, int index, E e) {
    Node treeNode = data[treeIndex];
    int left = treeNode.left;
    int right = treeNode.right;
    if (left == right) {
      treeNode.data = e;
      return;
    }
    int leftTreeIndex = leftChild(treeIndex);
    int rightTreeIndex = rightChild(treeIndex);
    int middle = left + ((right - left) >> 1);
    if (index > middle) {
      update(rightTreeIndex, index, e);
    } else {
      update(leftTreeIndex, index, e);
    }
    treeNode.data = merger.merge(elementData(leftTreeIndex), elementData(rightTreeIndex));
  }

  private void buildTree(int treeIndex, E[] source, int left, int right) {
    if (left == right) {
      data[treeIndex] = new Node<>(source[left], left, right);
      return;
    }
    int leftTreeIndex = leftChild(treeIndex);
    int rightTreeIndex = rightChild(treeIndex);
    int middle = left + ((right - left) >> 1);
    buildTree(leftTreeIndex, source, left, middle);
    buildTree(rightTreeIndex, source, middle + 1, right);
    E treeData = merger.merge(elementData(leftTreeIndex), elementData(rightTreeIndex));
    data[treeIndex] = new Node<>(treeData, left, right);
  }

  @Override
  public String toString() {
    return Arrays.toString(data);
  }

  private E elementData(int index) {
    return (E) data[index].data;
  }

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

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

  private static class Node<E> {

    E data;
    int left;
    int right;

    Node(E data, int left, int right) {
      this.data = data;
      this.left = left;
      this.right = right;
    }

    @Override
    public String toString() {
      return String.valueOf(data);
    }
  }

  public interface Merger<E> {

    E merge(E e1, E e2);
  }
}

我们以LeetCode上的一个问题来分析线段树的构建,查询和更新,LeetCode307问题如下:

给定一个整数数组,查询索引区间[i,j]的元素的总和。

线段树构建

private void buildTree(int treeIndex, E[] source, int left, int right) {
    if (left == right) {
      data[treeIndex] = new Node<>(source[left], left, right);
      return;
    }
    int leftTreeIndex = leftChild(treeIndex);
    int rightTreeIndex = rightChild(treeIndex);
    int middle = left + ((right - left) >> 1);
    buildTree(leftTreeIndex, source, left, middle);
    buildTree(rightTreeIndex, source, middle + 1, right);
    E treeData = merger.merge(elementData(leftTreeIndex), elementData(rightTreeIndex));
    data[treeIndex] = new Node<>(treeData, left, right);
  }

测试代码

public class Main {

  public static void main(String[] args) {
    Integer[] nums = {-2, 0, 3, -5, 2, -1};
    SegmentTree<Integer> segmentTree = new SegmentTree<>(nums, Integer::sum);
    System.out.println(segmentTree);
  }

}

最后构造出的线段树如下,前面为元素值,括号中为包含的区间。

递归构造过程为

区间查询

  
  private E search(int treeIndex, int queryLeft, int queryRight) {
    Node treeNode = data[treeIndex];
    int left = treeNode.left;
    int right = treeNode.right;
    if (left == queryLeft && right == queryRight) {
      return elementData(treeIndex);
    }
    int leftTreeIndex = leftChild(treeIndex);
    int rightTreeIndex = rightChild(treeIndex);
    int middle = left + ((right - left) >> 1);
    if (queryLeft > middle) {
      return search(rightTreeIndex, queryLeft, queryRight);
    } else if (queryRight <= middle) {
      return search(leftTreeIndex, queryLeft, queryRight);
    }
    E leftEle = search(leftTreeIndex, queryLeft, middle);
    E rightEle = search(rightTreeIndex, middle + 1, queryRight);
    return merger.merge(leftEle, rightEle);
  }

查询区间2-5的和

public class Main {

  public static void main(String[] args) {
    Integer[] nums = {-2, 0, 3, -5, 2, -1};
    SegmentTree<Integer> segmentTree = new SegmentTree<>(nums, Integer::sum);
    System.out.println(segmentTree);
    System.out.println(segmentTree.search(2, 5)); // -1
  }

}

查询过程为

更新

  
  private void update(int treeIndex, int index, E e) {
    Node treeNode = data[treeIndex];
    int left = treeNode.left;
    int right = treeNode.right;
    if (left == right) {
      treeNode.data = e;
      return;
    }
    int leftTreeIndex = leftChild(treeIndex);
    int rightTreeIndex = rightChild(treeIndex);
    int middle = left + ((right - left) >> 1);
    if (index > middle) {
      update(rightTreeIndex, index, e);
    } else {
      update(leftTreeIndex, index, e);
    }
    treeNode.data = merger.merge(elementData(leftTreeIndex), elementData(rightTreeIndex));
  }

更新只影响元素值,不影响元素区间。

更新其实和构建的逻辑类似,找到待更新的实际索引,依次更新父节点的值。

总结

线段树可以很好地处理区间问题,如区间求和,求最大最小值等。

以上就是Java 数据结构之线段树详解的详细内容,更多关于Java 线段树的资料请关注编程网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯