Skip to content

Commit a9bbf7c

Browse files
committed
feat: add -tree chapter
1 parent 7ed92ac commit a9bbf7c

File tree

7 files changed

+1115
-1125
lines changed

7 files changed

+1115
-1125
lines changed

docs/SUMMARY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [Overview][Randomization Overview]
1515
* [Discrete Probability](randomization/discrete-probability.md)
1616
* [Randomized Algorithms](randomization/randomized-algorithms.md)
17+
* [Reservoir Sampling](randomization/reservoir-sampling.md)
1718
* [Sorting][Sorting Overview]
1819
* [Overview][Sorting Overview]
1920
* [Selection Sort](sorting/selection-sort.md)
@@ -23,6 +24,7 @@
2324
* [Merge Sort](sorting/merge-sort.md)
2425
* [Heap Sort](sorting/heap-sort.md)
2526
* [Quick Sort](sorting/quick-sort.md)
27+
* [Non-Comparison Sort](sorting/non-comparison-sort.md)
2628
* [Graph Algorithms][Graph Algo]
2729
* [Overview][Graph Algo]
2830
* [Minimum Spanning Tree](graph-algorithms/minimum-spanning-tree.md)
@@ -37,6 +39,7 @@
3739
* [Heap](tree/heap.md)
3840
* [Disjoint Set](tree/disjoint-set.md)
3941
* [Threaded Binary Tree](tree/threaded-binary-tree.md)
42+
* [Segment Tree](tree/segment-tree.md)
4043
* [Searching][Searching Overview]
4144
* [Overview][Searching Overview]
4245
* [Binary Search](searching/binary-search.md)

docs/images/segment-tree.png

622 KB
Loading

docs/randomization/overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ In the algorithm design, [randomization](#randomization) plays a pivot role in p
1212

1313
* [Discrete Probability](discrete-probability.md) - probability analysis, indicator random variable and several statistics definitions covered.
1414
* [Randomized Algorithms][randomized] - median-finding problem, i<sup>th</sup> order statistic finding problem are analyzed and solved randomly and deterministic.
15+
* [Reservoir Sampling](reservoir-sampling.md) - an introduction to a common technique in data processing.
1516

1617
[randomized]: randomized-algorithms.md

docs/tree/overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ A _**complete k-ary tree**_ is a tree in which all leaves have the same _depth_
4242
* [B-Tree](b-tree.md) - a disk based tree structure, particularly useful in database design.
4343
* [Disjoint-Set](disjoint-set.md) - a set operation that finds relevance among data and build up forests for faster manipulations.
4444
* [Threaded Binary Tree](threaded-binary-tree.md) - a special form of binary tree that connect leaves to parents, useful in designing Morris traversal, which require &Omicron;(1) extra space.
45+
* [Segment Tree](segment-tree.md) - a binary tree used for storing intervals or segments within a data array to speed up queries such as [Range Minimum Query](segment-tree.md).

docs/tree/segment-tree.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Segment Tree
2+
3+
Segment tree is a contextual binary tree designed for storing intervals or segments of a series of data items to speed up specific queries such as [Range Minimum Query](https://en.wikipedia.org/wiki/Range_minimum_query). Being contextual, each node of the segment tree represents an interval of data indices. Consider the following example:
4+
5+
An array of integers _INPUT_ of size N, build a segment tree _T_ which has:
6+
7+
1. The _root_ of _T_ represents the data to be queried within interval _INPUT[0...N-1]_
8+
2. Leaves represent a single element INPUT[i] where 0 <= i < N.
9+
3. The internal nodes in level _h_ of _T_ represents 2<sup>h</sup> number of nodes, each have data to be queried from interval from _INPUT_[i...j], where 0 <= i < j < N
10+
11+
There are N leaves in T and N - 1 internal nodes. Thus, the total number of nodes is 2 &sdot; N - 1. When the segment tree is built, its structure cannot be changed.
12+
13+
<figure style="text:center">
14+
<img src="../images/segment-tree.png" />
15+
<figcaption>Figure 1. A Segment Tree Built on an Array of size 7</figcaption>
16+
</figure>
17+
18+
There are two operations that can be performed on a segment tree:
19+
20+
1. update: given an index and a value within the array, update the corresponding element. The time complexity is &Omicron;(log N) time.
21+
2. query: given index _l_ and _r_, return the contextual value within the interval from _l_ to _r_. (e.g. the minimal value within the range). In average, each query takes &Omicron;(log N) time.

0 commit comments

Comments
 (0)