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

  • Home
  • SEARCH
  • 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 8484581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:25:10+00:00 2026-06-10T20:25:10+00:00

I have a rooted ordered tree representing sets of integers. Each node stores the

  • 0

I have a rooted ordered tree representing sets of integers. Each node stores the size of the associated subtree, and also the max and min elements in this subtree. The branch degree of all the nodes if fixed (but determined at runtime). Also for sufficiently small subtrees I would like to change the representation to a bitmap for the subset associated. For example the root node may store a set of size 1000000, one of this children would store a subset of size 100000, then again one of his children would store a subset of size 10000 and in the next level we would stop using this representation and store just a plain bitmap for the associated subset.

I’m trying to implement this structure in C++ and my definition for the node type stores three integers (size, min and max), an array of pointers (something like node_t ** children) to subtrees and the bitmap (in case we are using this representation). The problem is that all the nodes are storing at least one element which is irrelevant (if the set is big enough we would be using the array of pointers but not the bitmap, for example). How should the node type be declared to solve this problem ? I thought about using two subtypes of node (one for each case) but I am not sure what the impact on the performance at runtime would be.

Thanks in advance.
PS. Please let me know if the question is unclear to edit it.

  • 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-10T20:25:11+00:00Added an answer on June 10, 2026 at 8:25 pm

    Since you’re using multiple representations, you’ll probably need at least two node types: The first will be a generic node that handles the root as well as nearby descendants, and the second type will contain a pointer to a map. The latter nodes don’t have any children persay, but their immediate ancestors should see them as an entire sub-tree rather than a terminating node that points to a map.

    Since each of the upper nodes have pointers to their children, you’ll need a way to ensure that these pointers are also able to point to the mapNodes as well as the branching ones. A good way to do this is to create a virtual base node type with a virtual function that returns whatever data you’re looking for. For example:

    class baseNode {
      virtual int getLargest();
      virtual baseNode* addData(int);
    };
    
    class leafNode : baseNode { //for non-map termination
      leafNode(int in) {Data = in;}
    
      int getLargest() {return Data;}
      baseNode* addData(int);
    
      int Data;
    };
    
    class treeNode : baseNode {
    
    public:
      int getLargest(); //returns leftChild->getLargest(), etc
      baseNode* addData(int);
    
      baseNode* leftChild;//can point to either a treeNode or mapNode
      baseNode* rightChild;
    };
    
    class mapNode : baseNode {
      baseNode* addData(int);
      int getLargest(); //parses subMap to find/return the desired value
    
      Map* subMap;
    };
    

    You’ll need a bit of finessing to get it to do what you need it to, but the principle is the same. Keep in mind that with 1m objects, every byte you add increases the net memory use by about a megabyte, so do try to keep things minimal. If all of your branching nodes eventually reach a mapNode, you can eliminate the leafNode declaration altogether.

    Adding data to the structure is tricky, especially since you’re working with multiple types and the parents (hopefully) don’t know anything about their neighbors; Use virtual accessors to do what’s needed. In many scenarios, if a branching node tries to add a value ‘down the line’, the child node it references may need to change type. In this case, the child should construct the new substructure then return it to the parent. This can be done like so:

    baseNode* treeNode::addData(int in) {
      if ((childCount+1) < threshold) { //not enough to merit a map
        //....
        //if (input needs to go to the leftChild) {
          if (leftChild == 0) {
            leftChild = new leafNode(in); 
          } else {
            leftChild = leftChild->addData(in);
          }
        //}  
        return (baseNode*)this; //casting may be optional
      } else {  //new Data merits converting self + kids into a map
        mapNode* newMap = new mapNode();
        //Set newMap->subMap to children, deleting as you go
    
        delete this;//remove self after return
        return (baseNode*)newMap; //return the mapNode holding subtree
      }
    }
    
    baseNode* leafNode::addData(int in) {
      treeNode* tmpNode = new treeNode(); //create replacement
      tmpNode->leftChild = this; //pin self to new node
      tmpNode->rightChild = new leafNode(in); //store data
      return (baseNode*)tmpNode;
    }
    
    baseNode* mapNode::addData(int in) {
      subMap->addValue(in);//However you do it...
      return (baseNode*)this; //parent is always a treeNode
    }
    

    The leftChild = leftChild->addData(in); usually won’t actually modify anything, especially if it points to a treeNode, however it doesn’t really hurt anything to do so and the extra if (newPtr != leftChild) check would just add unnecessary overhead. Note that it will cause a change if a leafNode needs to change into a treeNode with multiple kids, or if it’s a treeNode with enough children to merit changing itself (and it’s kids!) into a mapNode.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a possibly large rooted tree structure that I want to transform into
I am writing code in Java that uses an unordered, rooted tree where each
I have rooted my device and I have install busybox. Now I want to
I have a rooted Nexus S and it doesn't have Sqlite installed so I
I am working on a rooted Android device. Is there a way to have
I am testing my app on a rooted Nexus S phone. I finally have
I have a MyTouch 3G running Android, which has been rooted. I was wondering
I have rooted phone. I want to copy data/data/com.android.providers.telephony/databases/mmssms.db file to sd card programatically.
I have created a namespace extension that is rooted under Desktop. The main purpose
I have a Nexus One (rooted) and a Xoom (stock, not rooted). I have

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.