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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:23:19+00:00 2026-05-12T22:23:19+00:00

I have a Tree class with the following definition: class Tree { Tree(); private:

  • 0

I have a Tree class with the following definition:

class Tree {
  Tree();
private:
  TreeNode *rootPtr;
}

TreeNode represents a node and has data, leftPtr and rightPtr.

How do I create a copy of a tree object using a copy constructor? I want to do something like:

Tree obj1;
//insert nodes

Tree obj2(obj1); //without modifying obj1.

Any help is appreciated!

  • 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-05-12T22:23:19+00:00Added an answer on May 12, 2026 at 10:23 pm

    Pseudo-code:

    struct Tree {
      Tree(Tree const& other) {
        for (each in other) {
          insert(each);
        }
      }
    
      void insert(T item);
    };
    

    Concrete example (changing how you walk the tree is important to know, but detracts from showing how the copy ctor works, and might be doing too much of someone’s homework here):

    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    template<class Type>
    struct TreeNode {
      Type data;
      TreeNode* left;
      TreeNode* right;
    
      explicit
      TreeNode(Type const& value=Type()) : data(value), left(0), right(0) {}
    };
    
    template<class Type>
    struct Tree {
      typedef TreeNode<Type> Node;
    
      Tree() : root(0) {}
      Tree(Tree const& other) : root(0) {
        std::vector<Node const*> remaining;
        Node const* cur = other.root;
        while (cur) {
          insert(cur->data);
          if (cur->right) {
            remaining.push_back(cur->right);
          }
          if (cur->left) {
            cur = cur->left;
          }
          else if (remaining.empty()) {
            break;
          }
          else {
            cur = remaining.back();
            remaining.pop_back();
          }
        }
      }
      ~Tree() {
        std::vector<Node*> remaining;
        Node* cur = root;
        while (cur) {
          Node* left = cur->left;
          if (cur->right) {
            remaining.push_back(cur->right);
          }
          delete cur;
          if (left) {
            cur = left;
          }
          else if (remaining.empty()) {
            break;
          }
          else {
            cur = remaining.back();
            remaining.pop_back();
          }
        }
      }
    
      void insert(Type const& value) {
        // sub-optimal insert
        Node* new_root = new Node(value);
        new_root->left = root;
        root = new_root;
      }
    
      // easier to include simple op= than either disallow it
      // or be wrong by using the compiler-supplied one
      void swap(Tree& other) { std::swap(root, other.root); }
      Tree& operator=(Tree copy) { swap(copy); return *this; }
    
      friend
      ostream& operator<<(ostream& s, Tree const& t) {
        std::vector<Node const*> remaining;
        Node const* cur = t.root;
        while (cur) {
          s << cur->data << ' ';
          if (cur->right) {
            remaining.push_back(cur->right);
          }
          if (cur->left) {
            cur = cur->left;
          }
          else if (remaining.empty()) {
            break;
          }
          else {
            cur = remaining.back();
            remaining.pop_back();
          }
        }
        return s;
      }
    
    private:
      Node* root;
    };
    
    int main() {
      using namespace std;
    
      Tree<int> a;
      a.insert(5);
      a.insert(28);
      a.insert(3);
      a.insert(42);
      cout << a << '\n';      
    
      Tree<int> b (a);
      cout << b << '\n';
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.