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

PDF
binary_trees4
PPTX
Binary Tree in Data Structure
PPT
Binary search trees (1)
PPTX
Binomial Heaps and Fibonacci Heaps
DOCX
Discrete assignment
PPT
Binary Search Tree and AVL
PPTX
Binary Search Tree
binary_trees4
Binary Tree in Data Structure
Binary search trees (1)
Binomial Heaps and Fibonacci Heaps
Discrete assignment
Binary Search Tree and AVL
Binary Search Tree

What's hot (20)

PPTX
Binary Search Tree in Data Structure
PDF
BinarySearchTree-bddicken
PPTX
Binary search tree
PDF
Data structure lecture 2 (pdf)
DOC
Data structure lecture 2
PPTX
Data Structures
PPTX
Bsc cs ii dfs u-1 introduction to data structure
PPTX
Heap tree
PDF
Lesson 2.2 abstraction
PDF
Mining Algorithm for Weighted FP-Growth Frequent Item Sets based on Ordered F...
PDF
binary_search
PPTX
An algorithm for building
PPSX
data structure(tree operations)
PDF
Binary Trees
PPTX
Binary trees1
PPT
Chapter 9 ds
PPT
Mca admission in india
PPTX
Bca ii dfs u-1 introduction to data structure
PPT
Binary Search Tree
PPTX
هياكلبيانات
Binary Search Tree in Data Structure
BinarySearchTree-bddicken
Binary search tree
Data structure lecture 2 (pdf)
Data structure lecture 2
Data Structures
Bsc cs ii dfs u-1 introduction to data structure
Heap tree
Lesson 2.2 abstraction
Mining Algorithm for Weighted FP-Growth Frequent Item Sets based on Ordered F...
binary_search
An algorithm for building
data structure(tree operations)
Binary Trees
Binary trees1
Chapter 9 ds
Mca admission in india
Bca ii dfs u-1 introduction to data structure
Binary Search Tree
هياكلبيانات
Ad

Similar to Binary search tree (20)

PPTX
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
PPT
1.1 binary tree
PPT
Introduction to data structure by anil dutt
PDF
tree basics to advanced all concepts part 2
PPT
Lecture 7-BinarySearchTrees.ppt
PPTX
Threaded binary tree
PPTX
Binary Search Tree in Data Structure
PPTX
Data Str Data Str Data Str Data Str Data Str
PDF
Add these three functions to the class binaryTreeType (provided).W.pdf
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PDF
create a binary search tree from an empty one by adding the key valu.pdf
PPTX
Data Structures
PPT
Data structures lecture 04
PPTX
Lecture_10 - Revised.pptx
PPT
FRbsbsvvsvsvbshgsgsvzvsvvsvsvsvsvsvvev.ppt
PPTX
PPTX
Data Strcutres-Non Linear DS-Advanced Trees
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
PPTX
Binary tree
PDF
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
1.1 binary tree
Introduction to data structure by anil dutt
tree basics to advanced all concepts part 2
Lecture 7-BinarySearchTrees.ppt
Threaded binary tree
Binary Search Tree in Data Structure
Data Str Data Str Data Str Data Str Data Str
Add these three functions to the class binaryTreeType (provided).W.pdf
UNIT III Non Linear Data Structures - Trees.pptx
create a binary search tree from an empty one by adding the key valu.pdf
Data Structures
Data structures lecture 04
Lecture_10 - Revised.pptx
FRbsbsvvsvsvbshgsgsvzvsvvsvsvsvsvsvvev.ppt
Data Strcutres-Non Linear DS-Advanced Trees
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
Binary tree
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Ad

Recently uploaded (20)

PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
advance database management system book.pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
International_Financial_Reporting_Standa.pdf
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
Hazard Identification & Risk Assessment .pdf
PPTX
Core Concepts of Personalized Learning and Virtual Learning Environments
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
Complications of Minimal Access-Surgery.pdf
PPTX
Virtual and Augmented Reality in Current Scenario
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
advance database management system book.pdf
Introduction to pro and eukaryotes and differences.pptx
International_Financial_Reporting_Standa.pdf
Unit 4 Computer Architecture Multicore Processor.pptx
Hazard Identification & Risk Assessment .pdf
Core Concepts of Personalized Learning and Virtual Learning Environments
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Environmental Education MCQ BD2EE - Share Source.pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
Uderstanding digital marketing and marketing stratergie for engaging the digi...
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Complications of Minimal Access-Surgery.pdf
Virtual and Augmented Reality in Current Scenario
What’s under the hood: Parsing standardized learning content for AI
FORM 1 BIOLOGY MIND MAPS and their schemes
Journal of Dental Science - UDMY (2021).pdf
Paper A Mock Exam 9_ Attempt review.pdf.
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
MBA _Common_ 2nd year Syllabus _2021-22_.pdf

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