|
Posted by Manish on 08/09/06 07:01
The array is not sorted.
New category id/sub-category id can be added at any time and to any
existing category. The new id value will be one more than the maximum
value. The datas are in database and after retrieving all the rows from
the table, such an array is created, so that to display the categories
in the folder like structure (javascript), at the page load itself.
Like it is in folder view in the explorer window.
So, all the id's will be needed at once at the page load itself, so
that the javascript tree can be created. Strictly speaking, it is just
table hide/show on click.
Benjamin Esham wrote:
> Manish wrote:
>
> > Any key in the array at any level is unique. i.e. key 1003 will not appear
> > twice.
> >
> > I want to find out at what level the particular key exists.
> >
> > Please suggest on the coding for function KeyLevel().
>
> I'll give you a couple of pointers (partly because this looks like a
> homework problem, and partly because I don't want to write and test an
> entire function :-)).
>
> Write a function to recursively traverse the array: for each element, call
> KeyLevel() on the element. Each time you call KeyLevel(), increment a
> counter to record how deep you are; each time an instance of KeyLevel()
> returns unsuccessfully, decrement the counter. If the desired key is found,
> simply return the counter value; otherwise, keep traversing the array until
> you find it.
>
> I'll admit, I'm not an experienced programmer, and algorithms were never my
> forte, but this should put you on the right track. By the way, if your
> input array is sorted, that should make the algorithm much simpler.
>
> HTH,
> --
> Benjamin D. Esham
> bdesham@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
> "Kreacher did not see young master," he said, turning around and
> bowing to Fred. Still facing the carpet, he added, perfectly
> audibly, "Nasty little brat of a blood traitor it is." - OotP
[Back to original message]
|