1

I'm looking for a data structure, that is basically a tree of maps, where the map at each node contains some new elements, as well as the elements in its parent node's map. By map here I mean a programming map with keys and values, like map in the STL or dict in python.

For example, there might be a root node:

root = {'car':1, 'boat':2}

and 2 children, each adding an element to the parent map

child1 = {'car':1, 'boat':2, 'jet':35}
child2 = {'car':1, 'boat':2, 'scooter':-5}

My searches will then be performed on the nodes. So for example child1['jet'] returns 35, but root['jet'] returns a not found error.

I would like this to be as space efficient as possible, ie i don't want to store a complete copy of the resulting map at each node, but ideally the lookup would still be O(log N), N being the total number of elements at the node, not the entire tree.

I was thinking perhaps there is a smart hash function I might use for this, but couldn't come up with anything.

The naive approach would be storing the newly added entries in a map at each node and then move up the tree if nothing is found. I don't like this because it depends on the tree depth.

phreeza
  • 155
  • 8

2 Answers2

0

How about creating a function that will compare hashmaps, it will return true or false whether they match, this might be a bit tricky cause of ordering of the key and value pairs.

Then use this function whenever you are adding a new node (map) to the tree. Check all the existing nodes in the tree and if the hashmap already exists, just have it point to that one.

This can require a lot of processing for comparing hashmaps but this will conserve most space.

Hope this helps.

edit: You may be able to do a Union on the maps and see if the result is the same length for comparison.

bhavinp
  • 823
  • 1
  • 9
  • 18
0

What I understand is that you are looking for 'jet' and that will get you the entire list of child1.

Your primary data will be a tree. You will keep a reference to all data at that level (eg 'jet':35, as well as a pointer to the parent.

The reference will be through another hash structure. This will map the key ('jet') to a pointer to the tree.

map['jet']  =>  {'jet':35, parent:root}

Which can then be expanded to

map['jet']  =>  {'car':1, 'boat':2, 'jet':35}

alt text

Kevin Lacquement
  • 5,057
  • 3
  • 25
  • 30
  • ah, I should have made that clearer. My searches will be performed on the nodes. So for example child1['jet'] returns 35, but root['jet'] returns a not found error. I'll edit to clarify. – phreeza Nov 25 '10 at 20:21