Answer to Question #296998 in C++ for Roe

Question #296998

class Node


{


public int key;


public Object data;


public Node left;


public Node right;


}



Node* search(Node root, int key)


{


if (root == null)


return null;


else if (key == root->key)


return root->data;


else if (key < root->key)


return searchTree(root->left, key);


else


return searchTree(root->right, key);


}



Rewrite this function iteratively instead of recursion



Requirements :


No global decelerations


Test run in main




1
Expert's answer
2022-02-12T11:07:48-0500
class Object;


class Node
{
public:
    int key;
    Object* data;
    Node* left;
    Node* right;
};


Object* search(Node* root, int key)
{
    while (root != nullptr) {
        if (key == root->key) {
            return root->data;
        }


        if (key < root->key) {
            root = root->left;
        }
        else {
            root = root->right;
        }


    }
    return nullptr;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog