Can i use binary search tree in an unordered array?

Hi there,

I have unordered array content and i want to have a search on that. So is it true that i have to sort my array first, "before" populating the tree?

LVL 12
Who is Participating?
CEHJConnect With a Mentor Commented:
It is true. Binary ordering/searching functionality has to be performed on sorted objects
d-glitchConnect With a Mentor Commented:
No.  In a binary search, every test should reduce the search range by half.
If the array is unordered, your test doesn't tell you which way to go.
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
I agree with CEHJ - yes, you need to sort it first.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

I agree with CEHJ as well.

The No in my comment refers to the question in the title.
jazzIIIloveAuthor Commented:
Thanks, but a question,
 In 0.06 of the video, the elements in the array are not presorted. I am a little confused.

CEHJConnect With a Mentor Commented:
The video i see is solely devoted to showing the ordering of the numbers
ValeriConnect With a Mentor Commented:
This video shows only the right way to build the binary tree, nothing more! :-)
You dont need to order the source array at the begining, but it's good to do that in order to choose the right element to put as a root of the tree. In the video root element "5" is chosen as a root "randomly" but it is very important because the rest 6 elemnts are : 3 bigger than 5 and 3 less than 5, so the tree is BALANCED by default.
If your array is not sorted at the beginning, you will be able to build the tree, but after that you need to balance it, in orer to have best performance for search operation.
see this:
Btw, Java offers implementatio of this structure, but yes, it's good to know how it works.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.