Re: [algogeeks] Suggested the Data Structure to implement the solution in O(1)
If you are using Java, you can go with LinkedHashMap. On Sat, Jan 5, 2013 at 6:40 PM, Nishant Pandey nishant.bits.me...@gmail.com wrote: Give a Data structure to store Name-value pair like name-age abc,12 xyz,34... such than insert(name,value), value = search(name), name = nthentry(n), delete(name); all can be perfomed in O(1). Note:- after deletion order should be maintained.Ex. ds,12 df,78 teu,54 etr,12 If delete(df) is called then nthentry(2) should return teu Please suggest the solution .. -- -- Regards, Sachin Maheshwari Cell phone: +91.7259917298 If we admit that human life can be ruled by reason; the possibility of life is destroyed. - Alexander Supertramp --
Re: [algogeeks] Trie Implementaion Issues
Hi Aditya, In C++ member variables gets initialized to garbage values. So in child function T-Symbol is garbage and is not null so it returns because of null check. Next in your insert method you try to access that garbage value. This will cause a crash. When you remove the cmnt1, you intialize symbol field properly and hence you see no crash. As a general practice, always initialize member variables in C++ to some value. I believe that was your intention of calling child method from makeTrie function. Regards, Sachin On Sun, Dec 23, 2012 at 5:48 AM, Aditya Raman adityarareloa...@gmail.comwrote: Hello everyone, I was trying to implement Trie in c++ for the first time and got some issues in coding its implementation. The code has 2 commented lines namely cmnt1 and cmnt 2. I dont understand how does it make a difference if i use cmnt1 instead of cmnt2 . Both lines are intended to check if a node in the Trie has childs never initialized before. - Code Below --- #include fstream #include iostream #include cstdio #include algorithm #include cstring #include string #include cctype #include stack #include queue #include list #include vector #include map #include sstream #include cmath #include bitset #include utility #include set #include numeric using namespace std; struct TrieNode { bool leaf; TrieNode* symbol; }; void child(TrieNode* T) { cmnt1 if( T-symbol != NULL)return; /// T-symbol=(TrieNode*)malloc(26*sizeof(TrieNode)); for(int i=0;i26;i++) { T-symbol[i].leaf=false; T-symbol[i].symbol=NULL; } } TrieNode* makeTrie( ) { TrieNode* T=(TrieNode*)malloc(sizeof(TrieNode)); T-leaf=false; child(T); return T; } void insert(string A,TrieNode* root) {TrieNode* temp=root; int i; for(i=0;iA.length()-1;i++) {temp=(temp-symbol[A[i]-'A']); //cmnt2// if( temp-symbol== NULL) / child(temp); } temp=(temp-symbol[A[i]-'A']); temp-leaf=true; } vectorchar display; void print(TrieNode* T) { if(T-leaf==true){ for(int i=0;idisplay.size();i++)coutdisplay[i] ;coutendl; } if(T==NULL)return; if(T-symbol!=NULL){ for(int i=0;i26;i++) { display.push_back('A'+i); print(T-symbol[i]); display.pop_back(); } } } int main() { TrieNode* root=makeTrie(); insert(ADI,root); insert(CAT,root); insert(ADII,root); print(root); // system(pause); } -end of code- if i remove code of cmnt 1(like in the pasted code) and directly use the code of cmnt 2 then trie code runs smooth .But if do the opposite case ,which is logically the same ,the code fails to run. kindly help! -- -- Regards, Sachin Maheshwari Cell phone: +91.7259917298 If we admit that human life can be ruled by reason; the possibility of life is destroyed. - Alexander Supertramp --
Re: [algogeeks] perfect square condition checking....
Are you asking for an algorithm for checking perfect square condition without using library functions like sqrt? On Sun, Dec 23, 2012 at 9:07 PM, Anil Sharma anilsharmau...@gmail.comwrote: please suggest some efficient solution to check perfect square condition . no matter how much large number is... eg..i/p-8949 o/p-93 -- -- Regards, Sachin Maheshwari Cell phone: +91.7259917298 If we admit that human life can be ruled by reason; the possibility of life is destroyed. - Alexander Supertramp --