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

PPT
1.5 binary search tree
PPTX
Binary Tree in Data Structure
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
PPTX
Binary Search Tree
PPT
BINARY SEARCH TREE
PDF
AVL tree ( Balanced Binary Search Tree)-Data Structure
PPTX
Threaded Binary Tree.pptx
PPT
Binary search tree(bst)
1.5 binary search tree
Binary Tree in Data Structure
Trees, Binary Search Tree, AVL Tree in Data Structures
Binary Search Tree
BINARY SEARCH TREE
AVL tree ( Balanced Binary Search Tree)-Data Structure
Threaded Binary Tree.pptx
Binary search tree(bst)

What's hot (20)

PPTX
Binary Search Tree
PDF
Expression trees
PPTX
B+ tree intro,uses,insertion and deletion
PPTX
Selection sorting
PPTX
Doubly Linked List
PPT
Binary search trees
PPT
BINARY TREE REPRESENTATION.ppt
PDF
Red Black Trees
PPT
Binary search trees
PPTX
Binary trees1
PPTX
AVL Tree Data Structure
PDF
Binary Search Tree
PPTX
Data structures trees - B Tree & B+Tree.pptx
PPT
Red black tree
PPT
Lec 17 heap data structure
PPSX
Data Structure (Tree)
PPT
Branch & bound
PPT
Data Structure and Algorithms AVL Trees
DOCX
Shortest Path Problem.docx
Binary Search Tree
Expression trees
B+ tree intro,uses,insertion and deletion
Selection sorting
Doubly Linked List
Binary search trees
BINARY TREE REPRESENTATION.ppt
Red Black Trees
Binary search trees
Binary trees1
AVL Tree Data Structure
Binary Search Tree
Data structures trees - B Tree & B+Tree.pptx
Red black tree
Lec 17 heap data structure
Data Structure (Tree)
Branch & bound
Data Structure and Algorithms AVL Trees
Shortest Path Problem.docx
Ad

Viewers also liked (16)

PPTX
Data structures 3
PDF
Certificado CCAA
PPTX
Lecture 9 data structures and algorithms
PDF
المتغيرات
PPTX
شرح مبسط
PDF
VB.net Database Chapter 1
PPTX
8 memory managment & pointers
PDF
C++ syntax summary
PPT
Wikipedia Arabic
PPTX
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
PDF
C++ arabic
PPTX
Tree - Data Structure
PDF
Orgnization structure
PDF
عربى 3ب ت1 جديد
PDF
Tree and binary tree
DOC
موقع سلايد شير
Data structures 3
Certificado CCAA
Lecture 9 data structures and algorithms
المتغيرات
شرح مبسط
VB.net Database Chapter 1
8 memory managment & pointers
C++ syntax summary
Wikipedia Arabic
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
C++ arabic
Tree - Data Structure
Orgnization structure
عربى 3ب ت1 جديد
Tree and binary tree
موقع سلايد شير
Ad

Similar to BinarySearchTree-bddicken (20)

PPT
Binary Search Tree
PPTX
Binary Search Tree in Data Structure
PDF
binarysearchtreeindatastructures-200604055006 (1).pdf
PPTX
Binary Search Tree
PPTX
Binary search tree definition operation.pptx
PPTX
Binary Search Tree In Python.pptx
PPTX
Data Str Data Str Data Str Data Str Data Str
PPT
bst.ppt
PPTX
8.binry search tree
PPTX
Binary search tree
PDF
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
PPT
Binary search tree in data structures
PPTX
Binary tree
PDF
Chapter 7 - Binary Search Tree in the context of DSA.pdf
PPTX
tree-160731205832.pptx
PDF
binary_trees2
PPT
mitochondria moment and super computer integration.ppt
PPT
data structure on bca.
PPTX
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
PPT
Lecture 7-BinarySearchTrees.ppt
Binary Search Tree
Binary Search Tree in Data Structure
binarysearchtreeindatastructures-200604055006 (1).pdf
Binary Search Tree
Binary search tree definition operation.pptx
Binary Search Tree In Python.pptx
Data Str Data Str Data Str Data Str Data Str
bst.ppt
8.binry search tree
Binary search tree
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Binary search tree in data structures
Binary tree
Chapter 7 - Binary Search Tree in the context of DSA.pdf
tree-160731205832.pptx
binary_trees2
mitochondria moment and super computer integration.ppt
data structure on bca.
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Lecture 7-BinarySearchTrees.ppt

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Empathic Computing: Creating Shared Understanding
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
cuic standard and advanced reporting.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
KodekX | Application Modernization Development
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Review of recent advances in non-invasive hemoglobin estimation
Dropbox Q2 2025 Financial Results & Investor Presentation
Empathic Computing: Creating Shared Understanding
Network Security Unit 5.pdf for BCA BBA.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Reach Out and Touch Someone: Haptics and Empathic Computing
cuic standard and advanced reporting.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectroscopy.pptx food analysis technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Big Data Technologies - Introduction.pptx
KodekX | Application Modernization Development
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

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