binary search tree – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Fri, 26 Aug 2022 17:00:31 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg binary search tree – Programmerbay https://programmerbay.com 32 32 How to Calculate AVL Tree Balance Factor ? https://programmerbay.com/avl-tree-a-height-balancing-bst/ https://programmerbay.com/avl-tree-a-height-balancing-bst/#respond Wed, 15 Jun 2022 17:28:28 +0000 https://www.programmerbay.com/?p=2963  

In Data Structures, AVL tree (Adelson-Velsky and Landis tree)  is a height-balanced binary search tree in which difference between the heights of left subtree and right subtree can be atmost 1. It is a binary search tree where each node associated with a balance factor. If balance factor paired with node is either 1,0, or – 1, it is said to be balanced.

AVL Tree example

Balance factor = height of left subtree – height of right subtree

It is important for a binary search tree to be balanced so that insertion and deletion require less search time, resulting increased efficiency. It requires O(log n) time to perform deletion, insertion and search operation.

AVL Tree Basic Operations

Search operations

A search operation starts from the root node to all the way downward recursively. The targeted element is compared with the root node, if both are equal then the search would get terminated. If it is smaller than the root node then the search goes on at left subtree. And if the targeted value is greater, then search operation would be performed left subtree recursively.

Insert operation

To insert a given element, AVL first performs search operation, starting from the root node in order to locate the insertion position. Ones a NIL location is found then insertion is performed.

Inserting a new node may increase the chances of AVL tree to get unbalanced, therefore, rotation is performed with the every insertion if required .

AVL Tree Rotations

It is a process of balancing the AVL tree when balance factor is greater than 1 or smaller than -1.
These are the following types of rotation:

Left-Left Rotation
When insertion is performed in the left subtree of the left subtree then LL imbalance occurs and the rotation process perform on this case is therefore known as left left rotation. It is a single rotation.

LL Rotation AVL Tree

Right-Right Rotation
When insertion is performed in the right subtree of the right subtree then RR imbalance occurs and the rotation process performed in this case is therefore known as Right-Right rotation. It is a single rotation.

RR Rotation AVL tree

Left Right Rotation
When insertion is performed in the right subtree of the left subtree then LR imbalance occurs and the rotation process performed in this case is therefore known as Left-Right. It is double rotation.

LR rotation in AVL TREE

Right Left Rotation
When insertion is performed in the left subtree of the right subtree then RL imbalance occurs and the rotation process performed in this case is therefore known as Right-Left. It is double rotation.

RL Rotation in AVL Tree

]]>
https://programmerbay.com/avl-tree-a-height-balancing-bst/feed/ 0
What is Binary Search Tree and its Operations? https://programmerbay.com/what-is-binary-search-tree-and-its-operations/ https://programmerbay.com/what-is-binary-search-tree-and-its-operations/#respond Sat, 13 Jun 2020 17:07:02 +0000 https://www.programmerbay.com/?p=2928 What is Binary Search Tree ?

In Data structures, a binary search tree is a linked data structure that provides a way to store data orderly. All stored keys should satisfy the properties of a Binary search tree. These properties are:

  • Nodes of right subtree always have a value greater than the parent’s node
  • Nodes of left subtree always have value lesser than the parent’s node

It requires O(log n) time to perform basic operations in the average case. However, basic operations on BST take time proportional to the height of the tree.

Each node represents an object that consists link to the right subtree, a key value associated with the node, and a link to left subtree. If any parent or child node is missing, then it would be represented as NIL value.

image 2020 12 26 190922
Figure : Binary Search Tree Example

Binary search tree supports operations such as Insertion, Deletion, Searching of a node in the tree and also allow us to find Minimum, Maximum, Successor and Predecessor. Apart from that, a BST can be traversed in three-ways Inorder traversal, post-order traversal, and pre-order traversal.

Basic Operations in Binary Search Tree

  • Search Operation

A search operation starts from the root node to all the way downward recursively. The targeted element is compared with the root node, if both are equal then the search would get terminated.

If it is smaller than the root node then the search goes on at left subtree. And if the targeted value is greater, then search operation would be performed left subtree recursively.

  • Insertion and deletion

To insert a given element, BST first performs search operation, starting from the root node in order to locate the insertion position. Ones a NIL location is found then insertion is performed.

To delete a key from a BST we must know three cases:
1) If the targeted element has no child then remove the element and place NIL.
2) If the targeted element has only a single child, in this case, we just fill that place by simply putting its child node in its position.
3) If it has two children, then the key greater then the targeted value or Successor of targeted value would replace it.

  • Minimum and Maximum

A minimum key in a BST can be figured out by traversing left subtree from root node till a NIL is not encountered.

Similarly, A maximum key in a BST can be figured out by traversing right subtree from root node till a NIL is not encountered.

  • Successor and predecessor

A successor node for a given key value of BST can be derived without performing any comparison. If there is an immediate key available which is greater than the given node then the value would be returned otherwise NIL would be returned.

A predecessor node for a given key value of BST can be derived, if there is an immediate key smaller than given node then the value would be returned otherwise NIL.

  • Traversal 

Traversing refers to visit each and every node of a tree. Binary search tree can be traversed in three ways

In-order traversal

In this way of traversing a tree, we start from visiting all the nodes of left subtree, then root and then lastly right subtree

Pre-order traversal

In this way of traversing a tree, we start from the root, then visit all the nodes of left subtree and then last traverse right subtree

Post-order traversal

In this way of traversing a tree, we start from visiting all the nodes of left subtree, then all the nodes of the right subtree and then lastly root

]]>
https://programmerbay.com/what-is-binary-search-tree-and-its-operations/feed/ 0