Implementing a Binary Tree in Java

I just announced the new Spring 5 modules in REST With Spring:

1. Introduction

In this article, we’ll cover the implementation of a binary tree in Java.

For the sake of this article, we’ll use a sorted binary tree that will contain int values.

2. Binary Tree

A binary tree is a recursive data structure where each node can have 2 children at most.

A common type of binary tree is a binary search tree, in which every node has a value that is greater than or equal to the node values in the left sub-tree, and less than or equal to the node values in the right sub-tree.

Here’s a quick visual representation of this type of binary tree:

For the implementation, we’ll use an auxiliary Node class that will store int values and keep a reference to each child: