访问单链表中的BST

Accessing a BST in a singly linked list

本文关键字:BST 链表 单链表 访问      更新时间:2023-10-16

有一个单链表,它的节点是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