递归函数到迭代函数

Posted

技术标签:

【中文标题】递归函数到迭代函数【英文标题】:Recursive Function to Iterative Function 【发布时间】:2021-12-31 13:26:29 【问题描述】:
//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;
    

对于我的课堂作业,我们被告知使用 add 函数并将其从递归更改为迭代。有人对我如何做到这一点有任何建议吗?我们应该保持函数不变,但我们可以改变它的定义。这只是整个代码的一个简短的 sn-p,但我认为不需要其余部分。

【问题讨论】:

您是否已经在某处拥有_add 功能?将其转换为迭代方法时遇到什么问题?你了解它的当前实现吗? 我们被告知要删除 _add 函数。我希望更改 add 函数的内部,但不知道从哪里开始。 【参考方案1】:

您可以从一个 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;
         
    

getValue()函数是getRootData()中用来获取当前被指向节点的数据的函数。

我得到了here的帮助

另外,如果没有找到满意的答案,最好先用谷歌搜索实际问题,然后来stack overflow。

【讨论】:

非常感谢。下次我会这样做!

以上是关于递归函数到迭代函数的主要内容,如果未能解决你的问题,请参考以下文章

递归,三目运算,匿名函数,迭代器

好好学python·函数进阶(递归函数,回调函数,闭包函数,匿名函数,迭代器)

函数7—递归

Python 是不是具有用于一阶递归关系的迭代递归生成器函数?

如何在python中将迭代函数转换为递归函数

python--递归函数匿名函数嵌套函数高阶函数装饰器生成器迭代器