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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:03:48+00:00 2026-05-10T14:03:48+00:00

How do I make a tree data structure in C++ that uses iterators instead

  • 0

How do I make a tree data structure in C++ that uses iterators instead of pointers? I couldn’t find anything in the STL that can do this. What I would like to do is to be able to create and manipulate trees like this:

#include <iostream> #include <tree> using namespace std;  int main() {     tree<int> myTree;      tree<int>::iterator i = myTree.root();     *i = 42;      tree<int>::iterator j = i.add_child();     *j = 777;     j = j.parent();      if (i == myTree.root() && i == j) cout << 'i and j are both pointing to the root\n';      return 0; } 

Thank you, tree.hh seems to be just what I was looking for.

If this is for gaining the benefit of a data-structure holding arbitrary index types, optimized for searching and good at insertion then consider using a map.

A map is an associative container that has performance guarantees identical to those of a tree: logarithmic searching, logarithmic insertion, logarithmic deletion, linear space. Internally they are often implemented as red-black trees, although that is not a guarantee. Still, as an STL user all you should care about is the performance guarantees of the STL algorithms and data-structures. Whether they’re implemented as trees or little green men shouldn’t matter to you.

I’m not sure if a map is what I need, but thanks for the info. I will remember to use maps whenever possible instead of implementing trees.

  • 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. 2026-05-10T14:03:48+00:00Added an answer on May 10, 2026 at 2:03 pm

    Here is tree.hh which is a bit close to what you want to do, though a bit different.

    Here is a piece of code extracted from its website.

    int main(int, char **)    {    tree<string> tr;    tree<string>::iterator top, one, two, loc, banana;     top=tr.begin();    one=tr.insert(top, 'one');    two=tr.append_child(one, 'two');    tr.append_child(two, 'apple');    banana=tr.append_child(two, 'banana');    tr.append_child(banana,'cherry');    tr.append_child(two, 'peach');    tr.append_child(one,'three');     loc=find(tr.begin(), tr.end(), 'two');    if(loc!=tr.end()) {       tree<string>::sibling_iterator sib=tr.begin(loc);       while(sib!=tr.end(loc)) {          cout << (*sib) << endl;          ++sib;          }       cout << endl;       tree<string>::iterator sib2=tr.begin(loc);       tree<string>::iterator end2=tr.end(loc);       while(sib2!=end2) {          for(int i=0; i<tr.depth(sib2)-2; ++i)              cout << ' ';          cout << (*sib2) << endl;          ++sib2;          }       }    } 

    Now what’s different? Your implementation is simpler when it comes to append a node to the tree. Though your version is indiscutably simpler, the dev of this lib probably wanted to have some info accessible without browsing the tree, such as the size of the tree for instance.

    I also assume he didn’t want to store the root on all nodes for performance reason. So if you want to implement it your way, I suggest you keep most of the logic and add the link to the parent tree in the iterator and rewrite append a bit.

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

Sidebar

Ask A Question

Stats

  • Questions 63k
  • Answers 63k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Open-sourcing may indeed be a good idea. If your project… May 11, 2026 at 10:28 am
  • added an answer I presume you are calling it within some loop which… May 11, 2026 at 10:28 am
  • added an answer You probably want to use struct. The code would look… May 11, 2026 at 10:28 am

Related Questions

How do I make a tree data structure in C++ that uses iterators instead
How do I make a user's browser blink/flash/highlight in the task bar using JavaScript?
How do I make a control fill the whole cell in a ListView (with
how do I make a pointer to a multidimensional array, which have a unknown
In a Django form, how do I make a field read-only (or disabled)? When
How do I make the most out of a MS in Business Analytics?
How do I make all occurrences of a phrase (search term) in a file
How do I make open read only the only option within a SharePoint document
How do I make diff ignore temporary files like foo.c~ ? Is there a
How do I clear the value from a cell and make it NULL?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.