如何使用二叉搜索树的索引生成器解决此问题
How can i fix this problem with index builder using binary search tree?
>我有一个任务,就是编写一个索引生成器应用程序,它接受由行组成的文本并打印文本的单词列表,它们出现在它们旁边的行。
但是当我尝试处理这种情况时,如果单词已经存在,我遇到了一个问题,它总是在向量中添加一个冗余数字
谁能帮我?
这是BSTnode的定义:
class BSTnode
{
public:
string data;
vector<int> linesAppear;
BSTnode* left;
BSTnode* right;
BSTnode()
{
left = right = NULL;
}
};
这是BSTFCI的定义:
class BSTFCI
{
public:
BSTnode* root;
BSTFCI()
{
root = NULL;
}
void add(string ToBST,int lineAppear);
BSTnode* Insert(BSTnode*& node,string ToBST,int lineAppear);
BSTnode* create_new_node(string ToBST,int lineAppear);
};
插入功能
BSTnode* BSTFCI::create_new_node(string ToBST,int lineAppear)
{
BSTnode* Temp = new BSTnode();
Temp->data = ToBST;
Temp->left = Temp->right = NULL;
Temp->linesAppear.push_back(lineAppear);
return Temp;
}
BSTnode* BSTFCI::Insert(BSTnode*& node,string ToBST,int lineAppear)
{
if(node == NULL)
{
node = create_new_node(ToBST,lineAppear);
}
if(ToBST > node->data)
{
node->right = Insert(node->right,ToBST,lineAppear);
}
if(ToBST < node->data)
{
node->left = Insert(node->left,ToBST,lineAppear);
}
//cout <<"inside insert"<< ToBST << endl;
if(node->data == ToBST)
{
node->linesAppear.push_back(lineAppear);
// cout <<"inside insert condition "<< node->data << endl;
}
return node;
}
void BSTFCI::add(string ToBST,int lineAppear)
{
root = Insert(root,ToBST,lineAppear);
}
主要功能:
int main()
{
BSTFCI o;
string input,ToBST;
int lineAppear = 0;
while(getline(cin,input))
{
if(input == "done")
{
break;
}
lineAppear++;
istringstream convert(input);
while(convert >> ToBST)
{
o.add(ToBST,lineAppear);
}
}
o.print_inOrder(o.root);
return 0;
}
这是因为您在 create_new_node
中添加了数字(实际上应该是 BSTnode
中的构造函数(和稍后if(node->data == ToBST)
时
您需要决定是在创建节点时还是之后添加它,但在创建时执行此操作最有意义 - 为什么要添加节点而不给它一个发生?
我会这样做:
class BSTnode
{
public:
string data;
vector<int> linesAppear;
BSTnode* left;
BSTnode* right;
BSTnode() : left(nullptr), right(nullptr) {}
BSTnode(const std::string& word, int appearance)
: data(word),
linesAppear(1, appearance),
left(nullptr),
right(nullptr)
{
}
};
BSTnode* BSTFCI::Insert(BSTnode* node, string ToBST,int lineAppear)
{
if(node == nullptr)
{
return new BSTnode(ToBST, lineAppear);
}
if(ToBST > node->data)
{
node->right = Insert(node->right, ToBST, lineAppear);
}
else if(ToBST < node->data)
{
node->left = Insert(node->left, ToBST, lineAppear);
}
else
{
node->linesAppear.push_back(lineAppear);
}
return node;
}
请注意,通过引用传递node
和返回都没有意义,因此我保留了返回并删除了引用。
你也可以反其道而行之。
相关文章:
- Project Euler问题4的错误解决方案
- 在java中解决这段代码时面临循环中的问题
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 难以理解某些人解决IOI问题的源代码
- 节俭并发:未解决的外部问题
- IpOpt拒绝解决不受约束的问题
- 错误LNK2005无法解决问题的解决方案
- 如何解决g++问题"internal compiler error: Illegal instruction min() _GLIBCXX_USE_NOEXCEPT { return __FLT_MI
- 必须使用尾随返回类型的示例,因为无法用旧方法解决问题
- 如何解决问题"unresolved extenal symbol _DllMainCRTStartup@12"?
- 如何使用OlesetClipboard将我的COM对象放置在剪贴板上时解决问题
- 与C++析构函数相比,C#析构函数和GC并没有真正解决问题
- 没有调用函数,而是使用垃圾值.如何调用函数并解决问题
- 所有的组合算法和解决C++问题的一般方法
- 模板过载解决问题
- 对"pthread_key_create"的未定义引用;参数顺序不能解决问题
- 如何在使用cplex解决问题时设置间隙
- 选择一门语言学习真的很重要吗?他们都使用相似的技术来解决问题
- Iostream解决c++问题
- 递归是一件坏事吗?在递归似乎最有用的地方,是否有更清晰或更有效的方法来解决问题?