0% found this document useful (0 votes)
16 views11 pages

Workings of Control Unit

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

Workings of Control Unit

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

BINARY

SEARCH TREE

NAMAN JAIN
23/CS/24
INTRODUCTIO
N
What are search Trees?
A search tree is a data structure that stores elements
in a sorted manner, allowing for effi cient retrieval,
insertion, and deletion.

The advantage of search trees is their effi cient search


time given the tree is reasonably balanced.

Binary Search Trees (BST) are one of the most common


types of search trees, providing optimal performance
when balanced.
INTRODUCTION TO BINARY SEARCH
TREE
• A Binary Search Tree (BST) is a node-based binary tree
data structure characterized by the following
properties.

• The left subtree of a node contains only nodes with


keys that are less than the node's key.

• The right subtree of a node contains only nodes with


keys that are greater than the node's key.

• Both the left and right subtrees must also adhere to


the structure of binary search trees.
Structure of a
BST
•A BST is structured to allow efficient
search, insertion, and deletion.
•The height of the tree determines
the efficiency of these operations.
•Ideally, a BST should be balanced to
maintain performance.
•Example diagram: A BST with nodes
showing hierarchical structure.
INSERTION IN
BST
•Insertion in a BST involves finding the correct position to
maintain the BST properties.

1. Start from the root and compare the value to be inserted with
the current node.

2. If the value is less than the current node, move to the left
child; if greater, move to the right child.

3. Repeat this process until an empty spot is found, then insert


the new node.
C++ CODE FOR
INSERTION
struct Node {
int data;
Node* left;
Node* right;
};

Node* insert(Node* root, int value) {


if (root == nullptr) {
root = new Node();
root->data = value;
root->left = root->right = nullptr;
} else if (value < root->data) {
root->left = insert(root->left, value);
} else {
root->right = insert(root->right, value);
}
return root;
}
DELETION IN
•DeletingBST
a node in a BST involves three
cases:
• Case 1-Leaf Node Deletion: Remove the node
directly.
•Case 2-Node with One Child: Remove the
node and link its child to its parent.
.
•Case-3-Node with Two Children: Replace the node
with its
inorder successor (smallest value in
the right subtree) or predecessor.
C++ CODE FOR
Node* findMin(Node* root)DELETION
{
while (root->left != nullptr) root = root->left;
return root;
}

Node* deleteNode(Node* root, int value) {


if (root == nullptr) return root;
if (value < root->data) root->left = deleteNode(root->left, value);
se if (value > root->data) root->right = deleteNode(root->right, value);
else {
if (root->left == nullptr) {
Node* temp = root->right;
delete root; Node* temp = findMin(root->right);
return temp; root->data = temp->data;
} root->right = deleteNode(root->right, temp-
if (root->right == nullptr) { >data);
Node* temp = root->left; }
delete root; return root;
return temp; }
}
BST
•BSTs are widely used in various applications, including:
Applications
• Database indexing to allow quick lookups.

• File systems to organize files hierarchically.

• Network routing algorithms for efficient data routing

• Autocomplete features in search engines for fast


suggestions.
•Binary Search Trees are foundational data
structures that support efficient insertion,
deletion, and search operations.
•By maintaining balanced BSTs, we can achieve
optimal performance in data processing tasks.

You might also like