Book Image

Java Fundamentals

By : Gazihan Alankus, Rogério Theodoro de Brito, Basheer Ahamed Fazal, Vinicius Isola, Miles Obare
Book Image

Java Fundamentals

By: Gazihan Alankus, Rogério Theodoro de Brito, Basheer Ahamed Fazal, Vinicius Isola, Miles Obare

Overview of this book

Since its inception, Java has stormed the programming world. Its features and functionalities provide developers with the tools needed to write robust cross-platform applications. Java Fundamentals introduces you to these tools and functionalities that will enable you to create Java programs. The book begins with an introduction to the language, its philosophy, and evolution over time, until the latest release. You'll learn how the javac/java tools work and what Java packages are - the way a Java program is usually organized. Once you are comfortable with this, you'll be introduced to advanced concepts of the language, such as control flow keywords. You'll explore object-oriented programming and the part it plays in making Java what it is. In the concluding chapters, you'll get to grips with classes, typecasting, and interfaces, and understand the use of data structures, arrays, strings, handling exceptions, and creating generics. By the end of this book, you will have learned to write programs, automate tasks, and follow advanced courses on algorithms and data structures or explore more advanced Java courses.
Table of Contents (12 chapters)
Java Fundamentals
Preface

Implementing Binary Search Tree


We already had a brief look at trees in Lesson 7, The Java Collections Framework and Generics let's look at a special implementation of trees known as binary search trees (BSTs).

To understand BSTs, let's take a look at what binary tree is. A tree in which each node in the tree has at most two child nodes, is a binary tree.

A BST is a special implementation of a binary tree, where the left-child node is always less than or equal to the parent node, and the right-child node is always greater than or equal to the parent node. This unique structure of the binary search tree makes it easier to add, delete, and search for elements of the tree. The following diagram represents a BST:

Figure 8.2: Representation of a binary search tree

The applications of binary search tree are as follows:

  • To implement a dictionary.

  • To implement multilevel indexing in a database.

  • To implement a searching algorithm.

Exercise 32: Creating a Binary Search Tree in Java

In this exercise, we...