Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Binary Search Trees: A Comprehensive Guide, Study notes of Data Structures and Algorithms

A comprehensive guide to binary search trees (bsts), a fundamental data structure in computer science. It covers the definition, properties, advantages, and implementation of bsts, including searching, insertion, and deletion operations. The document also explores different types of binary trees, such as complete, full, and perfect binary trees. It is a valuable resource for students and professionals seeking to understand and apply bsts in various applications.

Typology: Study notes

2020/2021

Uploaded on 12/07/2024

assignments
assignments 🇮🇳

1 document

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Tree Data Structure:
A tree is a non-linear data structure consisting of nodes connected by edges. It is used to represent
hierarchical structures, such as file systems, computer directories, and organization charts. Trees are
a fundamental data structure used in computer science, and they have many applications, such as in
search algorithms, decision-making processes, and data compression.
The topmost node in a tree is called the root node, which has no parent nodes. Each node in a tree
can have one or more child nodes, except for the leaf nodes, which have no children. Nodes that
share the same parent node are called siblings.
1. The tree is a connected graph: A tree is a connected graph, which means that there is a path
from any node to any other node in the tree.
2. The tree has a unique root node: A tree has a unique root node, which is the topmost node in
the tree.
3. Each child node has its own subtree: Each node in a tree has its own subtree, which is a
subtree that includes all the nodes that are descendants of the node.
4. The tree is acyclic: A tree is acyclic, which means that there are no cycles in the tree.
5. The tree may be empty: A tree may be empty, which means that it has no nodes.
The terminology used in trees includes the following:
1. Root: The topmost node in a tree, which has no parent nodes.
2. Parent: A node that has one or more child nodes.
3. Child: A node that has a parent node.
4. Siblings: Nodes that have the same parent node.
Object 1Object 2Object 3Object 4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Binary Search Trees: A Comprehensive Guide and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Tree Data Structure: A tree is a non-linear data structure consisting of nodes connected by edges. It is used to represent hierarchical structures, such as file systems, computer directories, and organization charts. Trees are a fundamental data structure used in computer science, and they have many applications, such as in search algorithms, decision-making processes, and data compression. The topmost node in a tree is called the root node, which has no parent nodes. Each node in a tree can have one or more child nodes, except for the leaf nodes, which have no children. Nodes that share the same parent node are called siblings.

  1. The tree is a connected graph: A tree is a connected graph, which means that there is a path from any node to any other node in the tree.
  2. The tree has a unique root node: A tree has a unique root node, which is the topmost node in the tree.
  3. Each child node has its own subtree: Each node in a tree has its own subtree, which is a subtree that includes all the nodes that are descendants of the node.
  4. The tree is acyclic: A tree is acyclic, which means that there are no cycles in the tree.
  5. The tree may be empty: A tree may be empty, which means that it has no nodes. The terminology used in trees includes the following:
  6. Root: The topmost node in a tree, which has no parent nodes.
  7. Parent: A node that has one or more child nodes.
  8. Child: A node that has a parent node.
  9. Siblings: Nodes that have the same parent node. Object 1Object 2Object 3Object 4
  1. Leaf: A node with no children.
  2. Depth or Level: The depth(or level) of a node is equal to the number of edges from tree's root node.
  3. Height: The height of the node is the length of the path from that node to the deepest node in the tree.
  4. Subtree: A tree that consists of a node and all its descendants. Binary Tree: A Binary Tree Data Structure is a hierarchical data structure in which each node has at most two children, referred to as the left child and the right child. It is commonly used in computer science for efficient storage and retrieval of data, with various operations such as insertion, deletion, and traversal. Each node can have at most two children: A binary tree is a tree data structure in which each node has at most two child nodes. This means that a node can have either zero, one, or two children. Binary trees can be implemented in many different ways, including arrays and linked lists. One common way to implement a binary tree is to use a linked list, where each node in the tree points to its left and right child nodes. Types of Binary Tree There are several types of binary trees, some of the common types are: 1. Complete Binary Tree: A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
  5. Full Binary Tree: A full binary tree is a binary tree in which every node has either 0 or 2 children. In other words, every node in the tree has either two child nodes or no child nodes.
  1. Balanced tree: A balanced tree is one in which the height of the left and right subtrees of every node differs by at most one. A balanced BST ensures that search, insertion, and deletion operations are performed in O(log n) time complexity on average.
  2. Maximum and minimum values: The maximum value in a BST is the rightmost node, and the minimum value is the leftmost node.
  3. Recursive structure: A BST is a recursive data structure, in which each subtree is itself a BST. Advantages of Binary Search Tree Binary search trees (BSTs) offer several advantages over other data structures. Here are some of the main advantages of BSTs:
  4. Efficient searching: BSTs allow for efficient searching of data. Due to the sorted order of the tree, search operations can be performed in O(log n) time complexity on average. This makes BSTs an excellent choice for applications that require frequent searching, such as database indexing and retrieval.
  5. Efficient insertion and deletion: BSTs also allow for efficient insertion and deletion of data. When adding or removing a node from a BST, the tree is automatically rebalanced to maintain the binary search property. This ensures that the tree remains efficient even after many insertions and deletions.
  6. Memory efficiency: BSTs can be implemented using pointers or references, which allows for efficient use of memory. Compared to other data structures, such as arrays or linked lists, BSTs require less memory to store the same amount of data.
  7. Sorted order: The sorted order of a BST makes it easy to perform operations that require the data to be sorted, such as finding the maximum or minimum value, or performing in- order traversal.
  8. Flexibility: BSTs are highly flexible and can be adapted to suit a wide range of applications. For example, a BST can be used to implement a priority queue, a symbol table, or a balanced binary tree. In summary, BSTs offer several advantages over other data structures, including efficient searching, efficient insertion and deletion, memory efficiency, sorted order, and flexibility. These properties make BSTs a popular choice for a wide range of applications in computer science and beyond.

Binary Search Tree

A tree is a kind of data structure that is used to represent the data in hierarchical form. It is a non- linear data structure as the data in a tree is not stored linearly or sequentially.It can be defined as a collection of objects or entities called as nodes that are linked together to simulate a hierarchy. A Binary Search Tree (BST) is a special type of binary tree in which the left child of a node has a value less than the node’s value and the right child has a value greater than the node’s value. This property is called the BST property and it makes it possible to efficiently search, insert, and delete elements in the tree. The root of a BST is the node that has the smallest value in the left subtree and the largest value in the right subtree. Each left subtree is a BST with nodes that have smaller values than the root and each right subtree is a BST with nodes that have larger values than the root. Binary Search Tree is a node-based binary tree data structure that has the following properties:  The left subtree of a node contains only nodes with keys lesser than or equal the node’s key.  The right subtree of a node contains only nodes with keys greater than the node’s key.  This means everything to the left of the root is less than the value of the root and everything to the right of the root is greater than the value of the root. Due to this performing, a binary search is very easy.  The left and right subtree each must also be a binary search tree. There must be no duplicate nodes(BST may have duplicate values with different handling approaches) // Given Node node struct node { int key; struct node left, right; }; // Function to create a new BST node struct node newNode(int item) { struct node temp= (struct node*)malloc(sizeof(struct node)); temp->key = item;

  1. return Search(root → right, item)
  2. END if
  3. Step 2 - END

Binary Search Tree Operations

Commonly performed operations on binary search tree are-  Search Operation  Insertion Operation  Deletion Operation

1. Search Operation-

Search Operation is performed to search a particular element in the Binary Search Tree.

Rules-

For searching a given key in the BST,  Compare the key with the value of root node.  If the key is present at the root node, then return the root node.  If the key is greater than the root node value, then recur for the root node’s right subtree.  If the key is smaller than the root node value, then recur for the root node’s left subtree.

Example-Consider key = 45 has to be searched in the given

BST-

We start our search from the root node 25. As 45 > 25, so we search in 25’s right subtree. As 45 < 50, so we search in 50’s left subtree. As 45 > 35, so we search in 35’s right subtree. As 45 > 44, so we search in 44’s right subtree but 44 has no subtrees. So, we conclude that 45 is not present in the above BST.

2. Insertion Operation-

Insertion Operation is performed to insert an element in the Binary Search Tree.

Rules-

The insertion of a new key always takes place as the child of some leaf node. For finding out the suitable leaf node,

  1. Search the key to be inserted from the root node till some leaf node is reached.
  2. Once a leaf node is reached, insert the key as child of that leaf node.

Algorithm

  1. Create a new BST node and assign values to it.
  2. insert(node, key) i) If root == NULL, return the new node to the calling function. ii) if root=>data < key call the insert function with root=>right and assign the return value in root=>right. root->right = insert(root=>right,key) iii) if root=>data > key call the insert function with root->left and assign the return value in root=>left. root=>left = insert(root=>left,key)
  3. Finally, return the original root pointer to the calling function.

Example-

Consider the following example where key = 40 is inserted in the given BST-

else if (root->key > val) root->left = insert(root->left,val); /*

  • It will handle two cases
  • (Prevent the duplicate nodes in the tree)
  • 1.if root->key == val it will straight away return the address of the root node
  • 2.After the insertion, it will return the original unchanged root's address / return root; } /
  • it will print the tree in ascending order
  • we will discuss about it in the upcoming tutorials */ void inorder ( struct node *root) { if (root == NULL) return ; inorder(root->left); printf("%d ",root->key); inorder(root->right); } int main () { struct node *root = NULL; root = insert(root, 100 ); root = insert(root, 50 ); root = insert(root, 150 ); root = insert(root, 50 ); inorder(root); return 0 ; }

3. Deletion Operation-

Deletion Operation is performed to delete a particular element from the Binary Search Tree. When it comes to deleting a node from the binary search tree, following three cases are possible-

Case-01: Deletion Of A Node Having No Child (Leaf Node)-

Just remove / disconnect the leaf node that is to deleted from the tree.

Example-

Consider the following example where node with value = 20 is deleted from the BST-

Case-

Deletion Of A Node Having Only One Child-

Just make the child of the deleting node, the child of its grandparent.

Example-

Consider the following example where node with value = 30 is deleted from the BST-

Case-

Deletion Of A Node Having Two Children-

A node with two children may be deleted from the BST in the following two ways-

Method-01:

 Visit to the right subtree of the deleting node.  Pluck the least value element called as inorder successor.  Replace the deleting element with its inorder successor.

Example-

Consider the following example where node with value = 15 is deleted from the BST-

temp->left = temp->right = NULL; return temp; } // A utility function to insert // a new node with given key in BST struct node* insert(struct node* node, int key) { // If the tree is empty, return a new node if (node == NULL) return newNode(key); // Otherwise, recur down the tree if (key < node->key) node->left = insert(node->left, key); else if (key > node->key) node->right = insert(node->right, key); // Return the (unchanged) node pointer return node; } // Utility function to search a key in a BST struct node* search(struct node* root, int key) { // Base Cases: root is null or key is present at root if (root == NULL || root->key == key) return root; // Key is greater than root's key if (root->key < key) return search(root->right, key); // Key is smaller than root's key return search(root->left, key); } // Driver Code int main() { struct node* root = NULL; root = insert(root, 50); insert(root, 30); insert(root, 20); insert(root, 40); insert(root, 70); insert(root, 60); insert(root, 80); // Key to be found int key = 6; // Searching in a BST

if (search(root, key) == NULL) printf("%d not found\n", key); else printf("%d found\n", key); key = 60; // Searching in a BST if (search(root, key) == NULL) printf("%d not found\n", key); else printf("%d found\n", key); return 0; }