访问单链表中的BST
Accessing a BST in a singly linked list
有一个单链表,它的节点是BST的,c++中需要使用什么代码才能让我能够访问BST的元素?
ifstream isd;
isd.open ( "book1.txt" );
while( !isd.eof ( ) ) {
book b1;
isd >> b1;
b[i] = b1;
i++;
}
isd.close ( );
int j = 0;
for( int k = 0; k < 104; k++ ) {
if( b[k].category == "adventure" )
bcat[j].insert ( b[k] );
if( b[k].category == "family" )
bcat[j + 1].insert ( b[k] );
if( b[k].category == "fiction" )
bcat[j + 2].insert ( b[k] );
if( b[k].category == "fun" )
bcat[j + 3].insert ( b[k] );
if( b[k].category == "history" )
bcat[j + 4].insert ( b[k] );
if( b[k].category == "horror" )
bcat[j + 5].insert ( b[k] );
}
if( b[k].category == "mystery" )
bcat[j + 6].insert ( b[k] );
if( b[k].category == "school" )
bcat[j + 7].insert ( b[k] );
if( b[k].category == "science" )
bcat[j + 8].insert ( b[k] );
if( b[k].category == "story" )
bcat[j + 9].insert ( b[k] );
if( b[k].category == "suspence" )
bcat[j + 10].insert ( b[k] );
for( int k = 0; k < 11; k++ )
sdb.insert ( bcat[k] );
BinNode < Elem > preorder ( BinNode < Elem > *subroot )
{
if( subroot == NULL )
break;
book ele;
return ele = subroot->val ( );
}
for( b1.setStart ( ); b1.getValue ( ro ); b1.next ( ) ) {
b2 = ro.getroot ( );
b3 = b2->val ( );
if( r.Category == b3.category ) { //compare titles in order to find the correct title
}
}
从文件中读取book
后,根据它们的类别将它们插入到bst数组中,然后将它们插入到链表中。我只需要能够访问bst的节点。
BST<string,book,titlebookcomp,bookcomp> ro;
BinNode<book> b2;
book b3;
为什么不推出自己的算法呢?bst在列表中的顺序如何?这应该是一个相当简单的练习。我不确定是否有对bst列表的内置(例如STL)支持。如果你把你的问题说清楚,这里有很多人会帮你写一个算法来做任何你想做的事。
编辑:下面是一些伪代码。告诉我这是什么错了,或者你在哪里有困难实现它:
FindBook(mylist, cat, tit)
1. while mylist != null do
2. if mylist.root.category = cat then
3. BSTnode = mylist.root
4. while BSTnode != null do
5. if BSTnode.title = tit then
6. print "Found title %s, category %s in BST %s", tit, cat, mylist
7. return true
8. elsif BSTnode.title < tit then
9. BSTnode = BSTnode.right
10. else
11. BSTnode = BSTnode.left
12. mylist = mylist.next
13. print "Did not find title %s, category %s", tit, cat
14. return false
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 我们可以删除链表中静态内存中的节点吗
- C++,指针数组,指向双链表中的条目
- 链表中写入访问冲突的未知原因
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 打印所有链表的元素 (C++)
- 错误:需要类名(链表c++)
- 为什么C中的通用链表中存储的数据已损坏
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 链表c++插入,所有情况都已检查,但没有任何工作
- 获取链表上的"expected ‘)’ before ‘&’ token"到 BST 构造函数签名
- 通过数组实现BST,通过链表实现堆
- 将 BST 转换为排序的双向链表