Javatpoint Logo
Javatpoint Logo

Advantages and Disadvantages of Binary Search Tree

An exclusive binary tree with the following characteristics is called a "Binary Search Tree" (BST):

  • The keys or values which are smaller than the key of the node are present in the left subtree.
  • The only keys which have higher values than the key of the node are present in the right subtree.
  • Binary search trees should be used for the left and right subtrees, respectively.
Advantages and Disadvantages of Binary Search Tree

Binary Search tree operations:

The four fundamental BST operations are:

  • Searching
  • Traversals
  • Insertion
  • Deletion

1. Searching in a BST

Comparing the key values is a necessary step in the BST search process. If the value of the key is same to the root key, then the search is successful. If the value of the key is less than the root key, the search is also successful. If the key value is larger than the root key, the search is successful.

Searching with the BST algorithm:

  • Verify whether the tree is NULL; if it is not, proceed to the next step.
  • Here we make a comparison between the search key and the root of the BST.
  • If the key value is smaller than the root value then we search in the left subtree.
  • Search in the right part of the tree or right subtree if the key value is bigger than the root value.
  • Return to the search screen and report "search successful" if the key matches root.
  • Follow steps 3, 4, or 5 again to obtain the subtree.

2. Traversals in a BST:

  • The Binary Search Tree has 4 various ways of traversals.
  • Traversal of Nodes in Level Order: The tree is traversed level by level, beginning at the root and moving upward.
  • Pre-order Traversal: The tree nodes are traversed from root to left subtree and then to right subtree.
  • Left subtree, followed by root, followed by right subtree, is the main format we use to visit the nodes in order to traverse.
  • After traversing all of the nodes, the format is left subtree, followed by right subtree, and finally root.

3. Insertion with a BST

The key values are compared during insertion in BST. If the key value is less than or equal to the root key, go to the left subtree, locate an empty space, and put the data there. If the key value exceeds the root key, locate an empty place in the right subtree and add the data there.

4. Deletion in a BST

Three scenarios are involved in deletion in a BST. - Locate the node by first running a search on the key that will be destroyed. Determine how many children the removed node has after that.

  • Case 1: In the event that a leaf node must be removed: Delete a leaf node if it needs to be done.
  • Case 2: Should the deleted node have just one child: If a node is to be destroyed and there is only one child, remove the node and place the child of the deleted node in its place.
  • Case 3: In the event that the node to be deleted has two kids: Locate the node's inorder predecessor or successor using the node's nearest available value if the node to be deleted has two children. Remove the out-of-order successor or predecessor using the scenarios stated above. In the proper order, replace the node with either its predecessor or successor.

Uses of a Binary Search Tree

  • Indexing is done using BSTs.
  • Additionally, it is utilised to create different search methods.
  • It is possible to implement several data structures using IT.
  • Data can be stored and easily retrieved using BSTs in decision support systems.
  • BSTs can be used in computer simulations to swiftly store and retrieve data.
  • Fast autocomplete systems can be put into place using BSTs.

Real-time Binary Search Tree Application

  • BSTs are employed in databases for indexing.
  • Algorithms for searching are implemented using it.
  • To implement the Huffman coding algorithm, BSTs are utilised.
  • Additionally, dictionaries are implemented using it.
  • Data caching is done with it.
  • In Priority queues, it is used.
  • In spell checkers, it is used.

Advantages of Binary Search Tree:

  • When balanced, BST is quick at insertion and deletion. It has a temporal complexity of O and is quick (log n).
  • BST is another tool for quick searching, with most operations having an O(log n) time complexity.
  • BST works efficiently. Because pointers and other data structures are not needed, it is efficient because it merely stores the elements.
  • In order to locate keys between N and M (N <= M), we may also perform range queries.
  • In comparison to other data structures, BST code is straightforward.
  • The items are always kept in a sorted sequence since BST can automatically sort them as they are entered.
  • BST is easily adaptable to enable different activities or to store more data. It is adaptable as a result.

Disadvantages of Binary Search Tree:

  • The key drawback is that a balanced binary search tree needs to be implemented at all times. Otherwise, it's possible that the cost of operations won't be logarithmic and will instead result in a linear array search.
  • Since the time complexity for search, insert, and delete operations is O(log n), which is good for big data sets but slower than some other data structures like arrays or hash tables, they are not well-suited for data structures that need to be accessed randomly.
  • The complexity of a BST might rise if it becomes unbalanced or degenerates.
  • Disable several operations that can be performed on ordered data structures.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA