SlideShare a Scribd company logo
BINARY SEARCH TREES
Fariha Tasmin Jaigirdar
Assistant Professor
Daffodil International University
BINARY SEARCH TREES
A Binary Search Tree is a binary tree, which is either empty or
satisfies the following properties :
1. Every node has a value and no two nodes have the same value (i.e.,
all the values are unique) root.
2. If there exists a left child or left sub tree then its value is less than the
value of the
3. The value(s) in the right child or right sub tree is larger than the value of
the root node.
BINARY SEARCH TREES cont.
Example: Constructing BST
Figure 8.12 shows a binary search tree.
Notice that this tree is obtained by inserting
the values
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
in that order, starting from an empty tree.
Fig 8.12
Operation on BST
INSERTING A NODE
Example: INSERTING A NODE
After inserting 55
10/29/08 Shaily Kabir,Dept. of CSE, DU 8
Inserting a Node into a BST
Rules
• Examine the root R.
• If the new value is less than the root’s, then it must be inserted at the left subtree.
• Otherwise it must be inserted at the right subtree.
• This process continues until the new value is compared with a leaf node and then it is
added as that node’s (leaf node) left or right child depending on its value.
Algorithm
• Insert_BST(Root, Left, Right, Item)
1. Set NewNode.Value := Item.
2. Insert(Root, NewNode).
End.
Here,
Root = Root of BST
Left = Left Subtree
Right = Right Subtree
Item = New Item to be inserted
10/29/08 Shaily Kabir,Dept. of CSE, DU 9
Insert(Node, NewNode)
1. If Node = NULL then Node := NewNode and return.
2. If NewNode.Value < Node.Value then Insert(Node.Left, NewNode).
3. Else Insert(Node.Right, NewNode).
Example:
Figure: BST with 5 Nodes
New Item: 110
FF.Value=110 (FF is the New Node address)
Steps:
1. Insert(Root, FF) Insert(AA, FF)
2. Insert(AA->Right, FF) Insert(CC, FF)
3. Insert(CC->Right, FF)
AA
BB CC
DD EE
15
13 100
12 90 110
FF
DELETING A NODE
Often you need to delete an element from a binary search tree. Doing
so is far more complex than adding an element into a binary search
tree.
To delete an element from a binary search tree, you need to first locate
the node that contains the element and also its parent node. Let current
point to the node that contains the element in the binary search tree
and parent point to the parent of the current node. The current node
may be a left child or a right child of the parent node.
There are two cases to consider:
Case 1: The current node does not have a left child, as shown in Figure
1(a). Simply connect the parent with the right child of the current node, as
shown in Figure 1(b).
DELETING A NODE cont.
Figure 1 Case 1: the current node has no left child.
DELETING A NODE: Case 1
Figure 2 Case 1: deleting node 10 from (a) results in (b).
For example, to delete node 10 in Figure 2(a). Connect the
parent of node 10 with the right child of node 10, as shown in
Figure 2(b).
Case 2: The current node has a left child.
Let rightMost point to the node that contains the largest element in the
left subtree of the current node and parentOfRightMost point to the
parent node of the rightMost node, as shown in Figure 3(a). Note that the
rightMost node cannot have a right child but may have a left child.
Replace the element value in the current node with the one in the
rightMost node, connect the parentOfRightMost node with the left child
of the rightMost node, and delete the rightMost node, as shown in Figure
3(b).
DELETING A NODE: Case 2
DELETING A NODE: Case 2
Figure 3 Case 2: the current node has a left child.
For example, consider deleting node 20 in Figure 4(a). The rightMost node
has the element value 16. Replace the element value 20 with 16 in the
current node and make node 10 the parent for node 14, as shown in
Figure 4(b).
DELETING A NODE: Case 2
Figure 4 Case 2: deleting node 20 from (a) results in (b).

More Related Content

PPTX
Binary search tree
PDF
computer notes - Stack
PPTX
Unit 2.8
PPTX
هياكلبيانات
PPTX
Pf presntation
PPTX
Unit 2.2
PPTX
Unit 3.1
Binary search tree
computer notes - Stack
Unit 2.8
هياكلبيانات
Pf presntation
Unit 2.2
Unit 3.1

What's hot (13)

PPT
Calc 2.4a
PPT
Calc 2.4a
PPTX
Unit 3.3
DOCX
Summary_Onset (1)
DOCX
Sap pi 10 nodes
PPTX
Trees (data structure)
PPTX
Array in c language
PDF
Lab12 dsa bsee20075
PPT
1.1 binary tree
DOCX
Chapter 4
PPT
Bar graph
PPTX
Binary tree and Binary search tree
PPTX
One dimensional arrays
Calc 2.4a
Calc 2.4a
Unit 3.3
Summary_Onset (1)
Sap pi 10 nodes
Trees (data structure)
Array in c language
Lab12 dsa bsee20075
1.1 binary tree
Chapter 4
Bar graph
Binary tree and Binary search tree
One dimensional arrays
Ad

Viewers also liked (20)

PDF
Vacante en Director de Ventas - Esri Colombia
PDF
環球影城六日
PDF
帛琉五日
PPT
14 recursion
PDF
20131006張家界八日
PPT
Slide Da Atividade Educacional
PPTX
Cyber security
PDF
20160304埃及10日 直客版
PPT
Power point in ped5 herwin
DOC
CVJM Update NYAMBE-F (2)
PDF
Bijoux ronaldo
PDF
Palestra Mariana
DOCX
ConnieJusticeCV-2016
PDF
Manual conciliação ok
PPTX
Linea de tiempo de literatura por monica llumiquinga
PDF
Material de trabajo informática i
PDF
Innovative Methods and Technologies in Project Management: Project Management...
PDF
Innovative Methods and Technologies in Project Management: Project Management...
PDF
Chancen 2013 - Seminare und Trainings - TUI Consulting und Services.pdf
Vacante en Director de Ventas - Esri Colombia
環球影城六日
帛琉五日
14 recursion
20131006張家界八日
Slide Da Atividade Educacional
Cyber security
20160304埃及10日 直客版
Power point in ped5 herwin
CVJM Update NYAMBE-F (2)
Bijoux ronaldo
Palestra Mariana
ConnieJusticeCV-2016
Manual conciliação ok
Linea de tiempo de literatura por monica llumiquinga
Material de trabajo informática i
Innovative Methods and Technologies in Project Management: Project Management...
Innovative Methods and Technologies in Project Management: Project Management...
Chancen 2013 - Seminare und Trainings - TUI Consulting und Services.pdf
Ad

Similar to Binary search trees (1) (20)

PPTX
Binary Search Tree
PPTX
BINARY SEARCH TREE.pptx all about trees how hey grow
PDF
Chapter 7 - Binary Search Tree in the context of DSA.pdf
PPT
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
PPTX
Lecture 7 bst
PPTX
Binary Search Tree (BST)
PPTX
8.binry search tree
PDF
Binary search tree operations
PPTX
Lecture 9 data structures and algorithms
PPT
Lecture 7-BinarySearchTrees.ppt
PPT
Binary searchtrees
PPTX
Lec 10_Binary Search Tree in data structure and algorithm.pptx
PPTX
DOC-20220815-WA0027..pptx
PPTX
Binary Search Tree
PPTX
DAA PPT.pptx
PPT
Data Structure and Algorithms Binary Search Tree
PPT
Bst(Binary Search Tree)
PPTX
Data Str Data Str Data Str Data Str Data Str
PPTX
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
PPTX
Binary Search Tree for design and analysis
Binary Search Tree
BINARY SEARCH TREE.pptx all about trees how hey grow
Chapter 7 - Binary Search Tree in the context of DSA.pdf
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
Lecture 7 bst
Binary Search Tree (BST)
8.binry search tree
Binary search tree operations
Lecture 9 data structures and algorithms
Lecture 7-BinarySearchTrees.ppt
Binary searchtrees
Lec 10_Binary Search Tree in data structure and algorithm.pptx
DOC-20220815-WA0027..pptx
Binary Search Tree
DAA PPT.pptx
Data Structure and Algorithms Binary Search Tree
Bst(Binary Search Tree)
Data Str Data Str Data Str Data Str Data Str
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Binary Search Tree for design and analysis

More from Himadri Sen Gupta (8)

DOCX
Report of industrial training
PPT
17 linkedlist (1)
PPT
Linked lists
PPTX
Report of industrial training
17 linkedlist (1)
Linked lists

Recently uploaded (20)

PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT
Mechanical Engineering MATERIALS Selection
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Sustainable Sites - Green Building Construction
DOCX
573137875-Attendance-Management-System-original
PPTX
Welding lecture in detail for understanding
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
web development for engineering and engineering
PDF
Digital Logic Computer Design lecture notes
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
OOP with Java - Java Introduction (Basics)
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
bas. eng. economics group 4 presentation 1.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Mechanical Engineering MATERIALS Selection
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Embodied AI: Ushering in the Next Era of Intelligent Systems
Internet of Things (IOT) - A guide to understanding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
CH1 Production IntroductoryConcepts.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Sustainable Sites - Green Building Construction
573137875-Attendance-Management-System-original
Welding lecture in detail for understanding
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
web development for engineering and engineering
Digital Logic Computer Design lecture notes
Foundation to blockchain - A guide to Blockchain Tech
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx

Binary search trees (1)

  • 1. BINARY SEARCH TREES Fariha Tasmin Jaigirdar Assistant Professor Daffodil International University
  • 2. BINARY SEARCH TREES A Binary Search Tree is a binary tree, which is either empty or satisfies the following properties : 1. Every node has a value and no two nodes have the same value (i.e., all the values are unique) root. 2. If there exists a left child or left sub tree then its value is less than the value of the 3. The value(s) in the right child or right sub tree is larger than the value of the root node.
  • 4. Example: Constructing BST Figure 8.12 shows a binary search tree. Notice that this tree is obtained by inserting the values 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order, starting from an empty tree. Fig 8.12
  • 7. Example: INSERTING A NODE After inserting 55
  • 8. 10/29/08 Shaily Kabir,Dept. of CSE, DU 8 Inserting a Node into a BST Rules • Examine the root R. • If the new value is less than the root’s, then it must be inserted at the left subtree. • Otherwise it must be inserted at the right subtree. • This process continues until the new value is compared with a leaf node and then it is added as that node’s (leaf node) left or right child depending on its value. Algorithm • Insert_BST(Root, Left, Right, Item) 1. Set NewNode.Value := Item. 2. Insert(Root, NewNode). End. Here, Root = Root of BST Left = Left Subtree Right = Right Subtree Item = New Item to be inserted
  • 9. 10/29/08 Shaily Kabir,Dept. of CSE, DU 9 Insert(Node, NewNode) 1. If Node = NULL then Node := NewNode and return. 2. If NewNode.Value < Node.Value then Insert(Node.Left, NewNode). 3. Else Insert(Node.Right, NewNode). Example: Figure: BST with 5 Nodes New Item: 110 FF.Value=110 (FF is the New Node address) Steps: 1. Insert(Root, FF) Insert(AA, FF) 2. Insert(AA->Right, FF) Insert(CC, FF) 3. Insert(CC->Right, FF) AA BB CC DD EE 15 13 100 12 90 110 FF
  • 10. DELETING A NODE Often you need to delete an element from a binary search tree. Doing so is far more complex than adding an element into a binary search tree. To delete an element from a binary search tree, you need to first locate the node that contains the element and also its parent node. Let current point to the node that contains the element in the binary search tree and parent point to the parent of the current node. The current node may be a left child or a right child of the parent node.
  • 11. There are two cases to consider: Case 1: The current node does not have a left child, as shown in Figure 1(a). Simply connect the parent with the right child of the current node, as shown in Figure 1(b). DELETING A NODE cont. Figure 1 Case 1: the current node has no left child.
  • 12. DELETING A NODE: Case 1 Figure 2 Case 1: deleting node 10 from (a) results in (b). For example, to delete node 10 in Figure 2(a). Connect the parent of node 10 with the right child of node 10, as shown in Figure 2(b).
  • 13. Case 2: The current node has a left child. Let rightMost point to the node that contains the largest element in the left subtree of the current node and parentOfRightMost point to the parent node of the rightMost node, as shown in Figure 3(a). Note that the rightMost node cannot have a right child but may have a left child. Replace the element value in the current node with the one in the rightMost node, connect the parentOfRightMost node with the left child of the rightMost node, and delete the rightMost node, as shown in Figure 3(b). DELETING A NODE: Case 2
  • 14. DELETING A NODE: Case 2 Figure 3 Case 2: the current node has a left child.
  • 15. For example, consider deleting node 20 in Figure 4(a). The rightMost node has the element value 16. Replace the element value 20 with 16 in the current node and make node 10 the parent for node 14, as shown in Figure 4(b). DELETING A NODE: Case 2 Figure 4 Case 2: deleting node 20 from (a) results in (b).