In this tutorial, we’ll discuss two popular tree data structures: binary tree and binary search tree. We’ll present formal definitions with properties and examples of both tree structures.
Finally, we’ll explain the core conceptual differences between them.
2. Introduction to Binary Trees
A binary tree is a popular and widely used tree data structure. As the name suggests, each node in a binary tree can have at most two children nodes: left and right children. It contains three types of nodes: root, intermediate parent, and leaf node.
A root node is a topmost node in a binary tree. A leaf node can be defined as a node without any children nodes. Finally, excluding the root and leaf nodes, all other nodes are the intermediate parent nodes. Let’s look at an example:
First of all, let’s check if the tree is a binary tree or not. The primary and most important property is that each node can have at most two children nodes. Therefore, we can see that our sample tree obeys this property. Hence, it’s a binary tree.
Here, the node is the root node of the binary tree . Moreover, the nodes and are the leaf nodes. Finally, the nodes and are the intermediate parent nodes.
Each node in a binary tree contains three fields: pointer to the left children, data, pointer to the right children. Let’s look at the logical representation of the binary tree :
There’re various types of binary trees. Some of them are the full, perfect, complete, skewed, balanced binary trees. Moreover, the binary tree data structure supports four main operations: searching, insertion, deletion, and traversal.
Let’s discuss some important properties of the binary tree data structure.
There can be a maximum of nodes in a binary tree with height . Let’s take a look at the binary tree . The leaf nodes are at the height of . Hence, the height of is .
Additionally, the height of the root is . Hence, the maximum number of nodes with height can be . Similarly, the maximum number of nodes can contain would be .
Now given the total number of nodes in a binary tree, the minimum height or level of the binary tree would be . In , there’re nodes. Hence, the minimum height or level would be .
We can have a maximum of nodes at the level of a binary tree. It can be verified easily. The level of the root node of a binary tree is always . Hence, there can be a maximum of node, which is valid for all binary trees.
Additionally, a detailed explanation with algorithms on how to calculate the exact number of nodes and level of a binary tree can be found in the tutorial: Number of Nodes in a Binary Tree With Level N.
Let’s assume a binary tree has leaves. According to the binary tree structure, it should have at least levels. In the case of , we have leaves. Hence, according to this property, should have at least levels which is true.
Now, based on the definition of a binary tree, each node can contain up to two children nodes. Therefore, consider a scenario where all the nodes in a binary tree either contain or children. In such a case, the number of nodes with children is always one less than the number of nodes with no children.
In , there’re nodes (nodes with key and ) with no children and nodes (nodes with key and ) with children. Hence, this example verifies the property.
4. Introduction to Binary Search Tree
A binary search tree is a sorted tree data structure. Moreover, it follows the properties of the binary tree with some additional properties. Hence, each node can have at most two children nodes. But here, the key of the left subtree is less than the key value of its parent node. Similarly, the key of the right subtree is greater than the key value of its parent node.
Let’s look at an example:
Let’s first determine whether it’s a binary search tree or not. The left children of the root node contain a key that has less value than the key of the root node. Similarly, the right children of the root node contain the key with a greater value than the root node. Finally, the rest of the tree follows both binary tree and binary search tree properties. Hence, we can say that it’s a binary search tree.
Let’s look at another example:
Here, the tree is a binary tree as the root and intermediate parent nodes contain children nodes each. Although, it’s not a binary search tree. If we look at the leaf nodes of the left subtree, we can observe that it violates the property of the binary search tree. Hence, it’s a binary tree but not a binary search tree.
A more detailed discussion on binary search tree data structure and possible tree operations can be found in the tutorial: A Quick Guide to Binary Search Trees.
There’re similarities between a binary search tree and a binary tree. Both are tree data structures, and each node in both structures can contain at most two children nodes. Regardless of the similarities, they have some core structural and functional differences. Let’s discuss the key differences between them:
In this tutorial, we discussed two tree data structures: binary tree and binary search tree. We explained both data structures with examples and presented key differences between them.