0% found this document useful (0 votes)
246 views14 pages

Advanced Data Structures: Binary Search Tree

This document discusses binary search trees. It begins by listing the objectives of learning how to organize data in a binary search tree and understand the differences between binary trees and binary search trees. It then defines a binary search tree as a binary tree where nodes are arranged in a specific order based on their data values. The properties that make a binary tree a BST are described. The key differences between binary trees and BSTs are outlined. Examples of constructing and searching a BST are provided, along with C code for searching an element in a BST.

Uploaded by

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

Advanced Data Structures: Binary Search Tree

This document discusses binary search trees. It begins by listing the objectives of learning how to organize data in a binary search tree and understand the differences between binary trees and binary search trees. It then defines a binary search tree as a binary tree where nodes are arranged in a specific order based on their data values. The properties that make a binary tree a BST are described. The key differences between binary trees and BSTs are outlined. Examples of constructing and searching a BST are provided, along with C code for searching an element in a BST.

Uploaded by

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

Advanced Data Structures

Binary Search Tree

M.Jeevana Sujitha
Asst Professor

Department of Computer science and Engineering


SRKR Engineering College, Bhimavaram, AP-534204
Objectives

 ToArray
learn how to organise data in a binary search tree
implementation

 To understand the difference between binary tree


and binary search tree

 To understand search operation that can be


performed on binary search tree
Binary Search Tree

 It is a kind of binary tree in which the nodes are


arranged in a specific order. This is also called
ordered binary tree.

 Each node stores a data value

 Each node has atmost two children i.e left and right
child
What makes a binary tree a BST

The properties that separate a binary search tree


from a regular binary tree is
 All nodes of left subtree are less than the root node
 All nodes of right subtree are more than the root
node

Binary Search Tree Not a Binary Search Tree


Difference between Binary Tree and
Binary Search Tree

 A binary tree is a simple tree in which each node can


Thishave atmost
is why twoare
arrays children
not good to represent polynomials

 A binary search tree is a binary tree in which nodes


are assigned values with the following restrictions:
 The left subtree of a node contains only nodes
with keys lesser than the node’s key.
Difference between Binary Tree and
Binary Search Tree(Contd…)

 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.


Declaration of node in Binary Search Tree(BST)

struct node
{
int data;
struct node* left;
struct node* right;
}
struct node* root=NULL;
Construction of a BST

Construct a binary tree


with the following
values
10,12,5,4,20,8,7,15,
13
Operations on Binary Search Tree

The following operations can be performed on a binary


search tree:
 Search
 Insertion
 Deletion
 Tree traversals
 Finding height of a tree
 Count number of nodes in a given binary search
tree
How to search for a value in a BST

Step-1. Start at the root node as current node


Step-2. If the search key’s value matches the current node’s
key then found a match
Step-3. If search key’s value is greater than current node’s
3(i). If the current node has a right child, search right
3(ii). Else, no matching node in the tree
Step-4. If search key is less than the current node’s
4(i). If the current node has a left child, search left
4(ii). Else, no matching node in the tree
Example

Search for 9:
 Step-1: Compare 9 with 15(root) 9<15
go to left subtree
 Step-2: Compare 9 with 6 (9>6) go
to right subtree
 Step-3: Compare 9 with 7 (9>7) go
to right subtree
 Step-4: Compare 9 with 13 (9<13)
go to left subtree
 Step-5: Compare 9 with 9 element
found.
“C” Code for searching an element in a
binary search tree

struct node* search( int key, struct node* root)


{
if (root == NULL)
return NULL;

if (key < root->key )


return search(key, root->left);

else if (key > root->key )


return search(key, root->right);
else
return root;
}
“C” Code for searching an element in a
binary search tree(Contd…)
Thank you

You might also like