开发者

Accessing a BST in a singly linked list [closed]

开发者 https://www.devze.com 2023-03-23 13:53 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 8 years ago.

Improve this question

Having a singly linked list with its nodes being BST's, what is the code in c++ which needs to be used in order for me to be able to access the elements of the 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" )
    bca开发者_高级运维t[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 
    }
}

After reading from the file the books..i inserted them into an array of bst depending on their category then i inserted them into the linked list. I just need to be able to access the nodes of the bst.

BST<string,book,titlebookcomp,bookcomp> ro;
BinNode<book> b2;
book b3;


Why not roll your own algorithm? How are the BSTs ordered in the list? It should be a fairly simple exercise to do this. I'm not sure there is built-in (e.g. STL) support for lists of BSTs. If you clarify your problem, lots of people here would help you write an algorithm to do anything you want.

EDIT:

Here's some pseudocode. Tell me what is wrong about this or where you are having trouble implementing it:

  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
0

精彩评论

暂无评论...
验证码 换一张
取 消