Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java 9 Data Structures and Algorithms

You're reading from   Java 9 Data Structures and Algorithms A step-by-step guide to data structures and algorithms

Arrow left icon
Product type Paperback
Published in Apr 2017
Publisher Packt
ISBN-13 9781785889349
Length 340 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Debasish Ray Chawdhuri Debasish Ray Chawdhuri
Author Profile Icon Debasish Ray Chawdhuri
Debasish Ray Chawdhuri
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Why Bother? – Basic FREE CHAPTER 2. Cogs and Pulleys – Building Blocks 3. Protocols – Abstract Data Types 4. Detour – Functional Programming 5. Efficient Searching – Binary Search and Sorting 6. Efficient Sorting – quicksort and mergesort 7. Concepts of Tree 8. More About Search – Search Trees and Hash Tables 9. Advanced General Purpose Data Structures 10. Concepts of Graph 11. Reactive Programming Index

Binary search tree


You already know what binary search is. Let's go back to the sorted array from an earlier chapter and study it again. If you think about binary search, you know you need to start from the middle of the sorted array. Depending on the value to be searched, either we return if the middle element is the search item, or move to the left or right based on whether the search value is greater than or less than the middle value. After this, we continue the same process recursively. This means the landing points in each step are quite fixed; they are the middle values. We can draw all the search paths as in the next figure. In each step, the arrows connect to the mid points of both the right half and left half, considering the current position. In the bottom part, we disassemble the array and spread out the elements while keeping the sources and targets of the arrows similar. As one can see, this gives us a binary tree. Since each edge in this tree moves from the midpoint of one...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime