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 8304273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:47:58+00:00 2026-06-08T17:47:58+00:00

I have the following hierarchy: – A – X : [1, 2, 3] –

  • 0

I have the following hierarchy:

- A
    - X : [1, 2, 3]
    - Y : [4, 5]
    - Z : [10, 11]
- B
    - X : [6, 7]
    - Y : [8]

And what I want is to have following queries give me following results:

get(A) ==> [1,2,3,4,5,10,11]
get(A,Y) ==> [4,5]
get(B) ==> [6,7,8]
get(B,X) ==> [6,7]

So far, it seems easy. I can accomplish this by having a Dictionary> which can be a defaultdict(lambda : defaultdict(list)) in Python.
However, what if I need to make it more generic and have another level, or another 2 levels?
Something like :

- A
    - X
        - i  : [1]
        - ii : [2,3]
    - Y
        - i  : [4, 5]
    - Z
        - ii : [10, 11]
- B
    - X
        - ii  : [6]
        - iii : [7]
    - Y
        - i   : [8]

In this example, the first hierarchy is a “projection” of the second hierarchy where the last level is merged into the parent. So, all queries for the first hierarchy should give the same results.

Some sample queries for new level:

get(B, X, ii) ==> [6]
get(B,X) ==> [6,7]          (same query and result as before)

Please note that, data is only in leaf nodes. So, for insertion, whole path must be given:

insert(A, X, i, 20)

That also means, we can give the depth of the tree in constructor of the data structure.

EDIT: I realized that I need validation of depth:

  • Insert operation : whole path must be given and the len(path) must be equal to depth
  • Get operation : a path “deeper” than the depth of the structure is not allowed
  • 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-08T17:47:59+00:00Added an answer on June 8, 2026 at 5:47 pm

    I wrote this class based on the idea of @black_dragon and with the support for the validation of depth.
    Here is how to use it (copied from the test case):

    def test_index_with_sample_case_for_depth_2(self):
        idx = HierarchicalIndex(2)
    
        # A
        idx.insert(1, 'A', 'X')
        idx.insert(2, 'A', 'X')
        idx.insert(3, 'A', 'X')
    
        idx.insert(4, 'A', 'Y')
        idx.insert(5, 'A', 'Y')
    
        idx.insert(10, 'A', 'Z')
        idx.insert(11, 'A', 'Z')
    
        #B
        idx.insert(6, 'B', 'X')
        idx.insert(7, 'B', 'X')
    
        idx.insert(8, 'B', 'Y')
    
        assert_that(idx.get('A'), equal_to([1, 2, 3, 4, 5, 10, 11]))
        assert_that(idx.get('A', 'Y'), equal_to([4, 5]))
        assert_that(idx.get('B'), equal_to([6, 7, 8]))
        assert_that(idx.get('B', 'X'), equal_to([6, 7]))
    
    
    def test_index_with_sample_case_for_depth_3(self):
        idx = HierarchicalIndex(3)
    
        # A
        idx.insert(1, 'A', 'X', 'i')
        idx.insert(2, 'A', 'X', 'ii')
        idx.insert(3, 'A', 'X', 'ii')
    
        idx.insert(4, 'A', 'Y', 'i')
        idx.insert(5, 'A', 'Y', 'ii')
    
        idx.insert(10, 'A', 'Z', 'ii')
        idx.insert(11, 'A', 'Z', 'iii')
    
        #B
        idx.insert(6, 'B', 'X', 'ii')
        idx.insert(7, 'B', 'X', 'iii')
    
        idx.insert(8, 'B', 'Y', 'i')
    
        #same queries with case for depth 2
        assert_that(idx.get('A'), equal_to([1, 2, 3, 4, 5, 10, 11]))
        assert_that(idx.get('A', 'Y'), equal_to([4, 5]))
        assert_that(idx.get('B'), equal_to([6, 7, 8]))
        assert_that(idx.get('B', 'X'), equal_to([6, 7]))
    
        #new queries
        assert_that(idx.get('B', 'X', 'ii'), equal_to([6]))
        assert_that(idx.get('A', 'X', 'ii'), equal_to([2, 3]))
    

    And validation of depth:

    def test_index_should_validate_depth_in_operations(self):
        # ....
        # depth=3
        idx = HierarchicalIndex(3)
    
        assert_that(idx.get('A'), has_length(0))
        assert_that(idx.get('A', 'X'), has_length(0))
        assert_that(idx.get('A', 'X', 'i'), has_length(0))
        self.assertRaises(AssertionError, lambda: idx.get('A', 'X', 'i', '1'))
    
        self.assertRaises(AssertionError, lambda: idx.insert(1))
        self.assertRaises(AssertionError, lambda: idx.insert(1, 'A'))
        self.assertRaises(AssertionError, lambda: idx.insert(1, 'A', 'X'))
        idx.insert(1, 'A', 'X', 'i')        # should not raise anything
        self.assertRaises(AssertionError, lambda: idx.insert(1, 'A', 'X', 'i', 'a'))
    
        assert_that(idx.get('A', 'X', 'i'), equal_to([1]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class hierarchy class Test { public string Name { get;
This is in NetBeans 6.5, Java 6. I have the following hierarchy in the
I have following UI hierarchy Scroll View -->TableLayout-->Rows [each row has TextView+EditText] Problem Inside
I'd like to have the following hierarchy in Doctrine2: - Message - SMS -
I have the following file hierarchy: Lib > MyModule.rb Lib > MyModule.rb > MyClass.rb
Lets say I have the following package Hierarchy: A--> B --> C --> D
Let's say I have the following class hierarchy: TaskViewer inherits from ListViewer<Task> which in
Let's say I have the following class hierarchy in C++: class Base; class Derived1
I have an hierarchy of classes that looks like the following: class Critical {
I have following div in a page (I can not modify). <div id=:0.control>Click me</div>

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.