Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - LL TO BST

 
Thread Tools Search this Thread
Old 01-19-2005, 10:11 PM   #1
Default LL TO BST


Do you see any problems with the following algorithm that converts Link
list to Binary Search tree. Can you think of a better solution?

node* LLtoBST(node *head, int num)
{

node *root=head;
int i, mid=(num+1)/2;

if(!head) return NULL;

if(n==1){
head->prev=NULL;
head->next=NULL;
}

else{
for (int i=0;i<mid;++i,root=root->next)
;
root->prev=LLtoBST(head, mid-1);
root->next=LLtoBST(root->next,mid-1);
}
return root;

}



puzzlecracker
  Reply With Quote
Old 01-19-2005, 10:30 PM   #2
Howard
 
Posts: n/a
Default Re: LL TO BST

"puzzlecracker" <> wrote in message
news: ups.com...
> Do you see any problems with the following algorithm that converts Link
> list to Binary Search tree. >


Aside from the fact it won't compile, you mean?

>Can you think of a better solution?



> node* LLtoBST(node *head, int num)
> {
>
> node *root=head;
> int i, mid=(num+1)/2;


Why do you declare i here? It's already declared as an int in the loop
below.

>
> if(!head) return NULL;


This could be at the top, to save a couple steps.

>
> if(n==1){


What's n? Did you mean num?

> head->prev=NULL;
> head->next=NULL;
> }
>
> else{
> for (int i=0;i<mid;++i,root=root->next)
> ;
> root->prev=LLtoBST(head, mid-1);


Suppose num is 4. Then mid = (4+1)/2 = 5/2 = 2. So, mid-1 is 1. So you're
going to point your "left" member to the original head of the list, with
only one member. But the loop above just moved the pointer to the third
item in the list, so you're going to lose the second item, aren't you? I
suspect that the num passed to this function needs to be mid, not mid-1.

But that's just for num==4. What about other values of num?

> root->next=LLtoBST(root->next,mid-1);
> }
> return root;
>
> }
>


Step through this on paper, carefully, for both odd and even values of num.

Then, once you have a good design, and have coded it so that ther's no
compile errors, step through it in the debugger and see if it does exactly
what your paper version did.

Also, you mention that you're creating a "Binary Search Tree". But you
don't say whether the linked list is sorted or not. If it isn't then your
tree isn't going to be sorted, either.

-Howard





Howard
  Reply With Quote
Old 01-19-2005, 11:10 PM   #3
Joseph Turian
 
Posts: n/a
Default Re: LL TO BST
pc:

You should also keep in mind that splitting it down the middle may not
guarantee the best amortized performance. It depends upon the
distribution of the queries.

For example, if you only have right children i.e. with the lowest
element at the root and the highest element as the (rightmost) leaf,
this will give the best performance if you're only looking up the
lowest element.

If I'm not mistaken, a balanced tree is desirable iff the queries are
uniformly distributed over the stored items (i.e. if there are no
queries for items that don't exist). If there are queries for items
that don't exist, then the analysis gets much hairier.

Joseph



Joseph Turian
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46