SlideShare a Scribd company logo
Binary Search Trees
Definition:
Binary search tree is a binary tree in
which the key values in the left node is
less than the root and the key values in
the right node is greater than the root.
Struct treenode
{
int Element;
SearchTree Left;
SearchTree Right;
}
insert
• To insert the element X into the tree
– Check with the root node T
– If it is less than the root
• Traverse the left sub tree recursively until it reaches
The TLeft ==NULL.
• Then X is placed in T Left
– If X is greater than the root
• Traverse the right sub tree recursively until it reaches
The TrRght ==NULL.
• Then X is placed in T Right
DS - BST
Void insert(int X, Tree T)
{
If(T==NULL)
{
T=malloc(sizeof(struct TreeNode));
if(T!=NULL)
{
TElement= X
T Left = NULL
T Right =NULL
}
}
else if (X<TElement)
Tleft=insert(X,Tleft)
Else if(X>TElement)
Tright=insert(X,Tright)
}
Find
– Check the value X with the root node value
• If X is equal to T data, return T
• If X is less than T data, traverse the left of T
recursively
• If X is greater than T data, traverse the right of T
recursively
Find
void find(int x, tree t)
{
if(T==NULL)
return NULL
if (X<Telement)
return find(X,Tleft)
else if(X>TElement)
return find(X,Tright)
else
return T
}
Findmin
– start from root go left as long as there is left child.
– Returns the position of the smallest element in
the tree
Findmin
void findmin(int X, Tree T)
{
if(T!=NULL)
while(Tleft!=NUL
L)
T=Tleft;
return T;
}
Findmax
• Return the position of largest in the tree
• start from root go left as long as there is left child.
Findmax:
void findmax(int X, Tree T)
{
if(T!=NULL)
While(TRight!=N
ULL)
T=TRight;
return T;
}
CASE 1 – node to be
deleted is a leaf node /
No children
• If the node is a leaf node,
it can be deleted
immediately
CASE 2 – Node with one
child
• If the node has one
child
– It can be deleted by
adjusting its parent
pointer that points to its
child node.
CASE 3: node with two
children
Replace the data of the node
to be deleted with its
smallest data of the right
sub tree and recursively
delete that node
void delete(int X, Tree T)
{
if(T==NULL)
Error “No tree value”;
else if (X<T-->Element)
T--> Left = Delete(X,T--> Left)
else
if(X> T> T--> Element)
T--> Right = Delete(X, T--> Right)
else
if(T--> Left && T--> Right)
{
Tmpcell = FindMin (T-->Right)
T-->Element = Tmpcell-->Element
T-->Right = Delete(T-->Element; T-->Right)
}
else
{
Tmpcell =T
if(T-->Left == NULL)
T=T-->Right
else if(T-->Right==NULL)
T=T--> Left
free(Tmpcell);
}
return T
}

More Related Content

PDF
Jan. 12 Binomial Factor Theorm
PPTX
8 elementary sorts-insertion
PPT
Avl trees
PPTX
Self organising list
PDF
Pre-Cal 30S January 14, 2009
PDF
Pre-Cal 30S January 15, 2009
PPT
Sets copy
PDF
AP Calculus January 15, 2009
Jan. 12 Binomial Factor Theorm
8 elementary sorts-insertion
Avl trees
Self organising list
Pre-Cal 30S January 14, 2009
Pre-Cal 30S January 15, 2009
Sets copy
AP Calculus January 15, 2009

What's hot (9)

PDF
sparse matrix in data structure
PPT
Array & string
PPTX
Sparse matrix
PPTX
Representation of binary tree in memory
PPT
Hashing
PPTX
Sorting and searching arrays binary search algorithm
PPT
Data Structure and Algorithms Hashing
PPT
Classification of matrix
PPTX
Introduction to matices
sparse matrix in data structure
Array & string
Sparse matrix
Representation of binary tree in memory
Hashing
Sorting and searching arrays binary search algorithm
Data Structure and Algorithms Hashing
Classification of matrix
Introduction to matices
Ad

Similar to DS - BST (20)

PDF
8 chapter4 trees_bst
PDF
PPTX
Lecture 9 data structures and algorithms
PPTX
8.binry search tree
PPT
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
PPT
Binary search tree(bst)
PPT
Binary Search Tree
PPTX
PPTX
BST.pptx this isp used for learning binary search trees
PPTX
BST.pptx this is Good for data structure
PPT
Binary Search Tree
PDF
Lecture notes data structures tree
PPTX
Data Strcutres-Non Linear DS-Advanced Trees
PPT
Data Structure and Algorithms Binary Search Tree
PPT
Bst(Binary Search Tree)
PPTX
Data structures and Algorithm analysis_Lecture4.pptx
PDF
09 binary trees
PPTX
PPT
Cinterviews Binarysearch Tree
PPTX
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
8 chapter4 trees_bst
Lecture 9 data structures and algorithms
8.binry search tree
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
Binary search tree(bst)
Binary Search Tree
BST.pptx this isp used for learning binary search trees
BST.pptx this is Good for data structure
Binary Search Tree
Lecture notes data structures tree
Data Strcutres-Non Linear DS-Advanced Trees
Data Structure and Algorithms Binary Search Tree
Bst(Binary Search Tree)
Data structures and Algorithm analysis_Lecture4.pptx
09 binary trees
Cinterviews Binarysearch Tree
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
Ad

More from MythiliMurugan3 (9)

PPTX
DS - Quick Sort
PPTX
DS - Graph Traversal
PPTX
DS- Stack ADT
PPTX
DS - Application of List
PPTX
DBMS - Relational Algebra
PPTX
DBMS - Distributed Databases
PPTX
DBMS - RAID
PPTX
DBMS - Transactions
PPTX
DBMS - ER Model
DS - Quick Sort
DS - Graph Traversal
DS- Stack ADT
DS - Application of List
DBMS - Relational Algebra
DBMS - Distributed Databases
DBMS - RAID
DBMS - Transactions
DBMS - ER Model

Recently uploaded (20)

PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Digital Logic Computer Design lecture notes
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Sustainable Sites - Green Building Construction
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
web development for engineering and engineering
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Welding lecture in detail for understanding
PDF
composite construction of structures.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Foundation to blockchain - A guide to Blockchain Tech
DOCX
573137875-Attendance-Management-System-original
PPTX
Construction Project Organization Group 2.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Digital Logic Computer Design lecture notes
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Sustainable Sites - Green Building Construction
Automation-in-Manufacturing-Chapter-Introduction.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
web development for engineering and engineering
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Welding lecture in detail for understanding
composite construction of structures.pdf
OOP with Java - Java Introduction (Basics)
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
R24 SURVEYING LAB MANUAL for civil enggi
Foundation to blockchain - A guide to Blockchain Tech
573137875-Attendance-Management-System-original
Construction Project Organization Group 2.pptx

DS - BST

  • 1. Binary Search Trees Definition: Binary search tree is a binary tree in which the key values in the left node is less than the root and the key values in the right node is greater than the root.
  • 2. Struct treenode { int Element; SearchTree Left; SearchTree Right; }
  • 3. insert • To insert the element X into the tree – Check with the root node T – If it is less than the root • Traverse the left sub tree recursively until it reaches The TLeft ==NULL. • Then X is placed in T Left – If X is greater than the root • Traverse the right sub tree recursively until it reaches The TrRght ==NULL. • Then X is placed in T Right
  • 5. Void insert(int X, Tree T) { If(T==NULL) { T=malloc(sizeof(struct TreeNode)); if(T!=NULL) { TElement= X T Left = NULL T Right =NULL } } else if (X<TElement) Tleft=insert(X,Tleft) Else if(X>TElement) Tright=insert(X,Tright) }
  • 6. Find – Check the value X with the root node value • If X is equal to T data, return T • If X is less than T data, traverse the left of T recursively • If X is greater than T data, traverse the right of T recursively
  • 7. Find void find(int x, tree t) { if(T==NULL) return NULL if (X<Telement) return find(X,Tleft) else if(X>TElement) return find(X,Tright) else return T }
  • 8. Findmin – start from root go left as long as there is left child. – Returns the position of the smallest element in the tree
  • 9. Findmin void findmin(int X, Tree T) { if(T!=NULL) while(Tleft!=NUL L) T=Tleft; return T; }
  • 10. Findmax • Return the position of largest in the tree • start from root go left as long as there is left child.
  • 11. Findmax: void findmax(int X, Tree T) { if(T!=NULL) While(TRight!=N ULL) T=TRight; return T; }
  • 12. CASE 1 – node to be deleted is a leaf node / No children • If the node is a leaf node, it can be deleted immediately
  • 13. CASE 2 – Node with one child • If the node has one child – It can be deleted by adjusting its parent pointer that points to its child node.
  • 14. CASE 3: node with two children Replace the data of the node to be deleted with its smallest data of the right sub tree and recursively delete that node
  • 15. void delete(int X, Tree T) { if(T==NULL) Error “No tree value”; else if (X<T-->Element) T--> Left = Delete(X,T--> Left) else if(X> T> T--> Element) T--> Right = Delete(X, T--> Right) else if(T--> Left && T--> Right) { Tmpcell = FindMin (T-->Right) T-->Element = Tmpcell-->Element T-->Right = Delete(T-->Element; T-->Right) }
  • 16. else { Tmpcell =T if(T-->Left == NULL) T=T-->Right else if(T-->Right==NULL) T=T--> Left free(Tmpcell); } return T }