-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Implement segment tree #79
Conversation
with the possibility of modifying the array elements. | ||
These queries could be finding the sum, minimum, or maximum in a subarray, or similar aggregated results. | ||
|
||
![Segment Tree](../../../../../docs/assets/images/SegmentTree.png) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will there be a short explanation of what the segment tree in the image actually represent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay nvm I just saw that it was explained below
### Querying | ||
To query an interval, say to find the sum of elements in the interval (L, R), | ||
the tree is traversed starting from the root: | ||
1. If the current node's segment is completely within (L, R), its value is part of the answer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does 'its value is part of the answer' mean that the current node's children nodes may be the answer?
This method utilizes a simple array where each element of the array corresponds to a node in the tree, | ||
including both leaves and internal nodes. | ||
|
||
### Why 4n space |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, could there perhaps be an explanation here as to why the total number of nodes will always be 4n?
guarantee we can house the entire segment tree. | ||
|
||
### Obtain index representing child nodes | ||
Suppose the parent node is captured at index *i* of the array (1-indexed). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could there also be an image that could encapsulate the indexing as well? For example representing the array like a binary tree like how you did in the above image but with indexing as well
private int query(SegmentTreeNode node, int leftEnd, int rightEnd) { | ||
// this is the case when: | ||
// start end | ||
// range query: ^ ^ --> so simply capture the sum at this node! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you capture the sums that are not part of the interval of the current node?
merge to main |
incomplete.