SlideShare a Scribd company logo
1 of 92
Download to read offline
Binary Search Tree
Benjamin Dicken
University of Arizona
Table of contents
1. Background
2. Binary Search Tree
3. Search
4. Insert
5. Delete
6. Implementation
7. Summary
1
Background
Background
Up to this point, we have seen:
• Binary search in an array data structure
• The concept of a binary tree
• Recursion
We are going to use all of these concepts when learning about binary
search trees.
2
Binary Search Tree
Binary Search Tree
A Binary Search Tree (BST) has the following properties:
3
Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
3
Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
– Each node of the tree has at most two children (a “left” child and a
“right” child)
3
Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
– Each node of the tree has at most two children (a “left” child and a
“right” child)
– Each node can legally have zero, one, or two children
3
Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
– Each node of the tree has at most two children (a “left” child and a
“right” child)
– Each node can legally have zero, one, or two children
• The Binary Search Tree Property must hold
3
Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
– Each node of the tree has at most two children (a “left” child and a
“right” child)
– Each node can legally have zero, one, or two children
• The Binary Search Tree Property must hold
– If duplicates not allowed: “The key in each node must be greater
than all keys stored in the left sub-tree, and less-than all keys in the
right sub-tree.”
3
Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
– Each node of the tree has at most two children (a “left” child and a
“right” child)
– Each node can legally have zero, one, or two children
• The Binary Search Tree Property must hold
– If duplicates not allowed: “The key in each node must be greater
than all keys stored in the left sub-tree, and less-than all keys in the
right sub-tree.”
– If duplicates allowed: “The key in each node must be greater than all
keys stored in the left sub-tree, and less-than or equal-to all keys in
the right sub-tree.”
3
Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
– Each node of the tree has at most two children (a “left” child and a
“right” child)
– Each node can legally have zero, one, or two children
• The Binary Search Tree Property must hold
– If duplicates not allowed: “The key in each node must be greater
than all keys stored in the left sub-tree, and less-than all keys in the
right sub-tree.”
– If duplicates allowed: “The key in each node must be greater than all
keys stored in the left sub-tree, and less-than or equal-to all keys in
the right sub-tree.”
– This guarantees that the tree is in sorted order.
3
Binary Search Tree
• If the Binary Search Tree Property holds, this guarantees that the
tree is in sorted order, thus making it a BST.
• An in-order traversal of the tree will visit the nodes in order from
lowest to highest.
• Let’s take a look at a few trees and apply these tests to them.
4
Binary Search Tree
Is the following a binary search tree?
. 37 .
. 21 .
11 24
. 55 .
48 62
5
Binary Search Tree
Is the following a binary search tree?
. 37 .
. 20 .
13 24
55 .
56 .
100
6
Binary Search Tree
Is the following a binary search tree?
. 37 .
. 20 .
21 24
. 55 .
18 62
7
Binary Search Tree
Is the following a binary search tree?
. 30
20 .
. 25
23
8
Binary Search Tree
Why use a BST? What are the advantages of a BST compared to some
of the data structures we have already learned about, such as...
• An array?
• A sorted array?
• A linked-list?
We will revisit this after discussing how searching, insertion, and deletion
works.
9
Search
Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
10
Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
10
Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
10
Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
• Else if K > C
10
Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
• Else if K > C
• If has a right child, continue recursively on the right child
10
Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, search key not in tree
10
Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, search key not in tree
• Else K < C
10
Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, search key not in tree
• Else K < C
• If has a left child, continue recursively on the left child
10
Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, search key not in tree
• Else K < C
• If has a left child, continue recursively on the left child
• Else, search key not in tree
10
Search
Exercise: search for 40, 91, 33
. 45 .
. 10 .
7 . 34 .
30 40
. 70 .
56 80 .
90
11
Insert
Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
12
Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
12
Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
12
Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
12
Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
• Else if K > C
12
Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
• Else if K > C
• If has a right child, continue recursively on the right child
12
Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, add right child with key K
12
Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, add right child with key K
• Else K < C
12
Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, add right child with key K
• Else K < C
• If has a left child, continue recursively on the left child
12
Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, add right child with key K
• Else K < C
• If has a left child, continue recursively on the left child
• Else, add left child with key K
12
Insert
Insertion example:
Null
13
Insert
Insert 19
Null
14
Insert
After inserting 19
19
15
Insert
Insert 50
19
16
Insert
After inserting 50
19 .
50
17
Insert
Insert 75
19 .
50
18
Insert
After inserting 75
19 .
50 .
75
19
Insert
Insert 40
19 .
50 .
75
20
Insert
After inserting 40
19 .
. 50 .
40 75
21
Insert
Insert 7
19 .
. 50 .
40 75
22
Insert
After inserting 7
. 19 .
7 . 50 .
40 75
23
Insert
Insert 12
. 19 .
7 . 50 .
40 75
24
Insert
After inserting 12
. 19 .
7 .
12
. 50 .
40 75
25
Insert
Insert 2
. 19 .
7 .
12
. 50 .
40 75
26
Insert
After inserting 2
. 19 .
. 7 .
2 12
. 50 .
40 75
27
Delete
Delete
How do we do deletion?
28
Delete
How do we do deletion?
• Traverse the tree, searching for the node with key K
28
Delete
How do we do deletion?
• Traverse the tree, searching for the node with key K
• If no node with key K exists, nothing to do!
28
Delete
How do we do deletion?
• Traverse the tree, searching for the node with key K
• If no node with key K exists, nothing to do!
• Else, execute the recursive delete operation.
28
Delete
What does the delete operation look like?
29
Delete
What does the delete operation look like?
• Call the node needing deletion C
29
Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
29
Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
29
Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
29
Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
29
Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
– In order successor: The left-most node of the right sub-tree.
29
Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
– In order successor: The left-most node of the right sub-tree.
– In order predecessor: The right-most node of the left sub-tree.
29
Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
– In order successor: The left-most node of the right sub-tree.
– In order predecessor: The right-most node of the left sub-tree.
– Copy the key from G into C
29
Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
– In order successor: The left-most node of the right sub-tree.
– In order predecessor: The right-most node of the left sub-tree.
– Copy the key from G into C
– Call the delete operation recursively on node G
29
Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
– In order successor: The left-most node of the right sub-tree.
– In order predecessor: The right-most node of the left sub-tree.
– Copy the key from G into C
– Call the delete operation recursively on node G
29
Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
– In order successor: The left-most node of the right sub-tree.
– In order predecessor: The right-most node of the left sub-tree.
– Copy the key from G into C
– Call the delete operation recursively on node G
When doing recursion in the 2-children case, eventually a node will be
reached that has either 0 or 1 children, in which case we do one of the
base-case operations described above. Let’s look at an example.
29
Delete
Exercise: delete 7
. 45 .
. 10 .
7 . 34 .
30 40
. 70 .
52 80 .
90
30
Delete
Exercise: delete 7
. 45 .
. 10 .
7 . 34 .
30 40
. 70 .
52 80 .
90
31
Delete
Exercise: delete 7
. 45 .
. 10 .
7 . 34 .
30 40
. 70 .
52 80 .
90
32
Delete
Exercise: delete 7
. 45 .
. 10 .
7 . 34 .
30 40
. 70 .
52 80 .
90
33
Delete
After deleting 7
. 45 .
10 .
. 34 .
30 40
. 70 .
52 80 .
90
34
Delete
Exercise: delete 10
. 45 .
10 .
. 34 .
30 40
. 70 .
52 80 .
90
35
Delete
Exercise: delete 10
. 45 .
10 .
. 34 .
30 40
. 70 .
52 80 .
90
36
Delete
Exercise: delete 10
. 45 .
10 .
. 34 .
30 40
. 70 .
52 80 .
90
37
Delete
After deleting 10
. 45 .
. 34 .
30 40
. 70 .
52 80 .
90
38
Delete
Exercise: delete 45
. 45 .
. 34 .
30 40
. 70 .
52 80 .
90
39
Delete
Exercise: delete 45
. 45 .
. 34 .
30 40
. 70 .
52 80 .
90
40
Delete
Exercise: delete 45
. 45 .
. 34 .
30 40
. 70 .
52 80 .
90
41
Delete
Exercise: delete 45
. 52 .
. 34 .
30 40
. 70 .
52 80 .
90
42
Delete
Exercise: delete 45
. 52 .
. 34 .
30 40
. 70 .
52 80 .
90
43
Delete
After deleting 45
. 52 .
. 34 .
30 40
. 70 .
80 .
90
44
Implementation
Implementation
45
Summary
Summary
Benefits of BSTs compared to:
• Sorted Array
• Faster insert and delete
• Similar search performance
• Linked-List
• Slower insert
• Faster delete and search
46

More Related Content

What's hot (20)

Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
Double ended queue
Double ended queueDouble ended queue
Double ended queue
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
BINARY SEARCH TREE
BINARY SEARCH TREE BINARY SEARCH TREE
BINARY SEARCH TREE
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Red black tree
Red black treeRed black tree
Red black tree
 
Red black trees
Red black treesRed black trees
Red black trees
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Binary tree
Binary tree Binary tree
Binary tree
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Shell sort
Shell sortShell sort
Shell sort
 
b+ tree
b+ treeb+ tree
b+ tree
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 

Viewers also liked

Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsAakash deep Singhal
 
شرح مبسط
شرح مبسطشرح مبسط
شرح مبسطayman
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search treeKrish_ver2
 
VB.net Database Chapter 1
VB.net Database Chapter 1VB.net Database Chapter 1
VB.net Database Chapter 1Haytham Malek
 
8 memory managment & pointers
8 memory managment & pointers8 memory managment & pointers
8 memory managment & pointersMohammad Alyan
 
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...sethuraman R
 
Orgnization structure
Orgnization structureOrgnization structure
Orgnization structureMohamed Dahi
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary treeZaid Shabbir
 
موقع سلايد شير
موقع سلايد شيرموقع سلايد شير
موقع سلايد شيرMohamed Elshazly
 

Viewers also liked (18)

Data structures 3
Data structures 3Data structures 3
Data structures 3
 
Certificado CCAA
Certificado CCAACertificado CCAA
Certificado CCAA
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
المتغيرات
المتغيراتالمتغيرات
المتغيرات
 
شرح مبسط
شرح مبسطشرح مبسط
شرح مبسط
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
VB.net Database Chapter 1
VB.net Database Chapter 1VB.net Database Chapter 1
VB.net Database Chapter 1
 
8 memory managment & pointers
8 memory managment & pointers8 memory managment & pointers
8 memory managment & pointers
 
C++ syntax summary
C++ syntax summaryC++ syntax summary
C++ syntax summary
 
Wikipedia Arabic
Wikipedia ArabicWikipedia Arabic
Wikipedia Arabic
 
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
 
C++ arabic
C++ arabicC++ arabic
C++ arabic
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Orgnization structure
Orgnization structureOrgnization structure
Orgnization structure
 
عربى 3ب ت1 جديد
عربى 3ب ت1 جديدعربى 3ب ت1 جديد
عربى 3ب ت1 جديد
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
موقع سلايد شير
موقع سلايد شيرموقع سلايد شير
موقع سلايد شير
 

Similar to BinarySearchTree-bddicken

Similar to BinarySearchTree-bddicken (20)

Binary tree
Binary treeBinary tree
Binary tree
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary search tree.pptx
Binary search tree.pptxBinary search tree.pptx
Binary search tree.pptx
 
s11_bin_search_trees.ppt
s11_bin_search_trees.ppts11_bin_search_trees.ppt
s11_bin_search_trees.ppt
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
binary_trees2
binary_trees2binary_trees2
binary_trees2
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
DAA PPT.pptx
DAA PPT.pptxDAA PPT.pptx
DAA PPT.pptx
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
6_1 (1).ppt
6_1 (1).ppt6_1 (1).ppt
6_1 (1).ppt
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
LEC 5-DS ALGO(updated).pdf
LEC 5-DS  ALGO(updated).pdfLEC 5-DS  ALGO(updated).pdf
LEC 5-DS ALGO(updated).pdf
 
Binary tree data structure
Binary tree data structureBinary tree data structure
Binary tree data structure
 
Binary Trees.ppt
Binary Trees.pptBinary Trees.ppt
Binary Trees.ppt
 
Lecture 7 bst
Lecture 7 bstLecture 7 bst
Lecture 7 bst
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 

Recently uploaded

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 

Recently uploaded (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 

BinarySearchTree-bddicken

  • 1. Binary Search Tree Benjamin Dicken University of Arizona
  • 2. Table of contents 1. Background 2. Binary Search Tree 3. Search 4. Insert 5. Delete 6. Implementation 7. Summary 1
  • 4. Background Up to this point, we have seen: • Binary search in an array data structure • The concept of a binary tree • Recursion We are going to use all of these concepts when learning about binary search trees. 2
  • 6. Binary Search Tree A Binary Search Tree (BST) has the following properties: 3
  • 7. Binary Search Tree A Binary Search Tree (BST) has the following properties: • Is a binary tree data structure 3
  • 8. Binary Search Tree A Binary Search Tree (BST) has the following properties: • Is a binary tree data structure – Each node of the tree has at most two children (a “left” child and a “right” child) 3
  • 9. Binary Search Tree A Binary Search Tree (BST) has the following properties: • Is a binary tree data structure – Each node of the tree has at most two children (a “left” child and a “right” child) – Each node can legally have zero, one, or two children 3
  • 10. Binary Search Tree A Binary Search Tree (BST) has the following properties: • Is a binary tree data structure – Each node of the tree has at most two children (a “left” child and a “right” child) – Each node can legally have zero, one, or two children • The Binary Search Tree Property must hold 3
  • 11. Binary Search Tree A Binary Search Tree (BST) has the following properties: • Is a binary tree data structure – Each node of the tree has at most two children (a “left” child and a “right” child) – Each node can legally have zero, one, or two children • The Binary Search Tree Property must hold – If duplicates not allowed: “The key in each node must be greater than all keys stored in the left sub-tree, and less-than all keys in the right sub-tree.” 3
  • 12. Binary Search Tree A Binary Search Tree (BST) has the following properties: • Is a binary tree data structure – Each node of the tree has at most two children (a “left” child and a “right” child) – Each node can legally have zero, one, or two children • The Binary Search Tree Property must hold – If duplicates not allowed: “The key in each node must be greater than all keys stored in the left sub-tree, and less-than all keys in the right sub-tree.” – If duplicates allowed: “The key in each node must be greater than all keys stored in the left sub-tree, and less-than or equal-to all keys in the right sub-tree.” 3
  • 13. Binary Search Tree A Binary Search Tree (BST) has the following properties: • Is a binary tree data structure – Each node of the tree has at most two children (a “left” child and a “right” child) – Each node can legally have zero, one, or two children • The Binary Search Tree Property must hold – If duplicates not allowed: “The key in each node must be greater than all keys stored in the left sub-tree, and less-than all keys in the right sub-tree.” – If duplicates allowed: “The key in each node must be greater than all keys stored in the left sub-tree, and less-than or equal-to all keys in the right sub-tree.” – This guarantees that the tree is in sorted order. 3
  • 14. Binary Search Tree • If the Binary Search Tree Property holds, this guarantees that the tree is in sorted order, thus making it a BST. • An in-order traversal of the tree will visit the nodes in order from lowest to highest. • Let’s take a look at a few trees and apply these tests to them. 4
  • 15. Binary Search Tree Is the following a binary search tree? . 37 . . 21 . 11 24 . 55 . 48 62 5
  • 16. Binary Search Tree Is the following a binary search tree? . 37 . . 20 . 13 24 55 . 56 . 100 6
  • 17. Binary Search Tree Is the following a binary search tree? . 37 . . 20 . 21 24 . 55 . 18 62 7
  • 18. Binary Search Tree Is the following a binary search tree? . 30 20 . . 25 23 8
  • 19. Binary Search Tree Why use a BST? What are the advantages of a BST compared to some of the data structures we have already learned about, such as... • An array? • A sorted array? • A linked-list? We will revisit this after discussing how searching, insertion, and deletion works. 9
  • 21. Search How do we search for a key K in a binary search tree? Beginning at the root of the tree, do the following operations: 10
  • 22. Search How do we search for a key K in a binary search tree? Beginning at the root of the tree, do the following operations: • Call the key of the current node C 10
  • 23. Search How do we search for a key K in a binary search tree? Beginning at the root of the tree, do the following operations: • Call the key of the current node C • If K == C, our search is complete! 10
  • 24. Search How do we search for a key K in a binary search tree? Beginning at the root of the tree, do the following operations: • Call the key of the current node C • If K == C, our search is complete! • Else if K > C 10
  • 25. Search How do we search for a key K in a binary search tree? Beginning at the root of the tree, do the following operations: • Call the key of the current node C • If K == C, our search is complete! • Else if K > C • If has a right child, continue recursively on the right child 10
  • 26. Search How do we search for a key K in a binary search tree? Beginning at the root of the tree, do the following operations: • Call the key of the current node C • If K == C, our search is complete! • Else if K > C • If has a right child, continue recursively on the right child • Else, search key not in tree 10
  • 27. Search How do we search for a key K in a binary search tree? Beginning at the root of the tree, do the following operations: • Call the key of the current node C • If K == C, our search is complete! • Else if K > C • If has a right child, continue recursively on the right child • Else, search key not in tree • Else K < C 10
  • 28. Search How do we search for a key K in a binary search tree? Beginning at the root of the tree, do the following operations: • Call the key of the current node C • If K == C, our search is complete! • Else if K > C • If has a right child, continue recursively on the right child • Else, search key not in tree • Else K < C • If has a left child, continue recursively on the left child 10
  • 29. Search How do we search for a key K in a binary search tree? Beginning at the root of the tree, do the following operations: • Call the key of the current node C • If K == C, our search is complete! • Else if K > C • If has a right child, continue recursively on the right child • Else, search key not in tree • Else K < C • If has a left child, continue recursively on the left child • Else, search key not in tree 10
  • 30. Search Exercise: search for 40, 91, 33 . 45 . . 10 . 7 . 34 . 30 40 . 70 . 56 80 . 90 11
  • 32. Insert How do we insert a key K in a binary search tree? Starting from the root node: 12
  • 33. Insert How do we insert a key K in a binary search tree? Starting from the root node: • Call the key of the current node C 12
  • 34. Insert How do we insert a key K in a binary search tree? Starting from the root node: • Call the key of the current node C • If the root node is null, insert a new node with key K as the root 12
  • 35. Insert How do we insert a key K in a binary search tree? Starting from the root node: • Call the key of the current node C • If the root node is null, insert a new node with key K as the root • Else if K == C, key already exists in tree, no need to insert (assuming no duplicates) 12
  • 36. Insert How do we insert a key K in a binary search tree? Starting from the root node: • Call the key of the current node C • If the root node is null, insert a new node with key K as the root • Else if K == C, key already exists in tree, no need to insert (assuming no duplicates) • Else if K > C 12
  • 37. Insert How do we insert a key K in a binary search tree? Starting from the root node: • Call the key of the current node C • If the root node is null, insert a new node with key K as the root • Else if K == C, key already exists in tree, no need to insert (assuming no duplicates) • Else if K > C • If has a right child, continue recursively on the right child 12
  • 38. Insert How do we insert a key K in a binary search tree? Starting from the root node: • Call the key of the current node C • If the root node is null, insert a new node with key K as the root • Else if K == C, key already exists in tree, no need to insert (assuming no duplicates) • Else if K > C • If has a right child, continue recursively on the right child • Else, add right child with key K 12
  • 39. Insert How do we insert a key K in a binary search tree? Starting from the root node: • Call the key of the current node C • If the root node is null, insert a new node with key K as the root • Else if K == C, key already exists in tree, no need to insert (assuming no duplicates) • Else if K > C • If has a right child, continue recursively on the right child • Else, add right child with key K • Else K < C 12
  • 40. Insert How do we insert a key K in a binary search tree? Starting from the root node: • Call the key of the current node C • If the root node is null, insert a new node with key K as the root • Else if K == C, key already exists in tree, no need to insert (assuming no duplicates) • Else if K > C • If has a right child, continue recursively on the right child • Else, add right child with key K • Else K < C • If has a left child, continue recursively on the left child 12
  • 41. Insert How do we insert a key K in a binary search tree? Starting from the root node: • Call the key of the current node C • If the root node is null, insert a new node with key K as the root • Else if K == C, key already exists in tree, no need to insert (assuming no duplicates) • Else if K > C • If has a right child, continue recursively on the right child • Else, add right child with key K • Else K < C • If has a left child, continue recursively on the left child • Else, add left child with key K 12
  • 50. Insert After inserting 40 19 . . 50 . 40 75 21
  • 51. Insert Insert 7 19 . . 50 . 40 75 22
  • 52. Insert After inserting 7 . 19 . 7 . 50 . 40 75 23
  • 53. Insert Insert 12 . 19 . 7 . 50 . 40 75 24
  • 54. Insert After inserting 12 . 19 . 7 . 12 . 50 . 40 75 25
  • 55. Insert Insert 2 . 19 . 7 . 12 . 50 . 40 75 26
  • 56. Insert After inserting 2 . 19 . . 7 . 2 12 . 50 . 40 75 27
  • 58. Delete How do we do deletion? 28
  • 59. Delete How do we do deletion? • Traverse the tree, searching for the node with key K 28
  • 60. Delete How do we do deletion? • Traverse the tree, searching for the node with key K • If no node with key K exists, nothing to do! 28
  • 61. Delete How do we do deletion? • Traverse the tree, searching for the node with key K • If no node with key K exists, nothing to do! • Else, execute the recursive delete operation. 28
  • 62. Delete What does the delete operation look like? 29
  • 63. Delete What does the delete operation look like? • Call the node needing deletion C 29
  • 64. Delete What does the delete operation look like? • Call the node needing deletion C • If C has 0 children: just drop the reference to it, and we’re done 29
  • 65. Delete What does the delete operation look like? • Call the node needing deletion C • If C has 0 children: just drop the reference to it, and we’re done • Else if C has 1 child: replace the parent of C’s reference to C with a reference to the single child, and we’re done 29
  • 66. Delete What does the delete operation look like? • Call the node needing deletion C • If C has 0 children: just drop the reference to it, and we’re done • Else if C has 1 child: replace the parent of C’s reference to C with a reference to the single child, and we’re done • Else C has 2 children: 29
  • 67. Delete What does the delete operation look like? • Call the node needing deletion C • If C has 0 children: just drop the reference to it, and we’re done • Else if C has 1 child: replace the parent of C’s reference to C with a reference to the single child, and we’re done • Else C has 2 children: – Find the in-order successor (or predecessor) of C. Call this node G. 29
  • 68. Delete What does the delete operation look like? • Call the node needing deletion C • If C has 0 children: just drop the reference to it, and we’re done • Else if C has 1 child: replace the parent of C’s reference to C with a reference to the single child, and we’re done • Else C has 2 children: – Find the in-order successor (or predecessor) of C. Call this node G. – In order successor: The left-most node of the right sub-tree. 29
  • 69. Delete What does the delete operation look like? • Call the node needing deletion C • If C has 0 children: just drop the reference to it, and we’re done • Else if C has 1 child: replace the parent of C’s reference to C with a reference to the single child, and we’re done • Else C has 2 children: – Find the in-order successor (or predecessor) of C. Call this node G. – In order successor: The left-most node of the right sub-tree. – In order predecessor: The right-most node of the left sub-tree. 29
  • 70. Delete What does the delete operation look like? • Call the node needing deletion C • If C has 0 children: just drop the reference to it, and we’re done • Else if C has 1 child: replace the parent of C’s reference to C with a reference to the single child, and we’re done • Else C has 2 children: – Find the in-order successor (or predecessor) of C. Call this node G. – In order successor: The left-most node of the right sub-tree. – In order predecessor: The right-most node of the left sub-tree. – Copy the key from G into C 29
  • 71. Delete What does the delete operation look like? • Call the node needing deletion C • If C has 0 children: just drop the reference to it, and we’re done • Else if C has 1 child: replace the parent of C’s reference to C with a reference to the single child, and we’re done • Else C has 2 children: – Find the in-order successor (or predecessor) of C. Call this node G. – In order successor: The left-most node of the right sub-tree. – In order predecessor: The right-most node of the left sub-tree. – Copy the key from G into C – Call the delete operation recursively on node G 29
  • 72. Delete What does the delete operation look like? • Call the node needing deletion C • If C has 0 children: just drop the reference to it, and we’re done • Else if C has 1 child: replace the parent of C’s reference to C with a reference to the single child, and we’re done • Else C has 2 children: – Find the in-order successor (or predecessor) of C. Call this node G. – In order successor: The left-most node of the right sub-tree. – In order predecessor: The right-most node of the left sub-tree. – Copy the key from G into C – Call the delete operation recursively on node G 29
  • 73. Delete What does the delete operation look like? • Call the node needing deletion C • If C has 0 children: just drop the reference to it, and we’re done • Else if C has 1 child: replace the parent of C’s reference to C with a reference to the single child, and we’re done • Else C has 2 children: – Find the in-order successor (or predecessor) of C. Call this node G. – In order successor: The left-most node of the right sub-tree. – In order predecessor: The right-most node of the left sub-tree. – Copy the key from G into C – Call the delete operation recursively on node G When doing recursion in the 2-children case, eventually a node will be reached that has either 0 or 1 children, in which case we do one of the base-case operations described above. Let’s look at an example. 29
  • 74. Delete Exercise: delete 7 . 45 . . 10 . 7 . 34 . 30 40 . 70 . 52 80 . 90 30
  • 75. Delete Exercise: delete 7 . 45 . . 10 . 7 . 34 . 30 40 . 70 . 52 80 . 90 31
  • 76. Delete Exercise: delete 7 . 45 . . 10 . 7 . 34 . 30 40 . 70 . 52 80 . 90 32
  • 77. Delete Exercise: delete 7 . 45 . . 10 . 7 . 34 . 30 40 . 70 . 52 80 . 90 33
  • 78. Delete After deleting 7 . 45 . 10 . . 34 . 30 40 . 70 . 52 80 . 90 34
  • 79. Delete Exercise: delete 10 . 45 . 10 . . 34 . 30 40 . 70 . 52 80 . 90 35
  • 80. Delete Exercise: delete 10 . 45 . 10 . . 34 . 30 40 . 70 . 52 80 . 90 36
  • 81. Delete Exercise: delete 10 . 45 . 10 . . 34 . 30 40 . 70 . 52 80 . 90 37
  • 82. Delete After deleting 10 . 45 . . 34 . 30 40 . 70 . 52 80 . 90 38
  • 83. Delete Exercise: delete 45 . 45 . . 34 . 30 40 . 70 . 52 80 . 90 39
  • 84. Delete Exercise: delete 45 . 45 . . 34 . 30 40 . 70 . 52 80 . 90 40
  • 85. Delete Exercise: delete 45 . 45 . . 34 . 30 40 . 70 . 52 80 . 90 41
  • 86. Delete Exercise: delete 45 . 52 . . 34 . 30 40 . 70 . 52 80 . 90 42
  • 87. Delete Exercise: delete 45 . 52 . . 34 . 30 40 . 70 . 52 80 . 90 43
  • 88. Delete After deleting 45 . 52 . . 34 . 30 40 . 70 . 80 . 90 44
  • 92. Summary Benefits of BSTs compared to: • Sorted Array • Faster insert and delete • Similar search performance • Linked-List • Slower insert • Faster delete and search 46