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

CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
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
 
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
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
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
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
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
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
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
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 

Recently uploaded (20)

CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
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
 
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...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
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)
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
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
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
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
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 

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