Skip to content
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

Merged
merged 7 commits into from
Sep 26, 2024
Merged

feat: Implement segment tree #79

merged 7 commits into from
Sep 26, 2024

Conversation

4ndrelim
Copy link
Owner

incomplete.

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)
Copy link
Collaborator

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?

Copy link
Collaborator

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.
Copy link
Collaborator

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
Copy link
Collaborator

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).
Copy link
Collaborator

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!
Copy link
Collaborator

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?

@4ndrelim
Copy link
Owner Author

merge to main

@4ndrelim 4ndrelim merged commit cd3c465 into main Sep 26, 2024
1 check passed
@4ndrelim 4ndrelim deleted the branch-segmentTree branch September 30, 2024 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants