-1

Hello it was suggested to me that I learn to write things non-recursively to understand what is going on much more clearly in the program. It is a binary search program that reads a .dat file of a list of presidents and sorts them alphabetically. Here is my code that I want to make non-recursive:

BstNode* Insert(BstNode* root, string data) {

if (root == NULL) {
    root = GetNewNode(data);
}
else if (data <= root->data) {
    root->left = Insert(root->left, data);
}
else {
    root->right = Insert(root->right, data);
}
return root;

}

I want to understand what recursion is and how to look at something recursive and make it non recursive. I'm having trouble understanding these concepts. I'm not sure if I would need to adjust the rest of the binary search tree if I made that portion non-recursive.

Here's the full program in case you need to see what else is going on:

       #include <stdio.h>
       #include <iostream>
       #include <fstream>
       #include <string>
       using namespace std;

struct BstNode {

    string data;
    BstNode* left;
    BstNode* right;

};

BstNode* GetNewNode(string data) {

    BstNode* newNode = new BstNode();
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;

}



BstNode* Insert(BstNode* root, string data) {

    if (root == NULL) {
        root = GetNewNode(data);
    }
    else if (data <= root->data) {
        root->left = Insert(root->left, data);
    }
    else {
        root->right = Insert(root->right, data);
    }
    return root;

}

bool Search(BstNode* root, string data) {

    if (root == NULL) return false;
    else if (root->data == data) return true;
    else if (data <= root->data) return Search(root->left, data);
    else return Search(root->right, data);

}

void Print(BstNode*x) {

    if (!x) return;
    Print(x->left);
    cout << ' ' << x->data << endl;
    Print(x->right);
}

int main() {

    BstNode* root = NULL; //creating an empty tree
    //root = Insert(root, "adam");  test
    //root = Insert(root, "greg");  test
    //root = Insert(root, "tom");   test
    //root = Insert(root, "bill");  test
    //root = Insert(root, "sarah"); test
    //root = Insert(root, "john");  test

    ifstream fin;
    fin.open("prez.dat");
    string currentprez;
    string input = "";

    while (!fin.eof()) {

        fin >> currentprez;
        root = Insert(root, currentprez);
    }

    for (;;) {
            string input = "";
            cout << "Would you like to 'search', 'insert', 'print' or 'quit'?\n";
            cin >> input;

            //if user searches
            if (input == "search" || input == "Search") {

                string searchstr = "";
                cout << "Enter string to be searched: \n";
                cin >> searchstr;
                if (Search(root, searchstr) == true) cout << searchstr + " was Found\n";
                else cout << "Not Found\n";

            } 
            //if user inserts
            else if (input == "insert" || input == "Insert") {

                string insertstr = "";
                cout << "Enter string to be inputted: \n";
                cin >> insertstr;
                if (Search(root, insertstr) == true) cout << insertstr + " already exists\n";
                else root = Insert(root, insertstr);

            }

            //if user prints
            else if (input == "print" || input == "Print") Print(root);
            //if user quits
            else if (input == "quit" || input == "Quit") return(0);
            //if anything else
            else cout << "Invalid input\n";


        }


    }
Adam Bunch
  • 113
  • 1
  • 2
  • 14
  • I don't understand what the question is.. – Eugene Sh. Oct 11 '16 at 18:48
  • Hmm. What is not clear about the question? Can you be specific? – Adam Bunch Oct 11 '16 at 18:50
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [on topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. StackOverflow is not a coding or tutorial service. First and foremost, go through some of the recursion explanations on SO -- or just search for "recursion tutorial". When you have a *specific* question, feel free to post again. – Prune Oct 11 '16 at 19:03
  • By the way, this is *not* a good example for you to learn: tree searches work quite naturally with recursion. – Prune Oct 11 '16 at 19:09
  • I'm aware that tree searches work naturally with recursion. The point is is that you can learn a lot about it by writing it non-recursively. Thanks for the help, though. – Adam Bunch Oct 11 '16 at 19:13

1 Answers1

2

Tree algorithms like this are actually very well suited for recursion, but a way to do it without recursion could be this:

BstNode* Insert(BstNode* root, string data) {
    if (root == null) {
        return GetNewNode(data);
    }    

    BstNode* prev = null;
    BstNode* pos = root;
    while (pos != null) {
        prev = pos;
        if (data <= pos->data) {
            pos = pos->left;
        }
        else {
            pos = pos->right;
        }
   }
   if ( data <= prev -> data) {
       prev -> left = GetNewNode(data);
   }else {
       prev -> right = GetNewNode(data);
   }

   return root;
}
user3115197
  • 121
  • 6
  • Thanks! This makes sense. How would you do it for search though without doing it recursively? bool Search(BstNode* root, string data) { if (root == NULL) return false; else if (root->data == data) return true; else if (data <= root->data) return Search(root->left, data); else return Search(root->right, data); } – Adam Bunch Oct 11 '16 at 19:33
  • @Adam It would be very similar: `bool Search(BstNode* root, string data) { if (root == null) { return false; } BstNode* pos = root; while (pos != null) { if (data < pos->data) { pos = pos->left; } else if (data > pos-> data){ pos = pos->right; } else { return true } } return false; }` – user3115197 Oct 11 '16 at 21:53