Thread: sum(S,k1,k2) in O(logn)???

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    10

    sum(S,k1,k2) in O(logn)???

    i have a h.w about red black trees.
    they ask to find a data structure that support the following algorithems
    1. insert in O(logn)
    2.delete in O(logn)
    3. pair-dif(S,d)-find x1,x2 that x2-x1=d>0
    in O(n) time
    4. sum(S,k1,k2)- return the sum of all the values between k1 and k2
    in O(logn)
    i used RB tree to solve 1-3 but i don't know how to solve 4.
    every time i think on a solution it evolve summing all the members in which k1<x<k2 and considering the fact that k1<min(S)<max(S)<k2
    may happen it mean O(n) time.
    i don't have any limitation on space complexity.
    thanks for the help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 4. sum(S,k1,k2)- return the sum of all the values between k1 and k2
    > in O(logn)
    Presumably, your RB tree is ordered such that if you find a value <K1 or >K2, you can dismiss the entire subtree beneath it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    10
    i can build whatever data sructure i want which as much space complexity as i need(no limitation).
    i guess i need to use RB trees cause this is the material in the chapters that the assignment about.
    for the first 3 algorithems i can use regular RB tree but i don't know how to change the tree so it will fit the sum alghorithem too.

    i think the data structre you just mantioned is impossible. in order to do it you wil have to eliminate a subtree just beacuse the father is father<k1
    but right[father]>father so it is possible that father<k1<right[father]
    k1\k2 aren't const and you don't know them during insert
    Last edited by Naama Levi; 06-12-2016 at 08:14 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I dunno, you claimed to have solutions for 1 to 3, perhaps start there.

    Red–black tree - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2015
    Posts
    10
    the solution to 1-2 is to use the RB-INSERT RB-DELETE given in the book
    in 3 i can create an arrray B[n] and using an alghrithem called INORDER-WALK-TREE sort the keys in the array(O(n) time)
    then i can do the classic SUM-2 solution on a sorted array(O(n)) time

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > the solution to 1-2 is to use the RB-INSERT RB-DELETE given in the book
    Oh good, you've learned to copy and paste, but did you understand what you were doing any why?

    > i don't have any limitation on space complexity.
    Perhaps you can use the extra space where each node stores the sum of all the nodes beneath it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2015
    Posts
    10
    Yes i understand the solution in the book. I have to cause in some of the questions in this course i have to "open"
    The algorithem and change it so it will have new properties. Meaning i have to understand each and every line in it.

    I thought on this solution. The problem is that beacuse of the structure of binary tree even if for example father<k1 his son or his son son
    Exp my still be in the range meaning i`ll have to find manually all his decended in the range.

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Ensure node values are unique. If you want duplicates, store them as a count for the node with that value.

    Maintain a sum for the subtree rooted at each node, inclusive of the node's total value (node.value * node.count). This is easy to maintain and doesn't add to the complexity of additions, deletions, rotations.
    Code:
    Example: (node.value, node.sum)    // assume all node.count's are 1
    
                                10,94
                  5,21                        15,63
           3,3             7,13        13,13         17,35
                       6,6                                 18,18
    Consider how to use the above info to calculate the sum from the smallest value up to a particular value, like from 3 to 6, 3 to 10, 3 to 17. You can use those sums to calculate the sum between any two values.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heap Sort - O(logn)
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-14-2002, 08:19 PM

Tags for this Thread