What is Binary Search Tree
Last Updated :
01 Mar, 2023
A binary search tree (BST) is a binary tree in which the left subtree of a node contains only nodes with less value and the right subtree of a node contains only nodes with values greater than it.
Binary Search Tree
Characteristics of Binary Search Tree:
The properties of a binary search tree are as follows:
- Ordering property: Every node in a binary search tree has a value, and the values in the left subtree are less than the value of the node, while the values in the right subtree are greater than the value of the node.
- Recursive structure: A binary search tree is a recursive data structure because each subtree of a binary search tree is also a binary search tree.
- Traversal order: The in-order traversal of a binary search tree gives the keys in sorted order.
Applications of Binary Search Tree:
- Searching: As the name suggests, binary search trees are primarily used for searching operations. Due to their ordering property, they can be used to quickly search for a particular key or value.
- Sorting: Binary search trees can be used to sort a list of items in O(N * logN) time. The inorder traversal of the tree produces the items in sorted order.
- Symbol tables: Symbol tables are data structures used to store key-value pairs. Binary search trees can be used to implement symbol tables with efficient search, insert, and delete operations.
- Indexing: BSTs are used for indexing in databases.
For more applications of BST refer to this article.
Advantages of Binary Search Tree:
- Fast search: Binary search trees provide efficient search operations with an average time complexity of O(log n) for a tree with n nodes. This makes them ideal for applications that require fast searching.
- Sorting: Binary search trees can be used to sort a list of items in O(n log n) time, which is faster than many other sorting algorithms..
- Dynamic resizing: Binary search trees can be easily resized by inserting or deleting nodes, which makes them suitable for applications that require dynamic resizing.
- Efficient memory usage: Binary search trees use memory efficiently by only storing the data required for each node, which makes them ideal for applications with limited memory.
For more advantages refer to this article.
Disadvantages of Binary Search Tree:
The disadvantages of binary search trees are as follows:
- Unbalanced Trees: If the binary search tree is unbalanced, with one subtree much larger than the other, it can lead to slow search and insertion times. Unbalanced trees can also lead to higher memory usage.
- Degenerate Trees: A degenerate tree is a tree where each parent node has only one child. In this case, the binary search tree becomes a linked list, which can result in slow search and insertion times.
- Difficulty of Deletion: Deleting a node in a binary search tree can be complex, especially when the node has two children. In this case, the tree must be restructured to maintain the ordering property.
- Not thread-safe: Binary search trees are not inherently thread-safe, and concurrent access to the tree can lead to race conditions and other concurrency issues.
- Performance depends on the data: The performance of binary search trees depends on the structure of the data being stored. In some cases, other data structures may be more suitable for the data being stored.
For more disadvantages, refer to this article.
What else can you read?
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...