簡體   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;
    }
}

對於我的 class 分配,我們被告知要使用 add function 並將其從遞歸更改為迭代。 有人對我如何做到這一點有任何建議嗎? 我們應該保持 function 不變,但我們可以更改其定義。 這只是整個代碼的一小部分,但我認為不需要 rest。

您可以從一個 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() function 是 getRootData() 中用於獲取當前被指向節點的數據的方法。

我從這里得到幫助

此外,如果您沒有找到滿意的答案,最好先用谷歌搜索實際問題,然后再進行堆棧溢出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM