Search code examples
c++pass-by-referencetrie

Pass by reference issue in passing the nodes in the function


#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <string>

using namespace std;

struct node {
        bool indicator;
        map<int, struct node*> vec; 
};

node * create_node () {
        node *newnode = new node();
        newnode -> indicator = 0;
        for(int i = 1; i <= 26; i++)
            newnode -> vec[i] = NULL;
        return newnode;
}

void insert_node (node* insertNode, int str_size, int currentIndex, string str) {
        node *newNode;
        newNode = create_node();
        int int_convert = str[currentIndex] % 96;
        insertNode -> vec[int_convert] = newNode;
        currentIndex++;
        if(currentIndex == str_size) {
            insertNode -> vec[int_convert] -> indicator = 1;
            cout<<"String added in the dictionary with certain insertions\n";
        }
        else
            insert_node(insertNode -> vec[int_convert], str_size, currentIndex, str);
}

void travel (node* currentNode, int str_size, int currentIndex, string str) {
        if(currentIndex != str_size) {
            int int_convert = str[currentIndex] % 96;
            if (currentNode -> vec[int_convert])
                travel(currentNode -> vec[int_convert], str_size, ++currentIndex, str);
            else {
                char c;
                cout<<"String not present\n"<<"Do u want 2 build (Y/N)\n";
                cin>>c;
                if (c == 'Y')
                    insert_node(currentNode, str_size, currentIndex, str);
            }
        }
        else {
            if(currentNode -> indicator == 1)
                cout<<"String FOund in DIctionary\n";
            else {
                cout<<"String not in dictionary BUT can acheived without further insertions\n";
                currentNode -> indicator = 1;
                cout<<"String stored in dictionary\n";
            }
        }
}

int main() {
        int num;
        char C;
        cout<<"enter number of insertions\n";
        scanf("%d", &num);
        node *rootnode;
        rootnode = create_node();
        for(int i =0; i < num; i++) {
            string S;
            cout<<"Enter string\n";
            scanf("%c", &C);
            getline(cin, S);
            travel(rootnode, S.size(), 0, S);
        }
        return 0;
}

In the above code when more than 2 inputs are given then the root of the tree is lost and again the new tree forms from NULL, eliminating the tree formed by previous values, Thus the problem I think is of passing the nodes to functions, the node address is not getting preserved. So kindly figure it out, well this is a program of Trie. for example string INPUT 1 - "top" initially the tree is empty so "top" wont be present, hence top will be inserted in the tree for future traversals. string input 2 - "top" this time it will print "found in dictionary because "top" has been enetered in first input , uptill here program is responding well string input 3 - "top" when third input is again "top", then the output should be again "found in dictionary" but the output is " not found" and "top" is again inserted, so the tree is responding correct for two inputs at a time, means 4th input will be corresponding to 3rd input but it wont consider 1st or 2nd input, similarily on 5th input the tree becomes again empty and string gets inserted and 6th input will hav effect of the presence of 5th input only.


Solution

  • The algorithm itself has no obvious errors. One issue is that you're mixing up iostream and traditional C I/O functions and doing it wrong.

    Lets say I have this input:

    3
    top
    top
    top
    

    The line scanf("%d", &num); reads just the number 3 without a newline character. The first time the loop is entered, scanf("%c", &C); reads the newline and then getline(cin, S); reads the string "top" properly. However, the next time the loop is executed the newline character has already been read by the getline function, as it read the whole line. So the code scanf("%c", &C); instead reads the first character of "top": 't'.

    The correct approach to choose just one of the I/O libraries: either cin/cout, or scanf/printf, and consistently use it everywhere. And to read in a string, just use cin >> s or scanf("%s", s), don't mix it up with getline.