I need to print the level order traversal of an AVL tree. I know how the idea works using a queue:
enqueue the node, and then enqueue the left and right nodes of it.
every node has a int data which needs to be printed
but then the thing is i don't know how to make queue of object type

void AvlTree<Comparable>::levelorder( )const
{
  AvlNode<int> temp = root;
  Queue<AvlNode> q ;  //it would not allow me to do this
  q.enqueue(root);
  
  while (!q.isEmpty( ))
  {  
    temp = q.dequeue();
    cout<< (q.dequeue())->element;
    if (temp->left !=NULL)
    {  q.enqueue(temp->left);
    }
    if (temp->right !=NULL)
    {  q.enqueue(temp->right);
    }
}

Does your AvlNode class have a public default constructor, public destructor, and public assignment operator? Those are the requirements for storage in a container class.

Does your AvlNode class have a public default constructor, public destructor, and public assignment operator? Those are the requirements for storage in a container class.

well here is the code for AvlNode class...

template <class Comparable>
class AvlNode
{
    Comparable element;
    AvlNode   *left;
    AvlNode   *right;
    int        height;

    AvlNode( const Comparable & theElement, AvlNode *lt, AvlNode *rt, int h = 0 )
      : element( theElement ), left( lt ), right( rt ), height( h ) { }
    friend class AvlTree<Comparable>;
};

but we are not suppose to modify the existing code. How can I get away with the queue? I tried making it an int, but i ended up with seg fault.
Thanks

Probaly way too late for you but you need to have passed the Node by reference not the node itself for the queue. That way you are only dealing with addresses.

>> Queue<AvlNode> q ; //it would not allow me to do this

You meant Queue<AvlNode<int>> q ; //right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.