0% found this document useful (0 votes)
23 views

Binary Search Trees11111

Searching a node in a binary search tree (BST) involves comparing the search key to the root node's key and recursively searching the left or right subtree depending on whether the key is smaller or larger. The search algorithm traverses the tree until the key is found or no more nodes remain. BSTs allow efficient searching, insertion, and deletion and are commonly used to implement sorted data structures and indexing for databases. Self-balancing BSTs like AVL and red-black trees are often used in practice to maintain the tree height as O(log n) time for operations.

Uploaded by

thejugowda121
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)
23 views

Binary Search Trees11111

Searching a node in a binary search tree (BST) involves comparing the search key to the root node's key and recursively searching the left or right subtree depending on whether the key is smaller or larger. The search algorithm traverses the tree until the key is found or no more nodes remain. BSTs allow efficient searching, insertion, and deletion and are commonly used to implement sorted data structures and indexing for databases. Self-balancing BSTs like AVL and red-black trees are often used in practice to maintain the tree height as O(log n) time for operations.

Uploaded by

thejugowda121
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/ 7

TEJASWINI C A (1DA22IS057)

Searching in Binary Search Tree (BST)

 Given a BST, the task is to search a node in this BST.

 Algorithm to search for a key in a given Binary Search Tree

 We compare the value to be searched with the value of the root.


 If it’s equal we are done with the search if it’s smaller we know that we
need to go to the left subtree because in a binary search tree all the
elements in the left subtree are smaller and all the elements in the right
subtree are larger.

 Repeat the above step till no more traversal is possible

 If at any iteration, key is found, return True. Else False.


Illustration of searching in a BST:
Illustration of searching in a BST:

See the illustration below for a better understanding

See the illustration below for a better


:

understanding:
C function to search a given key
in a given BST
struct node* search(struct node* root, int key)
#include <stdlib.h>
{
#include <stdio.h> if (root == NULL || root->key == key)
struct node { return root;
int key; if (root->key < key)
struct node *left, *right; return search(root->right, key);
}; return search(root->left, key);
}
struct node* newNode(int item)
int main()
{
{
struct node* temp struct node* root = NULL;
= (struct node*)malloc(sizeof(struct node)); root = insert(root, 50);
temp->key = item; insert(root, 30);
temp->left = temp->right = NULL; insert(root, 20);
return temp; insert(root, 40);
insert(root, 70);
}
insert(root, 60);
struct node* insert(struct node* node, int key)
insert(root, 80);
{ int key = 6;
if (node == NULL) if (search(root, key) == NULL)
return newNode(key); printf("%d not found\n", key);
if (key < node->key) else
node->left = insert(node->left, key); printf("%d found\n", key);
key = 60;
else if (key > node->key)
node->right = insert(node->right, key);
return node;
}
if (search(root, key) == NULL)
printf("%d not found\n", key);
else
printf("%d found\n", key);
return 0;
}

OUTPUT:

6 not found
60 found
Applications of BST
 Binary Search Tree (BST) is a data structure that is commonly used to implement
efficient searching, insertion, and deletion operations. The key feature of a BST is
that it is a binary tree, where each node has at most two child nodes, and the value
of each node is greater than all the values in its left subtree and less than all the
values in its right subtree

 Due to this property, BSTs allow for efficient searching by repeatedly dividing the
search space in half, which makes it an important data structure and many other
fields.

 The left subtree of a node contains only nodes with keys lesser than the node’s key.

 The right subtree of a node contains only nodes with keys greater than the node’s
key.

 The left and right subtree each must also be a binary search tree. There must be no
duplicate nodes.
A Self-Balancing Binary Search Tree is used to
maintain sorted stream of data. For example,
suppose we are getting online orders placed and
we want to maintain the live data (in RAM) in
sorted order of prices. For example, we wish to
know number of items purchased at cost below a
given cost at any moment

One of the most common use cases of BSTs is


searching for a particular element in the tree.
A BST can be used to sort a large dataset. By
inserting the elements of the dataset into a BST
A BST supports operations and then performing an in-order traversal, the
like search, insert, delete, elements will be returned in sorted order.
floor, ceil, greater, smaller, etc Used in Database indexing.
in O(h) time where h is
height of the BST. To keep TreeMap and TreeSet are two data structures
height less, self balancing that are internally implemented using self-
balancing BSTs, more formally a Red-Black Tree.
BSTs (like AVL and
BSTs can be used to implement symbol tables,
Red Black Trees) are used in which are used to store data such as variable and
practice. These Self-Balancing function names in a programming language.
BSTs maintain the height as
O(Log n).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy