SlideShare a Scribd company logo
2
Most read
4
Most read
5
Most read
Binary Search Tree
Aakash Montheiro - 161702
Abhishek.L.R - 161704
Deelan Jostan Monthero - 161726
I Sem - M.C.A
Aloysius Institute Of Management &
Information Technology
What is …?
• A Binary tree is a non-linear data structure which is a collection of elements
called nodes.
• A Binary Search Tree (BST) is a Binary tree in which all the nodes follow the
below-mentioned properties :
The left sub-tree of a node has a key less than or equal to its parent node's key.
The right sub-tree of a node has a key greater than to its parent node's key.
• Thus, BST divides all its sub-trees into two segments;
 Left sub-tree
 Right sub-tree
Operations Performed in a BST:
Search − Searches an element in a tree.
Insert − Inserts an element in a tree.
Traversal :
Pre-order Traversal − Traverses a tree in a pre-order manner. [Root->Left->Right]
In-order Traversal − Traverses a tree in an in-order manner. [Left->Root->Right]
Post-order Traversal − Traverses a tree in a post-order manner.[Left->Right->Root]
Example:
Given : 37 ,24 ,45 ,20 ,29 ,41,52
37
24 45
20 29 41 52
Binary Tree v/s Binary Search Tree
Given : 37 ,45 ,24 ,29 ,41 ,20 ,24 ,52
24
37
24 45
20 29 41 52
37
24 45
20 29 41 52
37
24 45
20 29 41 52
Implementation:
• Creating the Structure
typedef struct Tree{
int Data;
struct Tree *Right;
struct Tree * Left;
}TREENODE;
Data *Right*Left
Null Null
Implementation Contd..
class BSTree{
TREENODE *Root;
public:
BTree();//--Constructor
TREENODE *MakeTreeNode(int Num);
void InsertTreeNode(int Num);
void Preorder(TREENODE *Root);
void Postorder(TREENODE *Root);
void Inorder(TREENODE *Root);
TREENODE *GetRoot();
int TreeSearch(int Num);
};
Creating the Class:
Implementation Contd…
BTree::BTree()
{
Root=NULL;
}
TREENODE *BTree::MakeTreeNode(int Num)
{
TREENODE *New;
New=(TREENODE *)malloc(sizeof(TREENODE));
New->Data=Num;
New->Right=NULL;
New->Left=NULL;
return New;
}
TREENODE *BTree::GetRoot()
{
return Root;
}
Constructor
Getting the Root Node
Creating the Node
Implementation Contd…
void BTree::InsertTreeNode(int Num)
{
TREENODE *New,*Cur,*Prev;
Cur=Prev=Root;
New=MakeTreeNode(Num);
if(Root==NULL){
Root=New;
return;
}
Inserting a Element to a tree:
Implementation Contd…
Inserting a Element to a tree Contd:
while(Cur){
if(Num>Cur->Data){
Prev=Cur;
Cur=Cur->Right;
}else{
Prev=Cur;
Cur=Cur->Left;
}
}
if(Num>Prev->Data){
Prev->Right=New;
}else{
Prev->Left=New;
}
return;
}
Implementation Contd…
void BTree::Preorder(TREENODE *Root)
{
if(Root){
cout<<Root->Data<<"t""";
Preorder(Root->Left);
Preorder(Root->Right);
}
}
void BTree::Postorder(TREENODE *Root)
{
if(Root){
Postorder(Root->Left);
Postorder(Root->Right);
cout<<Root->Data<<"t";
}
}
void BTree::Inorder(TREENODE *Root)
{
if(Root){
Inorder(Root->Left);
cout<<Root->Data<<"t";
Inorder(Root->Right);
}
}
Pre-Order
In-OrderPost-Order
Traversing the Tree
int BTree::TreeSearch(int Num)
{
TREENODE *Cur;
Cur=Root;
while(Cur){
if(Num==Cur->Data){
return 1;
}else if(Num>Cur->Data){
Cur=Cur->Right;
}else{
Cur=Cur->Left;
}
}
}
Implementation Contd…
Searching the Element
Implementation Contd…
Inserting elements in Main Program cout<<"Enter the value of N : ";
cin>>N;
cout<<"Enter the elements n";
for(i=1;i<=N;i++){
cout<<"Enter the element A["<<i<<"] : ";
cin>>Num;
BT.InsertTreeNode(Num);
}
Declaration in Main Program
TREENODE *Root;
BTree BT;
Implementation Contd…
Root=BT.GetRoot();
cout<<"Pre-order traverse is : ";
BT.Preorder(Root);
cout<<"n";
break;
Traversing the TreeSearching of Elements
cout<<"Enter the Search element : ";
cin>>Search;
Res=BT.TreeSearch(Search);
if(Res==1){
cout<<"Element "<<Search<<" Found...n";
}else{
cout<<"Element "<<Search<<" Not-found....n";
}
break;
Click here to download Source Code
Thank You
For
Your
Attention !
Any Question

More Related Content

PPT
Binary tree
PPT
1.5 binary search tree
PPTX
PPTX
Binary Tree in Data Structure
PDF
Binary tree
PPT
Data Structure and Algorithms Binary Search Tree
PPSX
Data Structure (Tree)
PPTX
Tree traversal techniques
Binary tree
1.5 binary search tree
Binary Tree in Data Structure
Binary tree
Data Structure and Algorithms Binary Search Tree
Data Structure (Tree)
Tree traversal techniques

What's hot (20)

PPT
Binary search tree(bst)
PDF
Binary search tree operations
PPT
1.1 binary tree
PPTX
Red black trees
PPTX
Doubly Linked List
PPTX
AVL Tree Data Structure
PPTX
B and B+ tree
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
PPTX
Binary Search Tree
PPTX
Priority Queue in Data Structure
PPTX
PPTX
Binary Search Tree in Data Structure
PPTX
Threaded Binary Tree.pptx
PPTX
Tree Traversal
PPT
BINARY TREE REPRESENTATION.ppt
PPT
Binary search tree in data structures
PDF
AVL tree ( Balanced Binary Search Tree)-Data Structure
PPTX
Priority queue in DSA
PPTX
Queue in Data Structure
PDF
Red black tree
Binary search tree(bst)
Binary search tree operations
1.1 binary tree
Red black trees
Doubly Linked List
AVL Tree Data Structure
B and B+ tree
Trees, Binary Search Tree, AVL Tree in Data Structures
Binary Search Tree
Priority Queue in Data Structure
Binary Search Tree in Data Structure
Threaded Binary Tree.pptx
Tree Traversal
BINARY TREE REPRESENTATION.ppt
Binary search tree in data structures
AVL tree ( Balanced Binary Search Tree)-Data Structure
Priority queue in DSA
Queue in Data Structure
Red black tree
Ad

Viewers also liked (20)

PPT
Linked list
PPTX
Doubly linked list
PPT
linked list
PPTX
Trees data structure
PPTX
7 Myths of AI
PPTX
Data structure using c module 1
PPTX
Linked list
PDF
Searching and Sorting Techniques in Data Structure
PPT
358 33 powerpoint-slides_8-linked-lists_chapter-8
PPTX
Array implementation and linked list as datat structure
PPTX
6. Linked list - Data Structures using C++ by Varsha Patil
PPTX
Double linked list
PDF
Cyber Crime
PDF
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
PPTX
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)
PPTX
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
DOC
موقع سلايد شير
PPTX
Open Legal Data Workshop at Stanford
PPTX
Harry Surden - Artificial Intelligence and Law Overview
PPTX
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
Linked list
Doubly linked list
linked list
Trees data structure
7 Myths of AI
Data structure using c module 1
Linked list
Searching and Sorting Techniques in Data Structure
358 33 powerpoint-slides_8-linked-lists_chapter-8
Array implementation and linked list as datat structure
6. Linked list - Data Structures using C++ by Varsha Patil
Double linked list
Cyber Crime
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
موقع سلايد شير
Open Legal Data Workshop at Stanford
Harry Surden - Artificial Intelligence and Law Overview
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
Ad

Similar to Binary Search Tree (20)

PPTX
presentation 1 binary search tree in data structures.pptx
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
PPTX
learn tree, linked list, queue, stack, and other algo
PPTX
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
PDF
Unit iv data structure-converted
PPTX
BST binary search tree with tree traversals.pptx
PPTX
Lec 10_Binary Search Tree in data structure and algorithm.pptx
PDF
Binary search tree with pre-order inorder and postorder
PPTX
Binary Search Tree in Data Structure
PPTX
Lecture_10 - Revised.pptx
PPTX
BINARY TREE data structure and algorithm-1.pptx
PPT
Lecture 7-BinarySearchTrees.ppt
PPTX
BST.pptx this is Good for data structure
PPTX
BST.pptx this isp used for learning binary search trees
PPTX
Lecture 09 - Binary Search Trees.pptx mission Sk it
PPTX
datastructurestreeand type of trees.pptx
PPTX
PPTX
Binary tree
PPTX
Binary search tree12345678901234567890.pptx
PDF
Binary search tree
presentation 1 binary search tree in data structures.pptx
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
learn tree, linked list, queue, stack, and other algo
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
Unit iv data structure-converted
BST binary search tree with tree traversals.pptx
Lec 10_Binary Search Tree in data structure and algorithm.pptx
Binary search tree with pre-order inorder and postorder
Binary Search Tree in Data Structure
Lecture_10 - Revised.pptx
BINARY TREE data structure and algorithm-1.pptx
Lecture 7-BinarySearchTrees.ppt
BST.pptx this is Good for data structure
BST.pptx this isp used for learning binary search trees
Lecture 09 - Binary Search Trees.pptx mission Sk it
datastructurestreeand type of trees.pptx
Binary tree
Binary search tree12345678901234567890.pptx
Binary search tree

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PDF
Electronic commerce courselecture one. Pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Modernizing your data center with Dell and AMD
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Big Data Technologies - Introduction.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Cloud computing and distributed systems.
Teaching material agriculture food technology
Electronic commerce courselecture one. Pdf
NewMind AI Weekly Chronicles - August'25 Week I
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
The AUB Centre for AI in Media Proposal.docx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
Empathic Computing: Creating Shared Understanding
Network Security Unit 5.pdf for BCA BBA.
Modernizing your data center with Dell and AMD
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Review of recent advances in non-invasive hemoglobin estimation
Unlocking AI with Model Context Protocol (MCP)
Big Data Technologies - Introduction.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

Binary Search Tree

  • 1. Binary Search Tree Aakash Montheiro - 161702 Abhishek.L.R - 161704 Deelan Jostan Monthero - 161726 I Sem - M.C.A Aloysius Institute Of Management & Information Technology
  • 2. What is …? • A Binary tree is a non-linear data structure which is a collection of elements called nodes. • A Binary Search Tree (BST) is a Binary tree in which all the nodes follow the below-mentioned properties : The left sub-tree of a node has a key less than or equal to its parent node's key. The right sub-tree of a node has a key greater than to its parent node's key. • Thus, BST divides all its sub-trees into two segments;  Left sub-tree  Right sub-tree
  • 3. Operations Performed in a BST: Search − Searches an element in a tree. Insert − Inserts an element in a tree. Traversal : Pre-order Traversal − Traverses a tree in a pre-order manner. [Root->Left->Right] In-order Traversal − Traverses a tree in an in-order manner. [Left->Root->Right] Post-order Traversal − Traverses a tree in a post-order manner.[Left->Right->Root]
  • 4. Example: Given : 37 ,24 ,45 ,20 ,29 ,41,52 37 24 45 20 29 41 52
  • 5. Binary Tree v/s Binary Search Tree Given : 37 ,45 ,24 ,29 ,41 ,20 ,24 ,52 24 37 24 45 20 29 41 52 37 24 45 20 29 41 52 37 24 45 20 29 41 52
  • 6. Implementation: • Creating the Structure typedef struct Tree{ int Data; struct Tree *Right; struct Tree * Left; }TREENODE; Data *Right*Left Null Null
  • 7. Implementation Contd.. class BSTree{ TREENODE *Root; public: BTree();//--Constructor TREENODE *MakeTreeNode(int Num); void InsertTreeNode(int Num); void Preorder(TREENODE *Root); void Postorder(TREENODE *Root); void Inorder(TREENODE *Root); TREENODE *GetRoot(); int TreeSearch(int Num); }; Creating the Class:
  • 8. Implementation Contd… BTree::BTree() { Root=NULL; } TREENODE *BTree::MakeTreeNode(int Num) { TREENODE *New; New=(TREENODE *)malloc(sizeof(TREENODE)); New->Data=Num; New->Right=NULL; New->Left=NULL; return New; } TREENODE *BTree::GetRoot() { return Root; } Constructor Getting the Root Node Creating the Node
  • 9. Implementation Contd… void BTree::InsertTreeNode(int Num) { TREENODE *New,*Cur,*Prev; Cur=Prev=Root; New=MakeTreeNode(Num); if(Root==NULL){ Root=New; return; } Inserting a Element to a tree:
  • 10. Implementation Contd… Inserting a Element to a tree Contd: while(Cur){ if(Num>Cur->Data){ Prev=Cur; Cur=Cur->Right; }else{ Prev=Cur; Cur=Cur->Left; } } if(Num>Prev->Data){ Prev->Right=New; }else{ Prev->Left=New; } return; }
  • 11. Implementation Contd… void BTree::Preorder(TREENODE *Root) { if(Root){ cout<<Root->Data<<"t"""; Preorder(Root->Left); Preorder(Root->Right); } } void BTree::Postorder(TREENODE *Root) { if(Root){ Postorder(Root->Left); Postorder(Root->Right); cout<<Root->Data<<"t"; } } void BTree::Inorder(TREENODE *Root) { if(Root){ Inorder(Root->Left); cout<<Root->Data<<"t"; Inorder(Root->Right); } } Pre-Order In-OrderPost-Order Traversing the Tree
  • 12. int BTree::TreeSearch(int Num) { TREENODE *Cur; Cur=Root; while(Cur){ if(Num==Cur->Data){ return 1; }else if(Num>Cur->Data){ Cur=Cur->Right; }else{ Cur=Cur->Left; } } } Implementation Contd… Searching the Element
  • 13. Implementation Contd… Inserting elements in Main Program cout<<"Enter the value of N : "; cin>>N; cout<<"Enter the elements n"; for(i=1;i<=N;i++){ cout<<"Enter the element A["<<i<<"] : "; cin>>Num; BT.InsertTreeNode(Num); } Declaration in Main Program TREENODE *Root; BTree BT;
  • 14. Implementation Contd… Root=BT.GetRoot(); cout<<"Pre-order traverse is : "; BT.Preorder(Root); cout<<"n"; break; Traversing the TreeSearching of Elements cout<<"Enter the Search element : "; cin>>Search; Res=BT.TreeSearch(Search); if(Res==1){ cout<<"Element "<<Search<<" Found...n"; }else{ cout<<"Element "<<Search<<" Not-found....n"; } break; Click here to download Source Code