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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:11:23+00:00 2026-06-14T05:11:23+00:00

Main: #include <iostream> #include <cstdlib> #include avl_tree.h using namespace std; int main() { AVLTree<int>

  • 0

Main:

#include <iostream>
#include <cstdlib>
#include "avl_tree.h"


using namespace std;


int main()
{
  AVLTree<int> av1;
  int testarray [10] = { 16, 2, 77, 40, 54 , 1 , 100, 39, 73, 35 };
  AVLTree<int> av3;

  for( unsigned int i = 0; i < 10; i++ )
  {
    av1.insert( testarray[i] );
  }

  AVLTree<int> av2 = av1; //test copy constructor
  av3 = av1;  //test operator=
  av2.printTree();
  av1.printTree();
  av3.printTree();

  exit( 0 );
}

Header:

#ifndef AVL
#define AVL

#include <iostream>
using namespace std;
/**
 * An AVL tree class adapted from Weiss.
 * Does NOT allow duplicate elements.
 */
template <typename Comparable>
class AVLTree
{
 public:
  AVLTree( ) : root ( )
  {
  //nothing goes in the main constructor
  }

  AVLTree( const AVLTree & rhs ) : root ()
  {
      copyNodes( rhs.root , root );
  }

  ~AVLTree( )
  {
      makeEmpty( root );
      delete root;
  }

  const AVLTree & operator=( const AVLTree & rhs )
  {

      makeEmpty( root );
      copyNodes( rhs.root , root );
  }

  void printTree( ) const
  {
    printTree( root, 0 );
  }

  void makeEmpty( )
  {
      makeEmpty( root );
  }

  void insert( const Comparable & x )
  {
      insert( x , root );
  }
  // void remove( const Comparable & x );

 private:

  struct AVLNode
  {
    Comparable element;
    AVLNode   *left;
    AVLNode   *right;
    int       height;

  AVLNode( const Comparable & element,
       AVLNode *left,
       AVLNode *right,
       int height = 0 )
  : element( element ), left( left ), right( right ), height( height ) { }
  }; // end of AVLNode

  AVLNode * root;

  void insert( const Comparable & x, AVLNode * & t )
  {

    if( t == NULL )
    {
      //cout << "tnull" <<endl;
      t = new AVLNode( x, NULL, NULL );
    }
    else if( x < t->element )
    {
    //cout << "c1" <<endl;
      insert( x, t->left );
      if( height( t->left ) - height( t->right ) == 2 )
        if( x < t->left->element )
          rotateWithLeftChild( t );
        else
          doubleWithLeftChild( t );
    }
    else if( t->element < x )
    {
     // cout << "c2 " << t->element << " " << x <<endl;
      insert( x, t->right );
      if( height( t->right ) - height( t->left ) == 2 )
        if( t->right->element < x )
          rotateWithRightChild( t );
        else
          doubleWithRightChild( t );
    }
    //cout << "end" << endl;
    // else duplicate; do nothing
    t->height = max( height( t->left ), height( t->right ) ) + 1;
  }

  void makeEmpty( AVLNode * & t )
  {
    if ( t != NULL )
    {
        makeEmpty ( t -> left ) ;
        makeEmpty ( t -> right ) ;
    }
    delete t;
    t = NULL;
  }

  void copyNodes( AVLNode * t , AVLNode * r )
  {
      if ( t != NULL )
      {
          copyNodes( t->left , r );
          copyNodes( t->right, r );
          insert(t->element, r );
          cout << t->element << r->element << endl; //these always print as the same 
      }
  }
#endif

I’m afraid my copy constructor and operator= are not working properly as they do not result in av2 or av3 as being copies of av1. I know that the copyNodes() is working properly because the cout on line 122 reflects that t->element and r->element are the same. Why do lines 22 and 24 of the test program produce no output?

Any help on this would be much appreciated.

note: printTree() is omitted because I know for sure that it is not the problem and it’s a large function.

other note: I have walked through code step by step and examined several other copy constructor/operator= functions for other classes. When I traced through step by step, I result in it working, however it does not when I actually compile 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-14T05:11:25+00:00Added an answer on June 14, 2026 at 5:11 am

    You might be able to fix your code by making the second argument to copy_nodes a reference. In your code when you call insert from within copy_nodes you are not passing in a reference to the root node of your tree but instead a reference to the r parameter of copy_node.

    But I think there’s a much easier (and more efficient, no rebalancing needed) way to do it. Rewrite copy_nodes as a static method which returns the copied nodes.

    static AVLNode * copyNodes( AVLNode * t)
    {
        if ( t != NULL )
        {
            AVLNode* left = copyNodes( t->left );
            AVLNode* right = copyNodes( t->right );
            return new AVLNode(t->element, left, right, t->height);
        }
        else
        {
            return NULL;
        }
    }
    

    You can then use this method in your copy constructor like this

    AVLTree( const AVLTree & rhs )
    {
        root = copyNodes( rhs.root );
    }
    

    similarly for the assignment operator.

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

Sidebar

Related Questions

#include<iostream> #include<fstream> #include<cstdlib> #include<iomanip> using namespace std; int main() { ifstream in_stream; // reads
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; const int FILENAME_MAX=20; int main()
This is a small program: #include <iostream> #include <cstdlib> using namespace std; int main()
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main() { fstream inOutGrades(
i have following code #include <iostream> #include <cstdlib> using namespace std; int main() {
#include<iostream> #include<fstream> #include<cstdlib> #include<string> using namespace std; **int main() { double write(); double read();
Take the following program: #include <cstdlib> using std::rand; #include <iostream> using std::cout; int main()
#include <iostream> using namespace std; int main() { int a, b, c, max; cout<<a=;
#include <iostream> #include <string.h> using namespace std; int main() { int e=0; int b=0;
#include <iostream> #include <iomanip> using namespace std; int main() { char array[10]; for(int i

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.