Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:14:03+00:00 2026-06-16T05:14:03+00:00

I found this in book: Design a data structure for maintaining dynamic sequence x_1,

  • 0

I found this in book:

Design a data structure for maintaining dynamic sequence x_1, x_2,... , x_n 
which provides operations:
Insert(a,i) - inserts a as x_i, indexes from i+1 to n go up by 1
Delete(i) - deletes x_i, indexes from i+1 to n go down by 1
Find(i) - returns element x_i
Sum_even() - returns sum of the elements with even indexes

The sequence is dynamic so I want to use AVL tree to keep it. So I think I can use standard trick for find, delete and insert – just keep in each node size of sub tree rooted in this node. Then I can easily navigate the tree to find ith element in O(log n) time. Inorder gives me then: x_1, x_2,…, x_n.

But for Sum_even() I probably should have also sum of elements with even and odd indexes in each node. Although it’s difficult to update while inserting or deleting. How should it work, can anyone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:14:04+00:00Added an answer on June 16, 2026 at 5:14 am

    You don’t have to store size of subtree in a node. AVL tree stores only difference in heights of left and right subtree: -1, 0 or +1.

    In order to easily maintain sum_even you need to store sum_even and sum_odd for each node. It will be the sum of values on even/odd indexes for subtree.

    So each node will have variables:

    • difference difference in height of left and right subtrees
    • value
    • parity parity of subtree size (0 or 1)
    • sum_even
    • sum_odd

    For inserts and deletes use standard algorithm http://en.wikipedia.org/wiki/AVL_tree.
    But for each affected node (nodes on the way up to root and rotated nodes) update values:

    parity := (left.parity + right.parity + 1) % 2
    if left.parity == 0
        sum_even := left.sum_even + right.sum_odd
        sum_odd := left.sum_odd + right.sum_even + value
    else
        sum_even := left.sum_even + right.sum_even + value
        sum_odd := left.sum_odd + right.sum_odd
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From Accelerated C++ (book), I found this code which is identical program, but the
I found this problem in a book. Problem: What is the output of the
While learning Rails, I found this example in the Sam Ruby's book: app/controllers/line_items_controller.rb def
I'm looking for a document/text/book about the database design standards, until now I've found
Hi I was going through the SCJP book about the innerclasses, and found this
I am learning Collections in CoreJava book and I found this code: List<String> a
The HTTP protocol is stateless, but I found this on the Kurose-Ross book: The
I found this html for add_filter menu: <li class=dropdown style=float:right> <a class=dropdown-toggle data-toggle=dropdown href=#>
I found quicksort algorithm from this book This is the algorithm QUICKSORT (A, p,
I am willing to learn more from concurrency, and I have found this book

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.