SlideShare a Scribd company logo
1 of 15
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

What's hot

What's hot (13)

Calc 2.4a
Calc 2.4aCalc 2.4a
Calc 2.4a
 
Calc 2.4a
Calc 2.4aCalc 2.4a
Calc 2.4a
 
Unit 3.3
Unit 3.3Unit 3.3
Unit 3.3
 
Summary_Onset (1)
Summary_Onset (1)Summary_Onset (1)
Summary_Onset (1)
 
Sap pi 10 nodes
Sap pi 10 nodesSap pi 10 nodes
Sap pi 10 nodes
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Lab12 dsa bsee20075
Lab12 dsa bsee20075Lab12 dsa bsee20075
Lab12 dsa bsee20075
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Bar graph
Bar graphBar graph
Bar graph
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search tree
 
One dimensional arrays
One dimensional arraysOne dimensional arrays
One dimensional arrays
 

Viewers also liked

Vacante en Director de Ventas - Esri Colombia
Vacante en Director de Ventas - Esri ColombiaVacante en Director de Ventas - Esri Colombia
Vacante en Director de Ventas - Esri ColombiaPaola A
 
Slide Da Atividade Educacional
Slide Da Atividade EducacionalSlide Da Atividade Educacional
Slide Da Atividade Educacionalanalima198422
 
20160304埃及10日 直客版
20160304埃及10日 直客版20160304埃及10日 直客版
20160304埃及10日 直客版府城旅行社
 
Power point in ped5 herwin
Power point in ped5 herwinPower point in ped5 herwin
Power point in ped5 herwinHerwin Clemente
 
Manual conciliação ok
Manual conciliação  okManual conciliação  ok
Manual conciliação okBruna Santos
 
Material de trabajo informática i
Material de trabajo informática iMaterial de trabajo informática i
Material de trabajo informática isliderfiler
 
Innovative Methods and Technologies in Project Management: Project Management...
Innovative Methods and Technologies in Project Management: Project Management...Innovative Methods and Technologies in Project Management: Project Management...
Innovative Methods and Technologies in Project Management: Project Management...Association for Project Management
 
Innovative Methods and Technologies in Project Management: Project Management...
Innovative Methods and Technologies in Project Management: Project Management...Innovative Methods and Technologies in Project Management: Project Management...
Innovative Methods and Technologies in Project Management: Project Management...Association for Project Management
 
Chancen 2013 - Seminare und Trainings - TUI Consulting und Services.pdf
Chancen 2013 - Seminare und Trainings - TUI Consulting und Services.pdfChancen 2013 - Seminare und Trainings - TUI Consulting und Services.pdf
Chancen 2013 - Seminare und Trainings - TUI Consulting und Services.pdfunn | UNITED NEWS NETWORK GmbH
 

Viewers also liked (20)

Vacante en Director de Ventas - Esri Colombia
Vacante en Director de Ventas - Esri ColombiaVacante en Director de Ventas - Esri Colombia
Vacante en Director de Ventas - Esri Colombia
 
--**
--**--**
--**
 
環球影城六日
環球影城六日環球影城六日
環球影城六日
 
帛琉五日
帛琉五日帛琉五日
帛琉五日
 
14 recursion
14 recursion14 recursion
14 recursion
 
20131006張家界八日
20131006張家界八日20131006張家界八日
20131006張家界八日
 
Slide Da Atividade Educacional
Slide Da Atividade EducacionalSlide Da Atividade Educacional
Slide Da Atividade Educacional
 
Cyber security
Cyber securityCyber security
Cyber security
 
20160304埃及10日 直客版
20160304埃及10日 直客版20160304埃及10日 直客版
20160304埃及10日 直客版
 
Power point in ped5 herwin
Power point in ped5 herwinPower point in ped5 herwin
Power point in ped5 herwin
 
CVJM Update NYAMBE-F (2)
CVJM Update NYAMBE-F (2)CVJM Update NYAMBE-F (2)
CVJM Update NYAMBE-F (2)
 
Bijoux ronaldo
Bijoux   ronaldoBijoux   ronaldo
Bijoux ronaldo
 
Palestra Mariana
Palestra MarianaPalestra Mariana
Palestra Mariana
 
ConnieJusticeCV-2016
ConnieJusticeCV-2016ConnieJusticeCV-2016
ConnieJusticeCV-2016
 
Manual conciliação ok
Manual conciliação  okManual conciliação  ok
Manual conciliação ok
 
Linea de tiempo de literatura por monica llumiquinga
Linea de tiempo de literatura por monica llumiquingaLinea de tiempo de literatura por monica llumiquinga
Linea de tiempo de literatura por monica llumiquinga
 
Material de trabajo informática i
Material de trabajo informática iMaterial de trabajo informática i
Material de trabajo informática i
 
Innovative Methods and Technologies in Project Management: Project Management...
Innovative Methods and Technologies in Project Management: Project Management...Innovative Methods and Technologies in Project Management: Project Management...
Innovative Methods and Technologies in Project Management: Project Management...
 
Innovative Methods and Technologies in Project Management: Project Management...
Innovative Methods and Technologies in Project Management: Project Management...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
Chancen 2013 - Seminare und Trainings - TUI Consulting und Services.pdfChancen 2013 - Seminare und Trainings - TUI Consulting und Services.pdf
Chancen 2013 - Seminare und Trainings - TUI Consulting und Services.pdf
 

Similar to Binary search trees (1)

Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptDrBashirMSaad
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data StructureMeghaj Mallick
 
Unit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.pptUnit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.pptplagcheck
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfhadpadrrajeshh
 
BSTNode.Java Node class used for implementing the BST. .pdf
BSTNode.Java   Node class used for implementing the BST. .pdfBSTNode.Java   Node class used for implementing the BST. .pdf
BSTNode.Java Node class used for implementing the BST. .pdfinfo189835
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVLKatang Isip
 
Lo27
Lo27Lo27
Lo27lksoo
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data StructureOm Prakash
 
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxAssg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxfestockton
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Treekhabbab_h
 

Similar to Binary search trees (1) (20)

Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
 
Binary searchtrees
Binary searchtreesBinary searchtrees
Binary searchtrees
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
L 19 ct1120
L 19 ct1120L 19 ct1120
L 19 ct1120
 
Unit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.pptUnit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.ppt
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdf
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
binary_trees2
binary_trees2binary_trees2
binary_trees2
 
BSTNode.Java Node class used for implementing the BST. .pdf
BSTNode.Java   Node class used for implementing the BST. .pdfBSTNode.Java   Node class used for implementing the BST. .pdf
BSTNode.Java Node class used for implementing the BST. .pdf
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Data Structures
Data StructuresData Structures
Data Structures
 
05
0505
05
 
Lo27
Lo27Lo27
Lo27
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxAssg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Fp growth
Fp growthFp growth
Fp growth
 
E2
E2E2
E2
 

More from Himadri Sen Gupta (8)

Report of industrial training
Report of industrial trainingReport of industrial training
Report of industrial training
 
Graphs
GraphsGraphs
Graphs
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Tree
TreeTree
Tree
 
Heap
HeapHeap
Heap
 
Linked lists
Linked listsLinked lists
Linked lists
 
Queue
QueueQueue
Queue
 
1311004
13110041311004
1311004
 

Recently uploaded

High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...Call girls in Ahmedabad High profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Recently uploaded (20)

High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 

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).