Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 11, 2023
In this tutorial, we’ll discuss binary trees, linked lists, and hash tables. We’ll define these data structures, as well as outline where they are used and how they are structured. Finally, we’ll compare the properties of these data structures to point out the similarities and differences between them.
A binary tree is a hierarchical tree-based data structure in which each node has at most two children. The root node is the topmost node of a binary tree, while the left and right nodes are called left and right children, respectively. Furthermore, the links between nodes are known as branches, while a node without children is called a leaf node.
In a binary tree, a single node will contain a data value and a link to each of the child nodes. The following operations can be performed on binary trees: insertion, searching, and deletion. These operations can be executed in time.
A binary tree is illustrated as follows:
In computer science, a binary tree forms the basis of many other data structures, such as binary search trees, heaps, red-black trees, and hash trees. These data structures utilize the structure and properties of binary trees to implement a means of organizing and managing data. In addition, routing tables, decision trees, and sorting are other applications of binary trees.
For more information on the applications of binary trees, read our article here.
The main advantage of using binary trees is simplicity. Binary trees possess a simple-to-understand structure for data management and organization. Additionally, some benefits of binary trees are:
On the other hand, some limitations to using binary trees are:
A linked list is a dynamic data structure consisting of nodes and pointers to other nodes. The nodes form a sequence of nodes that contain data and links to the next nodes. The structure of a linked list is illustrated as follows:
The basic operations that can be performed on linked lists are searching, insertion, deletion, and update. Search operations can be done in time, while insertion and deletion are done in
time.
Just like binary trees, linked lists are also used in the implementation of other data structures, such as queues, graphs, and stacks. Doubly linked lists, circular linked lists, and singular linked lists are different variations of this data structure. The structure of a circular linked list is such that it has the last node pointer pointing to the first node, while a doubly-linked list has pointers to both preceding and succeeding nodes.
Linked lists are also used in dynamic memory allocation, where memory is assigned to tasks during execution. Likewise, this data structure can be used to represent sparse matrixes. For more information on other applications of linked lists, read our article here.
A few benefits to using linked lists are:
On the contrary, some limitations of linked lists are:
A hash table is different from binary trees and linked lists in the sense that it is implemented with an array. It stores data as key-value pairs. Each data value in a hash table has a key or index that is produced using a technique known as hashing.
In hashing, a hash function is used to convert a variable-length value or data into a fixed-length value. A hash table uses hashing to generate an index to determine where to store data values:
There are three basic operations that can be performed on hash tables: insertion, searching, and deletion of data values. These operations can usually be completed in time.
Hash tables are efficient due to their fast access and are used in many applications, such as address tables, compiler symbol tables, search engines, password look-ups, and file systems.
Consequently, some major benefits of using hash tables are:
Conversely, some limitations of using hash tables are:
| Binary Tree | Linked List | Hash Table |
|---|---|---|
| The nodes are connected to 2 other nodes at most | The nodes are connected to one other node at most | Node connections are arbitratry |
| Non linear data structure | Linear data structure | Can be implemented as both linear and non-linear structures. |
| Insertion, deletion and search are done in O(log(n)) time | Search is done in O(n) time Insertion and deletion are done in O(1) time | Insertion, deletion and search are done in O(1) time |
| Data values usually sorted/ordered | Data values are not ordered | Data values are not ordered |
| Input data size does not have to be specified before implementation | Input data size does not have be specified before implementation | Input data size must be specified before the structure is created |
In this article, we reviewed three data structures: binary trees, linked lists, and hash tables. We explored their structures, uses, and how they can be distinguished from each other. We also highlighted the basic operations that can be performed on these three data structures.