Home > Enterprise >  Recursive Function to Iterative Function
Recursive Function to Iterative Function

Time:11-22

//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.

CodePudding user response:

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.

  • Related