Cursor not moving in tree insert function, found in provided header file that has constructor and class definition. How can I move the cursor forward? - c++

The assignment requires that I write the code for an Insert, Print and Find functions in the already provided tree header file. It's a very simple search tree for ints. Here is the header file:
#include <map>
#include <iostream>
class Tree {
Tree *left;
Tree *right;
int node;
static std::map<int, Tree*> allNodes;
public:
Tree(int n, Tree *l=0, Tree *r=0) : left(l), right(r), node(n) {
allNodes[n] = this;
}
int GetNode() { return node; }
void Insert(Tree *newnode) {
// insert code to Insert newnode into tree pointed to by this... return the tree
// My own code // cout<<this->node;
if( newnode->node == this->node ){}
// skip dup
//Everything after is also my code
else if (newnode->node < this->node){
left = newnode;
cout<<"left"<<left->node<<endl;
}
else if (newnode->node > this->node){
right = newnode;
cout<<"right"<<right->node<<endl;
}
}
Here is the code that implements it(I wrote the code at the very bottom):
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include "trav.h"
std::map<int, Tree*> Tree::allNodes;
// trav print
// trav find X
int
main(int argc, char *argv[])
{
if( argc < 2 )
return 0;
string cmd(argv[1]);
// READ IN THE INTEGERS
vector<int> ids;
int input;
while( cin >> input ) {
ids.push_back( input );
}
// MAKE NODES FROM THE INTEGERS
vector<Tree*> nodes;
for( int id: ids ) {
nodes.push_back( new Tree(id) );
}
if( ids.size() == 0 )
return -1;
// PUT NODES INTO A TREE USING Insert
Tree *theTree = nodes[0];
if( theTree == nullptr )
return -1;
for( auto n : nodes ) {
//My code// theTree->Insert(n);
}
// PRINT TREE
if( cmd == "print" ) {
theTree->Print();
}
/*if (cmd == "find") {
string no = argv[2];
int num = atoi(no);
int result = theTree-> find(num);
if(result!=0)
cout<<num<<endl;
else
cout<<"-1"<<endl;
}*/
return 0;
}
Here is my output:
3232left8
32left3
32left5
32left9
32right40
32right100
32right632
32left1
32left9
32left9
32left3
32left2
32right64
32right43
2
43
32
Those last 3 numbers are from my print function.
The cursor doesn't appear to be moving forward. I've tried setting this=left, which returns an error. What can I do? And I'm not sure how to use the allNodes. Do I implement it in the insert function?

Related

Assigning pointer

I have been working on this for a while and cannot seem to understand what is happening. I am trying to take the values in istr, put them in a linked list and sort them alphabetically. Eventually I will print them out. I am not sure where my problem is but I thought it was in the function InsertAfter. Is this not my problem and if so do you know what may be causing my linked list to not link? The last bit of code only outputs the headObj and not all of them, so I assumed that my list wasn't linking properly in nextNodePtr in each object but I am not sure. Thank you for your help!
void WordNode::InsertAfter(WordNode* nodeLoc) {
WordNode* tmpNext = 0;
tmpNext = this->nextNodePtr; // Remember next
this->nextNodePtr = nodeLoc; // this -- node -- ?
nodeLoc->nextNodePtr = tmpNext; // this -- node -- next
return;
}
wordNode.hpp
#ifndef wordNode_hpp
#define wordNode_hpp
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class WordNode {
public:
WordNode(string wordval = "", int count = 0, WordNode* nextLoc = 0);
void InsertAfter(WordNode* nodePtr);
WordNode* GetNext();
void PrWordNodeData();
string GetWord();
private:
string word;
WordNode* nextNodePtr;
int wordCount;
};
wordNode.cpp
#include "wordNode.hpp"
// Constructor
WordNode::WordNode(string wordval,int count, WordNode* nextLoc) {
this->word = wordval;
this->wordCount = count;
this->nextNodePtr = nextLoc;
return;
}
/* Insert node after this node.
* Before: this -- next
* After: this -- node -- next
*/
void WordNode::InsertAfter(WordNode* nodeLoc) {
WordNode* tmpNext = 0;
tmpNext = this->nextNodePtr; // Remember next
this->nextNodePtr = nodeLoc; // this -- node -- ?
nodeLoc->nextNodePtr = tmpNext; // this -- node -- next
return;
}
// Print dataVal
void WordNode::PrWordNodeData() {
cout << this->word <<": count=" <<this->wordCount << endl;
return;
}
// Grab location pointed by nextNodePtr
WordNode* WordNode::GetNext() {
return this->nextNodePtr;
}
//Returns word
string WordNode::GetWord()
{
return word;
}
main.cpp
#include <iostream>
#include <sstream>
#include <string>
#include "wordNode.hpp"
int main() {
WordNode* headObj = 0; // Create WordNode objects
WordNode* currObj = 0;
WordNode* nextObj = 0;
string istr ="555 999 777 333 111";
istringstream instring(istr);
string temp;
//Assigns first word to the head object
if (!instring.eof()){
instring >> temp;
headObj=new WordNode(temp,1);
}
currObj=headObj;
while (!instring.eof()){
instring >> temp;
nextObj=new WordNode(temp,1);
//swaps values if currObj is greater than the next word
if(currObj > nextObj) {
currObj->InsertAfter(nextObj);
}
currObj=nextObj;
}
// Print linked list
currObj = headObj;
while (currObj != 0) {
currObj->PrWordNodeData();
currObj = currObj->GetNext();
}
string i;
cin >> i;
return 0;
}
In the very first iteration of the loop (using the string you gave as example) you loose the reference to the head object and hence subsequent iterations will add nodes to a "headless list".
currObj=headObj;
while (!instring.eof()){
instring >> temp;
nextObj = new WordNode(temp,1);
//swaps values if currObj is greater than the next word
if(currObj->GetWord() > nextObj->GetWord()) {
currObj->InsertAfter(nextObj);
}
// And what happens if it is not greater?
currObj = nextObj; // Loose reference to head here if not greater
}
To fix your code you will either just have to add all nodes to the list and then sort it with a sorting algorithm or insert them on the fly as you intend to do now. However, to do the latter you will have to modify your insertion logic, i.e. insert node at the beginning (if new node is alphabetically lower than the first element) or at the end. I recommend reading this nice article about singly linked lists. It has examples and code for the insertions mentioned.

How to sort alphabetically ordered BST as frequency ordered tree?

According to my homework assignment i am working on Binary Trees. The assignment is simple that reading words from in input text file and creating a BST that includes frequency data on it.
I've simply searched on it and created an implementation of alphebetically ordered BST and it is quite working for me.
But the question that i've been struggled for is that, Rank each unique word in descending order of frequency.
The tree is using alpabetical ordering by comparing strings and create nodes related to that... So how may i keep going with their frequencies ? Should i create a new tree as frequency balanced by using alphetically ordered tree elements ?
Any helps would be appreciated, many thanks in advance!
But how should i keep the count
TreeNode.h :
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
class treeNode
{
public:
char data[55];
int count;
struct treeNode *leftPtr, *rightPtr;
};
typedef struct treeNode TreeNode;
typedef TreeNode *TreeNodePtr;
void insertNode(TreeNodePtr *treePtr, char word[]);
void alphabetic(TreeNodePtr treePtr);
int sizeOfTree(TreeNodePtr treePtr);
TreeNode.cpp :
# include "treeNode.h"
void::insertNode(TreeNodePtr *treePtr, char word[55]){
TreeNode *temp = NULL;
if (*treePtr == NULL)
{
temp = (TreeNode *)malloc(sizeof(TreeNode));
temp->count = 1;
temp->leftPtr = NULL;
temp->rightPtr = NULL;
strcpy(temp->data, word);
*treePtr = temp;
}
else if (strcmp(word, (*treePtr)->data) < 0)
{
insertNode(&((*treePtr)->leftPtr), word);
}
else if (strcmp(word, (*treePtr)->data) > 0)
{
insertNode(&((*treePtr)->rightPtr), word);
}
else
{
(*treePtr)->count += 1;
}
}
void::alphabetic(TreeNodePtr treePtr)
{
if (treePtr != NULL)
{
alphabetic(treePtr->leftPtr);
printf("%s\t", treePtr->data);
printf("%d\n", treePtr->count);
alphabetic(treePtr->rightPtr);
}
}
int::sizeOfTree(TreeNodePtr treePtr){
if (treePtr == NULL)
return 0;
else
return(sizeOfTree(treePtr->leftPtr) + 1 + sizeOfTree(treePtr->rightPtr));
}
Main function :
int main()
{
/*reading strings from the file and add them to the tree*/
int totalSize = 0;
char first[55];
FILE *fp1;
TreeNodePtr rootPtr = NULL;
int c;
//fp1 = fopen("%FILENAME%", "r");
fp1 = fopen("FILENAME%", "r");
do
{
c = fscanf(fp1, "%s", first);
if (c != EOF)
{
//printf(first);
insertNode(&rootPtr, first);
}
} while (c != EOF);
fclose(fp1);
//printf("%s", rootPtr->rightPtr->leftPtr->data);
alphabetic(rootPtr);
printf("%d\n",sizeOfTree(rootPtr));
system("PAUSE");
return 0;
}
UPDATE : I am direclty asked for to use BST as data structure, none of other map, hash or C++ STL structures shouldn't be used.
You are not asked to rearrange the nodes in-tree. You are asked to extract the words, most common first.
The principle is quite simple. One map for each task, something like:
std::map<string, int> wordmap; // this is your bst.
while(in>>word){
wordmap[word]++;
}
std::map<int, string, std::greater<int>> countmap;
for(auto word_and_count: wordmap){
countmap[wordmap.second] = wordmap.first;
}
Note that this is not working code. It is intended to show the process.

Hash table implementation in C++

I am trying the following code for Hash table implementation in C++. The program compiles and accepts input and then a popup appears saying " the project has stopped working and windows is checking for a solution to the problem. I feel the program is going in the infinite loop somewhere. Can anyone spot the mistake?? Please help!
#include <iostream>
#include <stdlib.h>
#include <string>
#include <sstream>
using namespace std;
/* Definitions as shown */
typedef struct CellType* Position;
typedef int ElementType;
struct CellType{
ElementType value;
Position next;
};
/* *** Implements a List ADT with necessary functions.
You may make use of these functions (need not use all) to implement your HashTable ADT */
class List{
private:
Position listHead;
int count;
public:
//Initializes the number of nodes in the list
void setCount(int num){
count = num;
}
//Creates an empty list
void makeEmptyList(){
listHead = new CellType;
listHead->next = NULL;
}
//Inserts an element after Position p
int insertList(ElementType data, Position p){
Position temp;
temp = p->next;
p->next = new CellType;
p->next->next = temp;
p->next->value = data;
return ++count;
}
//Returns pointer to the last node
Position end(){
Position p;
p = listHead;
while (p->next != NULL){
p = p->next;
}
return p;
}
//Returns number of elements in the list
int getCount(){
return count;
}
};
class HashTable{
private:
List bucket[10];
int bucketIndex;
int numElemBucket;
Position posInsert;
string collision;
bool reportCol; //Helps to print a NO for no collisions
public:
HashTable(){ //constructor
int i;
for (i=0;i<10;i++){
bucket[i].setCount(0);
}
collision = "";
reportCol = false;
}
int insert(int data){
bucketIndex=data%10;
int col;
if(posInsert->next==NULL)
bucket[bucketIndex].insertList(data,posInsert);
else { while(posInsert->next != NULL){
posInsert=posInsert->next;
}
bucket[bucketIndex].insertList(data,posInsert);
reportCol=true;}
if (reportCol==true) col=1;
else col=0;
numElemBucket++;
return col ;
/*code to insert data into
hash table and report collision*/
}
void listCollision(int pos){
cout<< "("<< pos<< "," << bucketIndex << "," << numElemBucket << ")"; /*codeto generate a properly formatted
string to report multiple collisions*/
}
void printCollision();
};
int main(){
HashTable ht;
int i, data;
for (i=0;i<10;i++){
cin>>data;
int abc= ht.insert(data);
if(abc==1){
ht.listCollision(i);/* code to call insert function of HashTable ADT and if there is a collision, use listCollision to generate the list of collisions*/
}
//Prints the concatenated collision list
ht.printCollision();
}}
void HashTable::printCollision(){
if (reportCol == false)
cout <<"NO";
else
cout<<collision;
}
The output of the program is the point where there is a collision in the hash table, thecorresponding bucket number and the number of elements in that bucket.
After trying dubbuging, I come to know that, while calling a constructor you are not emptying the bucket[bucketIndex].
So your Hash Table constructor should be as follow:
HashTable(){ //constructor
int i;
for (i=0;i<10;i++){
bucket[i].setCount(0);
bucket[i].makeEmptyList(); //here we clear for first use
}
collision = "";
reportCol = false;
}
//Creates an empty list
void makeEmptyList(){
listHead = new CellType;
listHead->next = NULL;
}
what you can do is you can get posInsert using
bucket[bucketIndex].end()
so that posInsert-> is defined
and there is no need to
while(posInsert->next != NULL){
posInsert=posInsert->next;
because end() function is doing just that so use end() function

Decoder implementing a stack as a linked structure

This program I am writing will use a special implementation of a stack as a linked structure. An encoded message input my the user will be parsed and decoded using the stack. What I have written compiles find and runs without crashing. The program asks the user for the string to be decoded. However, the encoded message is not decoded with result printed on the screen. I can't figure out why my program isn't decoding and printing the user's input. Any help is greatly appreciated. Thanks.
My header file :
#ifndef DECODER_H
#define DECODER_H
#include <iostream>
#include <stdlib.h>
using namespace std;
// ---------------------------
// Structure which will serve
// as the link on the stack.
// ---------------------------
struct StackNode {
char ch;
StackNode* next;
};
// -------------------------------
// Class which will contains the
// functions for appropriate use
// of the stack.
// -------------------------------
class Decoder
{
private:
StackNode* top;
public:
Decoder();
~Decoder();
int EmptyStack();
int FullStack();
void Push(char ch);
char Pop();
void Decode(char *encMsg, char *decMsg);
};
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif
#endif // End of stack header.
My .cpp file:
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "Decoder.h"
// ------------------------------
// Function: Decoder()
//
// Purpose: Class constructor.
// ------------------------------
Decoder::Decoder()
{
top = NULL;
}
// ------------------------------
// Function: Decoder()
//
// Purpose: Class destructor.
// ------------------------------
Decoder::~Decoder()
{
// TODO
// Destroy anything remaining in the stack
}
// -----------------------------------
// FullStack()
//
// Return TRUE if the stack is full.
// -----------------------------------
int Decoder::FullStack()
{
return TRUE;
}
// -----------------------------------
// EmptyStack()
//
// Return TRUE if the stack is empty
// -----------------------------------
int Decoder::EmptyStack()
{
return (top == NULL);
}
// ------------------------------------------------
// Function: void Push(char ch)
//
// Purpose: Dynamically creates a structure of type
// StackNode (see Decoder.h), stores the character
// in the structure and pushes the structure onto
// the stack.
// ------------------------------------------------
void Decoder::Push(char ch)
{
// Make a new node whose reference is
// the existing list
StackNode* newNode = new (StackNode);
newNode->ch = ch;
// newNode->next = NULL;
if (top == NULL)
top = newNode; // top points to new node
else
{
newNode->next = top;
top = newNode;
}
}
// --------------------------------------------------
// Function: char Pop()
//
// Purpose: Remove (pop) the top node from the stack,
// copy the character, from this node, delete and
// return the character.
// --------------------------------------------------
char Decoder::Pop()
{
StackNode* temp;
char ch;
if (!EmptyStack())
{
ch = top->ch;
temp = top;
top = top->next;
delete(temp);
return ch;
}
else {
cout << "Warning: Overuse of Pop()" << endl;
return '\0';
}
}
// ----------------------------------------------------
// Function: void Decode(char* encMsg, char* decMsg)
//
// Purpose: Parse and decode the message stored in the
// character array encMsg using the stack functions
// and return the decoded message in the char array
// decMsg.
// ----------------------------------------------------
void Decoder::Decode(char* encMsg, char* decMsg)
{
int StackCount = 0;
char num[2] = " ";
for (int i = 0; i < strlen(encMsg); i++)
{
// check whether 1 is an even number of input
if ((encMsg[i] == '1') && (encMsg[i-1] != '2')) // every other index will be a command number
{
Push(encMsg[i+1]);
StackCount++;
}
if (encMsg[i] == '2' && ((encMsg[i+1] >= '0') && (encMsg[i+1 ] <= '9'))) // every other index will be a command number
{
num[0] = encMsg[i+1];
// pop as many as the argument states to pop
for (int j = 0; j < atoi(num); j++)
{
Pop();
StackCount--;
}
}
}
//cout << StackCount << endl;
// Place the remaining characters from the stack into decMsg
int i;
for (i = 0; i < StackCount; i++)
{
decMsg[i] = Pop();
}
decMsg[i] = '\0';
return;
}
My Main .cpp:
#include <iostream>
#include <string>
#include "Decoder.h"
using namespace std;
int main (void)
{
char quit[] = "QUIT";
char en[2048];
char dec[512];
Decoder d;
do {
cout << "\nEnter a message to be decoded" << endl;
cin.getline(en, 1024);
d.Decode(en, dec);
cout << dec << endl;
} while (strcmp(en,quit) != 0);
return 0;
}
This line of code
if ((encMsg[i] == '1') && (encMsg[i-1] != '2'))
Maybe a problem there when i is zero.
It is guaranteed to try endMsg[-1] every time since i=0 is followed immediately by encMsg[i-1] which is always checked since && is present.
for (int i = 0; i < strlen(encMsg); i++)
{
// check whether 1 is an even number of input
if ((encMsg[i] == '1') && (encMsg[i-1] != '2')) // every other index will be a command number
{

What's the correct approach to solve SPOJ www.spoj.com/problems/PRHYME/?

I have been trying to solve this problem SPOJ www.spoj.com/problems/PRHYME/? for several days, but have had no success.
Here is the problem in brief:
Given is a wordlist L, and a word w. Your task is to find a word in L that forms a perfect rhyme with w. This word u is uniquely determined by these properties:
It is in L.
It is different from w.
Their common suffix is as long as possible.
Out of all words that satisfy the previous points, u is the lexicographically smallest one.
Length of a word will be<=30.
And number of words both in the dictionary and the queries can be 2,50,000.
I am using a trie to store all the words in the dictionary reversed.
Then to solve the queries I proceed in the following fashion:-
If word is present in the trie,delete it from trie.
Now traverse the trie from the root till the point the character from the query string match the trie values.Let this point where last character match was found be P.
Now from this point P onward ,I traverse the trie using DFS,and on encountering a leaf node,push the string formed to the possible results list.
Now I return the lexicographic ally smallest result from this list.
When I submit my solution on SPOJ,my solution gets a Time Limit Exceeded Error.
Can someone please suggest a detailed algorithm or hint to solve this problem ?
I can post my code if required.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<climits>
#include<vector>
#include<string>
#include<algorithm>
#include<cctype>
#include<cstdlib>
#include<utility>
#include<map>
#include<queue>
#include<set>
#define ll long long signed int
#define ull unsigned long long int
const int alpha=26;
using namespace std;
struct node
{
int value;
node * child[alpha];
};
node * newnode()
{
node * newt=new node;
newt->value=0;
for(int i=0;i<alpha;i++)
{
newt->child[i]=NULL;
}
return newt;
}
struct trie
{
node * root;
int count;
trie()
{
count=0;
root=newnode();
}
};
trie * dict=new trie;
string reverse(string s)
{
int l=s.length();
string rev=s;
for(int i=0;i<l;i++)
{
int j=l-1-i;
rev[j]=s[i];
}
return rev;
}
void insert(string s)
{
int l=s.length();
node * ptr=dict->root;
dict->count++;
for(int i=0;i<l;i++)
{
int index=s[i]-'a';
if(ptr->child[index]==NULL)
{
ptr->child[index]=newnode();
}
ptr=ptr->child[index];
}
ptr->value=dict->count;
}
void dfs1(node *ptr,string p)
{
if(ptr==NULL) return;
if(ptr->value) cout<<"word" <<p<<endl;
for(int i=0;i<26;i++)
{
if(ptr->child[i]!=NULL)
dfs1(ptr->child[i],p+char('a'+i));
}
}
vector<string> results;
pair<node *,string> search(string s)
{
int l=s.length();
node * ptr=dict->root;
node *save=ptr;
string match="";
int i=0;
bool no_match=false;
while(i<l and !no_match)
{
int in=s[i]-'a';
if(ptr->child[in]==NULL)
{
save=ptr;
no_match=true;
}
else
{
ptr=ptr->child[in];
save=ptr;
match+=in+'a';
}
i++;
}
//cout<<s<<" matched till here"<<match <<" "<<endl;
return make_pair(save,match);
}
bool find(string s)
{
int l=s.length();
node * ptr=dict->root;
string match="";
for(int i=0;i<l;i++)
{
int in=s[i]-'a';
//cout<<match<<"match"<<endl;
if(ptr->child[in]==NULL)
{
return false;
}
ptr=ptr->child[in];
match+=char(in+'a');
}
//cout<<match<<"match"<<endl;
return true;
}
bool leafNode(node *pNode)
{
return (pNode->value != 0);
}
bool isItFreeNode(node *pNode)
{
int i;
for(i = 0; i < alpha; i++)
{
if( pNode->child[i] )
return false;
}
return true;
}
bool deleteHelper(node *pNode, string key, int level, int len)
{
if( pNode )
{
// Base case
if( level == len )
{
if( pNode->value )
{
// Unmark leaf node
pNode->value = 0;
// If empty, node to be deleted
if( isItFreeNode(pNode) )
{
return true;
}
return false;
}
}
else // Recursive case
{
int index = (key[level])-'a';
if( deleteHelper(pNode->child[index], key, level+1, len) )
{
// last node marked, delete it
free(pNode->child[index]);
pNode->child[index]=NULL;
// recursively climb up, and delete eligible nodes
return ( !leafNode(pNode) && isItFreeNode(pNode) );
}
}
}
return false;
}
void deleteKey(string key)
{
int len = key.length();
if( len > 0 )
{
deleteHelper(dict->root, key, 0, len);
}
}
string result="***";
void dfs(node *ptr,string p)
{
if(ptr==NULL) return;
if(ptr->value )
{
if((result)=="***")
{
result=reverse(p);
}
else
{
result=min(result,reverse(p));
}
}
for(int i=0;i<26;i++)
{
if(ptr->child[i]!=NULL)
dfs(ptr->child[i],p+char('a'+i));
}
}
int main(int argc ,char ** argv)
{
#ifndef ONLINE_JUDGE
freopen("prhyme.in","r",stdin);
#endif
string s;
while(getline(cin,s,'\n'))
{
if(s[0]<'a' and s[0]>'z')
break;
int l=s.length();
if(l==0) break;
string rev;//=new char[l+1];
rev=reverse(s);
insert(rev);
//cout<<"...........traverse..........."<<endl;
//dfs(dict->root);
//cout<<"..............traverse end.............."<<endl;
}
while(getline(cin,s))
{
results.clear();
//cout<<s<<endl;
int l=s.length();
if(!l) break;
string rev;//=new char[l+1];
rev=reverse(s);
//cout<<rev<<endl;
bool del=false;
if(find(rev))
{
del=true;
//cout<<"here found"<<endl;
deleteKey(rev);
}
if(find(rev))
{
del=true;
//cout<<"here found"<<endl;
deleteKey(rev);
}
else
{
//cout<<"not here found"<<endl;
}
// cout<<"...........traverse..........."<<endl;
//dfs1(dict->root,"");
// cout<<"..............traverse end.............."<<endl;
pair<node *,string> pp=search(rev);
result="***";
dfs(pp.first,pp.second);
//cout<<"search results"<<endl;
//dfs1(pp.first,pp.second);
//cout<<"end of search results"<<
for(int i=0;i<results.size();i++)
{
results[i]=reverse(results[i]);
// cout<<s<<" "<<results[i]<<endl;
}
string smin=result;
if(del)
{
insert(rev);
}
cout<<smin<<endl;
}
return 0;
}
Your algorithm (using a trie that stores all reversed words) is a good start. But one issue with it is that for each lookup, you have to enumerate all words with a certain suffix in order to find the lexicographically smallest one. For some cases, this can be a lot of work.
One way to fix this: In each node (corresponding to each suffix), store the two lexicographically smallest words that have that suffix. This is easy to maintain while building the trie by updating all ancestor nodes of each newly added leaf (see pseudo-code below).
Then to perform a lookup of a word w, start at the node corresponding to the word, and go up in the tree until you reach a node which contains a descendant word other than w. Then return the lexicographically smallest word stored in that node, or the second smallest in case the smallest is equal to w.
To create the trie, the following pseudo-code can be used:
for each word:
add word to trie
let n be the node corresponding to the new word.
for each ancestor a of n (including n):
if a.smallest==null or word < a.smallest:
a.second_smallest = a.smallest
a.smallest = word
else if a.second_smallest==null or word < a.second_smallest:
a.second_smallest = word
To lookup a word w:
let n be the node corresponding to longest possible suffix of w.
while ((n.smallest==w || n.smallest==null) &&
(n.second_smallest==w || n.second_smallest==null)):
n = n.parent
if n.smallest==w:
return n.second_smallest
else:
return n.smallest
Another similar possibility is to use a hash table mapping all suffixes to the two lexicographically smallest words instead of using a trie. This is probably easier to implement if you can use std::unordered_map.