SlideShare a Scribd company logo
1 of 26
BINARY
SEARCH TREE
-RACKSAVI.R,
I M.Sc Information Technology,
V.V.Vanniaperumal college for women,
Virudhunagar.
OBJECTIVES
 Definition of binary
search tree.
 Binary search tree
operations.
 Searching data.
 Inserting data.
 Deleting data.
OBJECTIVES Contd…
Algorithm for successor.
 Traversing the tree.
Advantages of binary
search tree.
Disadvantages of binary
search tree.
What is Binary search tree?
Binary Search Tree is a binary tree in
which every node contains only smaller
values in its left subtree and only larger
values in its right subtree.
left_subtree(Keys) <=
node(Keys) <=
right_subtree(Keys)
Operations on binary
search tree
 Searching data.
 Inserting data.
 Deleting data.
 Traversing the tree.
Searching in Binary search tree
Searching operation on a binary search tree,
searches for a node in the tree
Algorithm Search_BST:
Input : ITEM is the data that has to
be searched.
Output : If found then pointer to the
node containing data ITEM else a
message.
Data Structure : Linked Structure
of the binary tree.
STEPS:
1. Ptr = ROOT,flag = FALSE
2. While (ptr =! NULL) and (flag = FALSE) do
3. Case: ITEM < ptr->DATA
4. ptr = ptr->LCHILD
5. Case: ptr->DATA = ITEM
6. flag = TRUE
7. Case: ITEM > ptr->DATA
8. ptr = ptr->RCHILD
9. EndCase
10. EndWhile
11. If (flag = TRUE) then
12. Print “ITEM has found at the node”, ptr
13. Else
14. Print “Search is unsuccessful”
15. EndIf
16. Stop
Inserting in Binary search tree
Inserting operation on a binary search tree,
insert a node in the tree.
Algorithm Insert_BST:
Input : ITEM is the data
component of a node that has to
be inserted.
Output : If there is no node having
data ITEM, it is inserted
into the tree else a message.
Data Structure : Linked Structure of
the binary tree.
STEPS:
1. Ptr = ROOT,flag = FALSE
2. While (ptr =! NULL) and (flag = FALSE) do
3. Case: ITEM < ptr->DATA
4. ptr1 = ptr
5. ptr = ptr->LCHILD
6. Case: ITEM > ptr->DATA
7. ptr1 = ptr
8. ptr = ptr->RCHILD
9. Case: ptr->DATA = ITEM
10. flag = TRUE
11. Print “ITEM already exists”
12. Exit
13. EndCase
14. EndWhile
15. If (ptr = NULL) then
16. new = GetNode(NODE)
17. new->DATA = ITEM
18. new->LCHILD = NULL
19. new->RCHILD = NULL
20. If(ptr1->DATA < ITEM) then
21. ptr1->RCHILD = new
22. Else
23. ptr->RCHILD = new
24. EndIf
25. EndIf
26. Stop
Deletion in Binary search
tree
Deletion operation on a binary search tree,
deletes a node from the tree.
Deletion of the node depends on the
number of its children. Hence, 3 cases
may arise :
Case 1 : N is the leaf node.
Case 2 : N has exactly one child.
Case 3 : N has two childs.
Algorithm Delete_BST:
Input : ITEM is the data of the nodes to
be deleted.
Output : If the node with data ITEM
exists it is deleted else a message.
Data Structure : Linked Structure of the
binary tree.
STEPS:
1. Ptr = ROOT,flag = FALSE
2. While (ptr =! NULL) and (flag = FLASE) do
3. Case: ITEM < ptr->DATA
4. parent = ptr
5. Ptr = ptr->LCHILD
6. Case: ITEM > ptr->DATA
7. parent = ptr
8. ptr = ptr->RCHILD
9. Case: ptr->DATA = ITEM
10. flag = TRUE
11. EndCase
12. EndWhile
13. If (flag = FALSE) then
14. Print “ No deletion”
15. Exit
16. EndIf
*// DECIDE THE CASE OF DELETION //*
17. If(ptr->LCHILD = NULL) and (ptr->RCHILD
= NULL) then
18. Case = 1
19. Else
20. If(ptr->LCHILD =! NULL) and
(ptr->RCHILD =! NULL) then
21. Case = 3
22. Else
23. Case = 2
24. EndIf
25. EndIf
*// DELETION : CASE 1 //*
26.If ( Case = 1) then
27. If (parent->LCHILD = ptr)then
28. parent->LCHILD = NULL
29. Else
30. parent->RCHILD = NULL
31. EndIf
32. ReturnNode(ptr)
33. EndIf
*// DELETION : CASE 2 //*
34.If (Case = 2) then
35. If (parent->LCHILD = ptr) then
36. If (ptr->LCHILD = NULL) then
37. parent->LCHILD = ptr->RCHILD
38. Else
39. parent->LCHILD = ptr->LCHILD
40. EndIf
41. Else
42. If (parent->RCHILD = ptr) then
43. If (ptr->LCHILD = NULL) then
44. parent->RCHILD = ptr->RCHILD
45. Else
46. parent->RCHILD = ptr->LCHILD
47. EndIf
48. EndIf
49. EndIf
50. ReturnNode(ptr)
51. EndIf
*// DELETION : CASE 3 //*
52. If (Case = 3 )
53. ptr1 = SUCC(ptr)
54. item1 = ptr->DATA
55. Delete_BST (item1)
56. ptr->DATA = item1
57. EndIf
58. Stop
Algorithm for Succossor
Input : Pointer to a node PTR whose
inorder successor is to be found.
Output : Pointer to the inorder successor
of ptr.
Data Structure : Linked Structure of
the binary tree.
1. ptr1 = PTR->RCHILD
2. If ( ptr1 =! NULL) then
3. While(ptr1->LCHILD =! NULL) do
4. ptr1 = ptr1->LCHILD
5. EndWhile
6. EndIf
7. Return( ptr1 )
8. Stop
Traversal in Binary search tree
Traversal operation on a binary
search tree, visits every node of the tree.
There are three types of traversal
they are :
 In-order traversal.
 Pre-order traversal.
 Post-order traversal.
Advantages of Binary search tree
The major advantage of
binary search trees over other data
structures is that the related sorting
algorithms and search algorithms
such as in – order traversal can be
very efficient.
Disadvantages of Binary search tree
The shape of the Binary search
tree totally depends on the order
of insertions and it can be
regenerated
 It takes a long time to search an
element in a BST because key
value of each node has to be
compared with the key element to
be searched
Binary search tree

More Related Content

What's hot

Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
BinarySearchTree-bddicken
BinarySearchTree-bddickenBinarySearchTree-bddicken
BinarySearchTree-bddickenBenjamin Dicken
 
Binary search tree
Binary search treeBinary search tree
Binary search treeKousalya M
 
Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Abbott
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2Abbott
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureRai University
 
Mining Algorithm for Weighted FP-Growth Frequent Item Sets based on Ordered F...
Mining Algorithm for Weighted FP-Growth Frequent Item Sets based on Ordered F...Mining Algorithm for Weighted FP-Growth Frequent Item Sets based on Ordered F...
Mining Algorithm for Weighted FP-Growth Frequent Item Sets based on Ordered F...Dr. Amarjeet Singh
 
An algorithm for building
An algorithm for buildingAn algorithm for building
An algorithm for buildingajmal_fuuast
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)Waheed Khalid
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in indiaEdhole.com
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureRai University
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeZafar Ayub
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبياناتRafal Edward
 

What's hot (20)

Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
BinarySearchTree-bddicken
BinarySearchTree-bddickenBinarySearchTree-bddicken
BinarySearchTree-bddicken
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
 
Data Structures
Data StructuresData Structures
Data Structures
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
 
Heap tree
Heap treeHeap tree
Heap tree
 
Lesson 2.2 abstraction
Lesson 2.2   abstractionLesson 2.2   abstraction
Lesson 2.2 abstraction
 
Mining Algorithm for Weighted FP-Growth Frequent Item Sets based on Ordered F...
Mining Algorithm for Weighted FP-Growth Frequent Item Sets based on Ordered F...Mining Algorithm for Weighted FP-Growth Frequent Item Sets based on Ordered F...
Mining Algorithm for Weighted FP-Growth Frequent Item Sets based on Ordered F...
 
binary_search
binary_searchbinary_search
binary_search
 
An algorithm for building
An algorithm for buildingAn algorithm for building
An algorithm for building
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Chapter 9 ds
Chapter 9 dsChapter 9 ds
Chapter 9 ds
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in india
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 

Similar to Binary search tree

1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil duttAnil Dutt
 
Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptDrBashirMSaad
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary treeArunaP47
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureMeghaj Mallick
 
Add these three functions to the class binaryTreeType (provided).W.pdf
Add these three functions to the class binaryTreeType (provided).W.pdfAdd these three functions to the class binaryTreeType (provided).W.pdf
Add these three functions to the class binaryTreeType (provided).W.pdfindiaartz
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdferremmfab
 
Data structures lecture 04
Data structures  lecture 04Data structures  lecture 04
Data structures lecture 04Nazir Ahmed
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptxRedHeart11
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxasimshahzad8611
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary treeZaid Shabbir
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)Trupti Agrawal
 

Similar to Binary search tree (20)

1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary tree
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Add these three functions to the class binaryTreeType (provided).W.pdf
Add these three functions to the class binaryTreeType (provided).W.pdfAdd these three functions to the class binaryTreeType (provided).W.pdf
Add these three functions to the class binaryTreeType (provided).W.pdf
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdf
 
Data Structures
Data StructuresData Structures
Data Structures
 
Data structures lecture 04
Data structures  lecture 04Data structures  lecture 04
Data structures lecture 04
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
Tree
TreeTree
Tree
 
Binary tree
Binary treeBinary tree
Binary tree
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
LEC 6-DS ALGO(updated).pdf
LEC 6-DS  ALGO(updated).pdfLEC 6-DS  ALGO(updated).pdf
LEC 6-DS ALGO(updated).pdf
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
Materi Searching
Materi Searching Materi Searching
Materi Searching
 
Lab12 dsa bsee20075
Lab12 dsa bsee20075Lab12 dsa bsee20075
Lab12 dsa bsee20075
 
L3
L3L3
L3
 

Recently uploaded

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Recently uploaded (20)

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

Binary search tree

  • 1. BINARY SEARCH TREE -RACKSAVI.R, I M.Sc Information Technology, V.V.Vanniaperumal college for women, Virudhunagar.
  • 2. OBJECTIVES  Definition of binary search tree.  Binary search tree operations.  Searching data.  Inserting data.  Deleting data.
  • 3. OBJECTIVES Contd… Algorithm for successor.  Traversing the tree. Advantages of binary search tree. Disadvantages of binary search tree.
  • 4. What is Binary search tree? Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree. left_subtree(Keys) <= node(Keys) <= right_subtree(Keys)
  • 5. Operations on binary search tree  Searching data.  Inserting data.  Deleting data.  Traversing the tree.
  • 6. Searching in Binary search tree Searching operation on a binary search tree, searches for a node in the tree Algorithm Search_BST: Input : ITEM is the data that has to be searched. Output : If found then pointer to the node containing data ITEM else a message. Data Structure : Linked Structure of the binary tree.
  • 7. STEPS: 1. Ptr = ROOT,flag = FALSE 2. While (ptr =! NULL) and (flag = FALSE) do 3. Case: ITEM < ptr->DATA 4. ptr = ptr->LCHILD 5. Case: ptr->DATA = ITEM 6. flag = TRUE
  • 8. 7. Case: ITEM > ptr->DATA 8. ptr = ptr->RCHILD 9. EndCase 10. EndWhile 11. If (flag = TRUE) then 12. Print “ITEM has found at the node”, ptr 13. Else 14. Print “Search is unsuccessful” 15. EndIf 16. Stop
  • 9. Inserting in Binary search tree Inserting operation on a binary search tree, insert a node in the tree. Algorithm Insert_BST: Input : ITEM is the data component of a node that has to be inserted. Output : If there is no node having data ITEM, it is inserted into the tree else a message.
  • 10. Data Structure : Linked Structure of the binary tree. STEPS: 1. Ptr = ROOT,flag = FALSE 2. While (ptr =! NULL) and (flag = FALSE) do 3. Case: ITEM < ptr->DATA 4. ptr1 = ptr 5. ptr = ptr->LCHILD 6. Case: ITEM > ptr->DATA 7. ptr1 = ptr 8. ptr = ptr->RCHILD 9. Case: ptr->DATA = ITEM 10. flag = TRUE
  • 11. 11. Print “ITEM already exists” 12. Exit 13. EndCase 14. EndWhile 15. If (ptr = NULL) then 16. new = GetNode(NODE) 17. new->DATA = ITEM 18. new->LCHILD = NULL
  • 12. 19. new->RCHILD = NULL 20. If(ptr1->DATA < ITEM) then 21. ptr1->RCHILD = new 22. Else 23. ptr->RCHILD = new 24. EndIf 25. EndIf 26. Stop
  • 13. Deletion in Binary search tree Deletion operation on a binary search tree, deletes a node from the tree. Deletion of the node depends on the number of its children. Hence, 3 cases may arise : Case 1 : N is the leaf node. Case 2 : N has exactly one child. Case 3 : N has two childs.
  • 14. Algorithm Delete_BST: Input : ITEM is the data of the nodes to be deleted. Output : If the node with data ITEM exists it is deleted else a message. Data Structure : Linked Structure of the binary tree. STEPS: 1. Ptr = ROOT,flag = FALSE 2. While (ptr =! NULL) and (flag = FLASE) do 3. Case: ITEM < ptr->DATA 4. parent = ptr
  • 15. 5. Ptr = ptr->LCHILD 6. Case: ITEM > ptr->DATA 7. parent = ptr 8. ptr = ptr->RCHILD 9. Case: ptr->DATA = ITEM 10. flag = TRUE 11. EndCase 12. EndWhile 13. If (flag = FALSE) then 14. Print “ No deletion” 15. Exit
  • 16. 16. EndIf *// DECIDE THE CASE OF DELETION //* 17. If(ptr->LCHILD = NULL) and (ptr->RCHILD = NULL) then 18. Case = 1 19. Else 20. If(ptr->LCHILD =! NULL) and (ptr->RCHILD =! NULL) then 21. Case = 3 22. Else 23. Case = 2 24. EndIf
  • 17. 25. EndIf *// DELETION : CASE 1 //* 26.If ( Case = 1) then 27. If (parent->LCHILD = ptr)then 28. parent->LCHILD = NULL 29. Else 30. parent->RCHILD = NULL 31. EndIf 32. ReturnNode(ptr) 33. EndIf
  • 18. *// DELETION : CASE 2 //* 34.If (Case = 2) then 35. If (parent->LCHILD = ptr) then 36. If (ptr->LCHILD = NULL) then 37. parent->LCHILD = ptr->RCHILD 38. Else 39. parent->LCHILD = ptr->LCHILD 40. EndIf 41. Else 42. If (parent->RCHILD = ptr) then 43. If (ptr->LCHILD = NULL) then 44. parent->RCHILD = ptr->RCHILD
  • 19. 45. Else 46. parent->RCHILD = ptr->LCHILD 47. EndIf 48. EndIf 49. EndIf 50. ReturnNode(ptr) 51. EndIf
  • 20. *// DELETION : CASE 3 //* 52. If (Case = 3 ) 53. ptr1 = SUCC(ptr) 54. item1 = ptr->DATA 55. Delete_BST (item1) 56. ptr->DATA = item1 57. EndIf 58. Stop
  • 21. Algorithm for Succossor Input : Pointer to a node PTR whose inorder successor is to be found. Output : Pointer to the inorder successor of ptr. Data Structure : Linked Structure of the binary tree.
  • 22. 1. ptr1 = PTR->RCHILD 2. If ( ptr1 =! NULL) then 3. While(ptr1->LCHILD =! NULL) do 4. ptr1 = ptr1->LCHILD 5. EndWhile 6. EndIf 7. Return( ptr1 ) 8. Stop
  • 23. Traversal in Binary search tree Traversal operation on a binary search tree, visits every node of the tree. There are three types of traversal they are :  In-order traversal.  Pre-order traversal.  Post-order traversal.
  • 24. Advantages of Binary search tree The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in – order traversal can be very efficient.
  • 25. Disadvantages of Binary search tree The shape of the Binary search tree totally depends on the order of insertions and it can be regenerated  It takes a long time to search an element in a BST because key value of each node has to be compared with the key element to be searched