Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Recursive Function to Iterative Function

//Binary Search Tree

#include <iostream>
#include "BinaryNode.h"
using namespace std;

template <class V>
class BinaryTree {
    BinaryNode<V> *root;
    int nodeCount;
public:
    BinaryTree();
    //BinaryTree(const V& newValue);
    bool isEmpty() const;
    int getHeight() const;
    int _getHeight(BinaryNode<V>*) const;
    int getNumberOfNodes() const;
    V getRootData() const;
    void setRootData(const V&);

    bool add(const V&);


    bool remove(const V&);
    BinaryNode<V>* _remove(const V&, BinaryNode<V>*);
    BinaryNode<V>* getInOrderSuccessor(BinaryNode<V>*);


    void clear();
    bool contains(const V&) const;
    bool _contains(BinaryNode<V>*, const V&) const;

    //print tree
    void printPreorder() const;
    void _printPreorder(BinaryNode<V> *curr) const;
    //void printInorder() const;
    //void printPostorder() const;
};

template<class V>
BinaryTree<V>::BinaryTree(){
    root = nullptr;
    nodeCount = 0;
}   
/*
template<class V>
BinaryTree<V>::BinaryTree(const V& newValue){
    root = new BinaryNode<V>(;
    objCount++;
}
*/
template<class V>
bool BinaryTree<V>::isEmpty() const {
    return root == nullptr;
}

template<class V>
int BinaryTree<V>::getHeight() const {
    return _getHeight(root);
}

template<class V>
int BinaryTree<V>::_getHeight(BinaryNode<V>* curr) const{

    if (curr != nullptr) {
        int lHeight = _getHeight(curr->getLeftChildPtr());

        int rHeight = _getHeight(curr->getRightChildPtr());

        return ((lHeight > rHeight) ? lHeight + 1 : rHeight + 1);
    }
    else
        return 0;
}

template<class V>
int BinaryTree<V>::getNumberOfNodes() const {
    return nodeCount;
}


template<class V>
V BinaryTree<V>::getRootData() const {
    if (!isEmpty()) {
        return root->getValue();
    }
}

template<class V>
void BinaryTree<V>::setRootData(const V& newValue) {
    root->setValue(newValue);
}

template<class V>
bool BinaryTree<V>::add(const V& newValue) { // Adds a node
    cout << "adding node..." << endl;
    BinaryNode<V> *curr = root;
    if (!isEmpty()) {
        return _add(newValue, curr);
    }
    else {
        BinaryNode<V> *temp = new BinaryNode<V>(newValue);
        root = temp;
        nodeCount++;
        return true;
    }
}

For my class assignment, we were told to take the add function and change it from recursive to iterative. Does anybody have any suggestions on how I could do this? We are supposed to keep the function the same, but we are able to change its definition. This is just a short snippet of the whole code, but I don’t think the rest is needed for this.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can start with a while loop and 2 pointers like this:

BinaryNode<V> *curr = root;
BinaryNode<V> *prev;
    if (!isEmpty()) {
        while(curr) //while current is not null
          {
            prev=curr;
            if(newValue>curr->getValue())
              {
               curr=curr->right;
              }
             else
             {
              curr=curr->left;
             }
          }
         if (newValue>prev->getValue())
         {
             prev->right=newValue;
         }
         else
         {
         prev->left=newValue;
         }
    }

the getValue() function is the one used in getRootData() to get the data of current node that is being pointed.

I took help from here

Also, its a good practice to google the actual problem first and then come to stack overflow if you don’t find a satisfying answer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading