Binary tree

A binary tree is a tree that has a maximum of two children per node. The two children can be called the left and the right child of a node. The following figure shows an example of a binary tree:

Binary tree

Example binary tree

This particular tree is important mostly because of its simplicity. We can create a BinaryTree class by inheriting the general tree class. However, it will be difficult to stop someone from adding more than two nodes and will take a lot of code just to perform the checks. So, instead, we will create a BinaryTree class from scratch:

public class BinaryTree<E>  {

The Node has a very obvious implementation just like the generic tree: ...

Get Java 9 Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.