简体   繁体   English

递归 Function 到迭代 Function

[英]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.对于我的 class 分配,我们被告知要使用 add function 并将其从递归更改为迭代。 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.我们应该保持 function 不变,但我们可以更改其定义。 This is just a short snippet of the whole code, but I don't think the rest is needed for this.这只是整个代码的一小部分,但我认为不需要 rest。

You can start with a while loop and 2 pointers like this:您可以从一个 while 循环和 2 个指针开始,如下所示:

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. getValue() function 是 getRootData() 中用于获取当前被指向节点的数据的方法。

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.此外,如果您没有找到满意的答案,最好先用谷歌搜索实际问题,然后再进行堆栈溢出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM