A Binary Search Tree (BST) is a node-based binary tree data structure where each node stores a key, and every node in the left subtree holds a key strictly smaller than the parent, while every node in the right subtree holds a key strictly greater. This ordering property makes BSTs excellent for fast lookup, insertion, and deletion — all in O(log n) time on average.
In this post, we implement a BST in Java that supports insertion, search, and three traversal orders: inorder, preorder, and postorder.
BST Properties
One node is designated the root of the tree.
Each internal node contains a key and has at most two child subtrees.
The left subtree of a node contains only keys strictly less than the node's key.
The right subtree of a node contains only keys strictly greater than the node's key.
Each subtree is itself a valid BST.