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

The Archive Base Latest Questions

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

I am getting a few errors in my Binary Search Tree print code and

  • 0

I am getting a few errors in my Binary Search Tree print code and I do not know why. (Errors at the bottom)

#ifndef MYBSTREE_H
#define MYBSTREE_H

#include "abstractbstree.h"
#include "abstractstack.h"
using namespace std;

class LinkedStack *head = NULL;           //LINE 7

template<typename T>
class TreeNode
{
  public:
    T m_data;
    TreeNode* m_right;
    TreeNode* m_left;

};


template<typename T>
class LinkedStack: public AbstractStack<TreeNode<T>*>
{
  public:
    TreeNode<T>* m_data;
    LinkedStack *m_next;

    LinkedStack()
    {
      if(head != NULL)
        head = new LinkedStack;
      m_data = 0;
      m_next = NULL;
    }

    void clear()
    {
      while(head -> m_next != NULL)
      {
        LinkedStack *tmp = head -> m_next;
        head -> m_ldata= tmp -> m_ldata;
        head -> m_next = tmp -> m_next;
        delete tmp;
      }
    }


    void push(TreeNode<T>* x)
    {
      LinkedStack *tmp = new LinkedStack;
      tmp -> m_data = m_data;
      tmp -> m_next = m_next;
      m_data = x;
      m_next = tmp;
    }

    void pop()
    {
      if (isEmpty())
        cout<<"Panic! Nothing to pop"<<endl;
      else
      {
        LinkedStack *tmp;
        tmp = m_next;
        m_data = tmp -> m_data;
        m_next = tmp -> m_next;
        delete tmp;
      }
    }

    int& top()
    {
      if (isEmpty())
        cout<<"Panic! List is Empty"<<endl;

        return m_ldata;
    }

    bool isEmpty()
    {
      bool empty = false;

      if (m_next == NULL)
      {
        empty = true;
      }

      return empty;
    }
};

template<typename T>
class MyBSTree:public AbstractBSTree<T>
{
  private:

    TreeNode<T>* m_root;

  public:

    ~MyBSTree()  
    {
      clear();
    }

    MyBSTree()
    {
      m_root -> m_data = T();
      m_root -> m_right = NULL;
      m_root -> m_left = NULL;
    };

    int size() const
    {
        if(m_root==NULL)
          return 0;
        else
          return (size(m_root->m_left)+1+size(m_root->m_right));
    }

    bool isEmpty() const
    {
      if (m_root== NULL)
        return true;

      else
        return false;
    }

    int height() const
    {
      int lh,rh;
      if(m_root==NULL)
      {
        return 0;
      }
      else
      {
        lh = height(m_root->m_left);
        rh = height(m_root->m_right);

        if(lh > rh)
          return lh + 1;
        else
          return rh + 1;
      }
    }

    const T& findMax() const
    {
      TreeNode<T>* p = m_root;
      while(p -> m_right != NULL)
        p = p -> m_right;
      return p;
    }

    const T& findMin() const
    {
      TreeNode<T>* p = m_root;
      while(p -> m_left != NULL)
        p = p -> m_left;
      return p;
    }
/*
    int contains(const T& x) const;
*/
    void clear()
    {
      delete m_root;
    }

    void insert(const T& x)
    {
      TreeNode<T>* p = m_root;

      do
      {  
        if (x == p -> m_root)
          return;
        else if ( x < p->m_data)
        {
          if(p->m_left == NULL)
          {
              p->m_left = new TreeNode<T>;
              p -> m_left -> m_data = x;
              return;
          }
          else
            p = p -> m_left;
        }
        else if( x > p->m_data)
        {
          if(p->m_right == NULL)
          {
            p->m_right = new TreeNode<T>;
            p-> m_right -> m_data = x;
            return;
          }
          else
            p = p -> m_right;
        } 
      }while(p -> m_right != NULL && p -> m_left != NULL);
    }

    void remove(const T& x)
    {
        if(m_root == NULL)
            return;

        TreeNode<T>* q = m_root;
        TreeNode<T>* p;

        while(q != NULL)
        {
            if(m_root->m_data == x)
            {
                delete q;
                return;
            }
            else if(x > q->m_data)
            {
                p = q; 
                q = q->m_right;
            }
            else
            {
                p = q; 
                q = q->m_left;            
            }
        }

        if(q->m_left == NULL && q->m_right == NULL)
        {
            if(p == q)
                delete p;
            else if(p->m_left == q)
            {
                p->m_left = NULL;
                delete q;
            }
            else 
            {
                p->m_right = NULL;
                delete q;
            }
            return;
        }

        if((q->m_left == NULL && q->m_right != NULL) || (q->m_left != NULL && q->m_right == NULL))
        {
            if(q->m_left == NULL && q->m_right != NULL)
            {
                if(p->m_left == q)
                {
                    p->m_left = q->m_right;
                    delete q;
                }
                else
                {
                    p->m_right = q->m_right;
                    delete q;
                }
            }
            else
            {
                if(p->m_left == q)
                {
                    p->m_left = q->m_left;
                    delete q;
                }
                else
                {
                    p->m_right = q->m_left;
                    delete q;
                }
            }
            return;
        }

        if (q->m_left != NULL && q->m_right != NULL)
        {
            TreeNode<T>* check;
            check = q->m_right;
            if((check->m_left == NULL) && (check->m_right == NULL))
            {
                q = check;
                delete check;
                q->m_right = NULL;
            }
            else
            {
                if((q->m_right)->m_left != NULL)
                {
                    TreeNode<T>* m_leftq;
                    TreeNode<T>* m_leftp;
                    m_leftp = q->m_right;
                    m_leftq = (q->m_right)->m_left;
                    while(m_leftq->m_left != NULL)
                    {
                        m_leftp = m_leftq;
                        m_leftq = m_leftq->m_left;
                    }
                    q->data = m_leftq->data;
                    delete m_leftq;
                    m_leftp->m_left = NULL;
                }
                else
                {
                    TreeNode<T>* tmp;
                    tmp = q->m_right;
                    q->data = tmp->data;
                    q->m_right = tmp->m_right;
                    delete tmp;
                }
            }
            return;
        }
    }

    void printPreOrder() const
    {
      LinkedStack stack;
      stack<T>.push(m_root);                           //THIS LINE
      while(!stack.isEmpty())                          //THIS LINE
        {
          TreeNode<T>* root = stack.push();        //THIS LINE
          stack.pop();                             //THIS LINE
          if(root!= NULL)
          {
            stack.push(root->right);           //THIS LINE
            stack.push(root->m_left);          //THIS LINE
            cout << root->m_data << " ";
          }

        }

    }
/*
    void printPostOrder() const;

    void print() const;
    */
};


#endif

What I am getting are errors that say:

MyBSTree.h: In member function âvoid MyBSTree<T>::printPreOrder() constâ:
MyBSTree.h:362:9: error: invalid use of incomplete type âstruct LinkedStackâ
MyBSTree.h:7:7: error: forward declaration of âstruct LinkedStackâ

I get that for all the lines I have marked in that particular function. I’m pretty sure it has something to do with template syntax, but I’m not sure.

  • 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-14T03:11:55+00:00Added an answer on June 14, 2026 at 3:11 am

    Line class LinkedStack *head = NULL; has no sense. If you want to have a head to your LinkedStack, you should:

    1. drop the class in this line
    2. pass correct type since it’s a template
    3. do it after the templated class definition

    Something like this: LinkedStack<your type> *head = NULL;

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

Sidebar

Related Questions

I used this code for validating email id , im getting few errors i
Getting a few errors and not sure what to do. Please help. Trying to
I am trying to insert node in a binary search tree, I am getting
I am getting a few errors that I don't know about and have spent
Not very good at debugging yet but I'm getting a few errors. A few
I'm getting quite a few errors in my code. Consequently, I would like to
I have few jar files which I am not getting from any repositories.I have
I'm currently getting to know Java and OSGi, so I've read a few books.
Ok, I'm a bit closer but still getting a few errors. Netbeans is telling
I've noticed I'm getting a few errors at random points in my app. I've

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.