Binary Search Tree - Printinorder();

Asked

Viewed 102 times

3

My BST (templated class) has function PrintInOrder(), that does not receive parameters:

void BST<Type>::printInOrder() const{}

I’ve been looking on the Internet, and I found some InOrder(), but they get a Node* for the root as parameter, which makes sense.

How could I make a recursion in the function I quoted without passing parameters?
There’s a way I can create a private member in class to help with that?

My BST class:

struct Node
{
    Type m_data;
    Node *m_left, *m_right;

    Node(const Type& _data) : m_data(_data), m_left(nullptr), m_right(nullptr) {}
};

Node* m_root;
int m_size;

public:
    BST();
    ~BST();
    BST& operator=(const BST& that);
    BST(const BST& that);;
    void insert(const Type& v);
    bool findAndRemove(const Type& v);
    bool find(const Type& v) const;
    void clear();
    void printInOrder() const;
};
  • 1

    What you can do is create a private method that needs parameter (and implement it recursively) and in your public method you call this private by passing the m_root

1 answer

3


What I said in my comment would be this:

struct Node
{
    Type m_data;
    Node *m_left, *m_right;

    Node(const Type& _data) : m_data(_data), m_left(nullptr), m_right(nullptr) {}
};

Node* m_root;
int m_size;
void printInOrderRecursive(Node* root);

public:
    BST();
    ~BST();
    BST& operator=(const BST& that);
    BST(const BST& that);;
    void insert(const Type& v);
    bool findAndRemove(const Type& v);
    bool find(const Type& v) const;
    void clear();
    void printInOrder();
};

Since the implementation of void printInOrder(); would be:

void printInOrder() {
    printInOrderRecursive(m_root);
}

I did not put the implementation of void printInOrderRecursive(Node* root);, because you said in the question have already found.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.