Non-fixed-size Fenwick Tree implementation

I’m planning to implement a non-fixed-size Fenwick tree. That is, a Fenwick tree that allows interleaving range queries with adding/removing elements.

All implementations and samples I’ve seen so far are for fixed-size Fenwick trees, where the number of elements is known before preprocessing the frequencies. They use a fixed size array, so it’s not possible to add or remove elements after preprocessing is done. (Well, it is possible, but I’d need to re-build the structure).

I thought of extending TreeMap or maybe AbstractMap, but as TreeMap is actually a red-black tree, I don’t know how to implement the red-black mechanics (so that the tree remains balanced) without loosing the cumulative sums of nodes involved in the rebalancing process.

So I thought that maybe I should take another approach: why not extend or adapt a simple random access ArrayList and re-calculate all cumulative sums when the underlying array is resized? This would of course have an impact but, hey, that’s exactly what a HashMapdoes.

This is why I wanted to ask here first, in case someone has already done it, and to check which approach you think is the best.

0

The Fenwick Tree is a space-efficient data structure because it foregoes the original data and instead only stored some “encoded” version.

As you have noticed, however, this encoding means that you cannot add or remove an element (except at the tail) without a lot of churn.

If your add/remove operations are infrequent enough than rebuilding the whole underlying array is possible in your case; just do so. This will mean a latency spike, but should not affect throughput significantly.

If, on the other hand, this overhead is not acceptable, then the Fenwick Tree is just not an appropriate data-structure for your issue. In this case, I would recommend an Augmented Binary Search Tree.

Take the Binary Search Tree implementation that you prefer (Red-Black Tree, AVL Tree, Splay Tree, …) or even a B-Tree if you’d like, and augment the information each node/element carries with:

  • the number of left and right children
  • the sum of left and right children

When adding/removing elements you will need to keep the numbers and sums up-to-date on the path leading to the root. Adding and removing is O(log N) already, so since you’ll be updating O(log N) nodes/elements you will not change the complexity significantly.

Then, implement new methods to access by index and retrieve the sum. Note that the index is not immediately accessible, but knowing the left sub-tree has 4 nodes, if you wish to access:

  • index 2, then you should go the left sub-tree
  • index 5, then you are here
  • index 7, then you should go the right sub-tree (and access index 2 in the right sub-tree: 7 – sizeof(left sub-tree) – 1)

2

This is the first time I heard of this data structure. What sized trees are you aiming for? Unless it’s just thousands, it’s probably not going to matter much which underlying data structure you start with.

From Wikipedia, I understand that index-based access to the structure is important. For that reason, I would go with an implementation based on plain ArrayList to start with.

Remember that the point of an ADT is that you can swap out the implementation details while keeping the interface intact.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Non-fixed-size Fenwick Tree implementation

I’m planning to implement a non-fixed-size Fenwick tree. That is, a Fenwick tree that allows interleaving range queries with adding/removing elements.

All implementations and samples I’ve seen so far are for fixed-size Fenwick trees, where the number of elements is known before preprocessing the frequencies. They use a fixed size array, so it’s not possible to add or remove elements after preprocessing is done. (Well, it is possible, but I’d need to re-build the structure).

I thought of extending TreeMap or maybe AbstractMap, but as TreeMap is actually a red-black tree, I don’t know how to implement the red-black mechanics (so that the tree remains balanced) without loosing the cumulative sums of nodes involved in the rebalancing process.

So I thought that maybe I should take another approach: why not extend or adapt a simple random access ArrayList and re-calculate all cumulative sums when the underlying array is resized? This would of course have an impact but, hey, that’s exactly what a HashMapdoes.

This is why I wanted to ask here first, in case someone has already done it, and to check which approach you think is the best.

0

The Fenwick Tree is a space-efficient data structure because it foregoes the original data and instead only stored some “encoded” version.

As you have noticed, however, this encoding means that you cannot add or remove an element (except at the tail) without a lot of churn.

If your add/remove operations are infrequent enough than rebuilding the whole underlying array is possible in your case; just do so. This will mean a latency spike, but should not affect throughput significantly.

If, on the other hand, this overhead is not acceptable, then the Fenwick Tree is just not an appropriate data-structure for your issue. In this case, I would recommend an Augmented Binary Search Tree.

Take the Binary Search Tree implementation that you prefer (Red-Black Tree, AVL Tree, Splay Tree, …) or even a B-Tree if you’d like, and augment the information each node/element carries with:

  • the number of left and right children
  • the sum of left and right children

When adding/removing elements you will need to keep the numbers and sums up-to-date on the path leading to the root. Adding and removing is O(log N) already, so since you’ll be updating O(log N) nodes/elements you will not change the complexity significantly.

Then, implement new methods to access by index and retrieve the sum. Note that the index is not immediately accessible, but knowing the left sub-tree has 4 nodes, if you wish to access:

  • index 2, then you should go the left sub-tree
  • index 5, then you are here
  • index 7, then you should go the right sub-tree (and access index 2 in the right sub-tree: 7 – sizeof(left sub-tree) – 1)

2

This is the first time I heard of this data structure. What sized trees are you aiming for? Unless it’s just thousands, it’s probably not going to matter much which underlying data structure you start with.

From Wikipedia, I understand that index-based access to the structure is important. For that reason, I would go with an implementation based on plain ArrayList to start with.

Remember that the point of an ADT is that you can swap out the implementation details while keeping the interface intact.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Non-fixed-size Fenwick Tree implementation

I’m planning to implement a non-fixed-size Fenwick tree. That is, a Fenwick tree that allows interleaving range queries with adding/removing elements.

All implementations and samples I’ve seen so far are for fixed-size Fenwick trees, where the number of elements is known before preprocessing the frequencies. They use a fixed size array, so it’s not possible to add or remove elements after preprocessing is done. (Well, it is possible, but I’d need to re-build the structure).

I thought of extending TreeMap or maybe AbstractMap, but as TreeMap is actually a red-black tree, I don’t know how to implement the red-black mechanics (so that the tree remains balanced) without loosing the cumulative sums of nodes involved in the rebalancing process.

So I thought that maybe I should take another approach: why not extend or adapt a simple random access ArrayList and re-calculate all cumulative sums when the underlying array is resized? This would of course have an impact but, hey, that’s exactly what a HashMapdoes.

This is why I wanted to ask here first, in case someone has already done it, and to check which approach you think is the best.

0

The Fenwick Tree is a space-efficient data structure because it foregoes the original data and instead only stored some “encoded” version.

As you have noticed, however, this encoding means that you cannot add or remove an element (except at the tail) without a lot of churn.

If your add/remove operations are infrequent enough than rebuilding the whole underlying array is possible in your case; just do so. This will mean a latency spike, but should not affect throughput significantly.

If, on the other hand, this overhead is not acceptable, then the Fenwick Tree is just not an appropriate data-structure for your issue. In this case, I would recommend an Augmented Binary Search Tree.

Take the Binary Search Tree implementation that you prefer (Red-Black Tree, AVL Tree, Splay Tree, …) or even a B-Tree if you’d like, and augment the information each node/element carries with:

  • the number of left and right children
  • the sum of left and right children

When adding/removing elements you will need to keep the numbers and sums up-to-date on the path leading to the root. Adding and removing is O(log N) already, so since you’ll be updating O(log N) nodes/elements you will not change the complexity significantly.

Then, implement new methods to access by index and retrieve the sum. Note that the index is not immediately accessible, but knowing the left sub-tree has 4 nodes, if you wish to access:

  • index 2, then you should go the left sub-tree
  • index 5, then you are here
  • index 7, then you should go the right sub-tree (and access index 2 in the right sub-tree: 7 – sizeof(left sub-tree) – 1)

2

This is the first time I heard of this data structure. What sized trees are you aiming for? Unless it’s just thousands, it’s probably not going to matter much which underlying data structure you start with.

From Wikipedia, I understand that index-based access to the structure is important. For that reason, I would go with an implementation based on plain ArrayList to start with.

Remember that the point of an ADT is that you can swap out the implementation details while keeping the interface intact.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Non-fixed-size Fenwick Tree implementation

I’m planning to implement a non-fixed-size Fenwick tree. That is, a Fenwick tree that allows interleaving range queries with adding/removing elements.

All implementations and samples I’ve seen so far are for fixed-size Fenwick trees, where the number of elements is known before preprocessing the frequencies. They use a fixed size array, so it’s not possible to add or remove elements after preprocessing is done. (Well, it is possible, but I’d need to re-build the structure).

I thought of extending TreeMap or maybe AbstractMap, but as TreeMap is actually a red-black tree, I don’t know how to implement the red-black mechanics (so that the tree remains balanced) without loosing the cumulative sums of nodes involved in the rebalancing process.

So I thought that maybe I should take another approach: why not extend or adapt a simple random access ArrayList and re-calculate all cumulative sums when the underlying array is resized? This would of course have an impact but, hey, that’s exactly what a HashMapdoes.

This is why I wanted to ask here first, in case someone has already done it, and to check which approach you think is the best.

0

The Fenwick Tree is a space-efficient data structure because it foregoes the original data and instead only stored some “encoded” version.

As you have noticed, however, this encoding means that you cannot add or remove an element (except at the tail) without a lot of churn.

If your add/remove operations are infrequent enough than rebuilding the whole underlying array is possible in your case; just do so. This will mean a latency spike, but should not affect throughput significantly.

If, on the other hand, this overhead is not acceptable, then the Fenwick Tree is just not an appropriate data-structure for your issue. In this case, I would recommend an Augmented Binary Search Tree.

Take the Binary Search Tree implementation that you prefer (Red-Black Tree, AVL Tree, Splay Tree, …) or even a B-Tree if you’d like, and augment the information each node/element carries with:

  • the number of left and right children
  • the sum of left and right children

When adding/removing elements you will need to keep the numbers and sums up-to-date on the path leading to the root. Adding and removing is O(log N) already, so since you’ll be updating O(log N) nodes/elements you will not change the complexity significantly.

Then, implement new methods to access by index and retrieve the sum. Note that the index is not immediately accessible, but knowing the left sub-tree has 4 nodes, if you wish to access:

  • index 2, then you should go the left sub-tree
  • index 5, then you are here
  • index 7, then you should go the right sub-tree (and access index 2 in the right sub-tree: 7 – sizeof(left sub-tree) – 1)

2

This is the first time I heard of this data structure. What sized trees are you aiming for? Unless it’s just thousands, it’s probably not going to matter much which underlying data structure you start with.

From Wikipedia, I understand that index-based access to the structure is important. For that reason, I would go with an implementation based on plain ArrayList to start with.

Remember that the point of an ADT is that you can swap out the implementation details while keeping the interface intact.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật