SlideShare a Scribd company logo
Instructor: Lilian de Greef
Quarter: Summer 2017
CSE 373: Data Structures and Algorithms
Lecture 9: Binary Search Trees
Today
• Announcements
• Binary Trees
• Height
• Traversals
• Binary Search Trees
• Definition
• find
• insert
• delete
• buildTree
Announcements
• Change to office hours for just this week
• Tuesday’s “office” office hours / private office hours
• 12:00pm – 12:30pm
• (not at 1:30pm!)
• Dorothy and I trading 2:00pm - 3:00pm office hours this week
• Same time and location
• Homework 1 Statistics
• Mean: 39.7/50 (+1 extra credit)
• Median: 42.5/50 (+0 extra credit)
• Max: 49/50 (+1) or 47/50 (+4)
• Standard Deviation: 10.18
Reminder: Tree terminology
A
E
B
D F
C
G
I
H
L
J M
K N
Node / Vertex
Edges
Root
Leaves
Left subtree
Right subtree
Binary Trees
• Binary tree: Each node has at most 2 children (branching factor 2)
• Binary tree is
• A root (with data)
• A left subtree (may be empty)
• A right subtree (may be empty)
• Special Cases:
(Last week’s practice) What does the following method do?
int height(Node root){
if (root == null),
return -1;
return 1 + max(height(root.left),
height(root.right);
}
A. It calculates the number of nodes in the tree.
B. It calculates the depth of the nodes.
C. It calculates the height of the tree.
D. It calculates the number of leaves in the tree.
Binary Trees: Some Numbers
Recall: height of a tree = longest path from root to leaf (count edges)
For binary tree of height h:
• max # of leaves:
• max # of nodes:
• min # of leaves:
• min # of nodes:
For n nodes, the min height (best-case) is
the max height (worst-case) is
Tree Traversals
A traversal is an order for visiting all the nodes of a tree
• Pre-order: root, left subtree, right subtree
• In-order: left subtree, root, right subtree
• Post-order: left subtree, right subtree, root
A
B
D E
C
F
G
Tree Traversals: Practice
Which one makes sense for evaluating this expression tree?
• Pre-order: root, left subtree, right subtree
• In-order: left subtree, root, right subtree
• Post-order: left subtree, right subtree, root
+
*
2 4
5
4
12
10
6
2
11
5
8
14
13
7 9
• Structure property (binary tree)
• Each node has  2 children
• Result: keeps operations simple
• Order property
• Result: straight-forward to find any given value
A binary search tree is a type of binary tree
(but not all binary trees are binary search trees!)
Binary Search Tree (BST) Data Structure
3
11
7
1
8
4
5
4
18
10
6
2
11
5
8
20
21
7
15
Practice: are these BSTs?
How do we find(value) in BST’s?
20
9
2
15
5
12
30
7 17
10
find in BST: Recursive Version
What is the running time?
20
9
2
15
5
12
30
7 17
10
Data find(Data value, Node root){
if(root == null)
return null;
if(key < root.value)
return find(value, root.left);
if(key > root.value)
return find(value, root.right);
return root.value;
}
find in BST: Iterative Version
20
9
2
15
5
12
30
7 17
10
Data find(Object value, Node root){
while(root != null
&& root.value != value) {
if (value < root.value)
root = root.left;
else (value > root.value)
root = root.right;
}
if(root == null)
return null;
return root.value;
}
Other BST “Finding” Operations
findMin: Find minimum node
findMax: Find maximum node 20
9
2
15
5
12
30
7 17
10
insert in BST
insert(13)
insert(8)
insert(31)
Worst-case running time:
20
9
2
15
5
12
30
7 17
10
Practice with insert, primer for delete
Start with an empty tree. Insert the following values, in the given order:
14, 2, 5, 20, 42, 1, 4, 16
Then, changing as few nodes as possible, delete the following in order:
42, 14
What would the root of the resulting tree be?
A. 2
B. 4
C. 5
D. 16
(Extra space for scratch work / notes)
delete in BST
• Why might delete be harder than insert?
• Basic idea:
• Three potential cases to fix:
delete case: Leaf
delete(17)
20
9
2
15
5
12
30
7 17
10
delete case: One Child
delete(15)
20
9
2
15
5
12
30
7 10
delete case: Two Children
delete(5)
20
9
2
5
12
30
7 10
What can we
replace 5 with?
delete case: Two Children
What can we replace the node with?
Options:
delete case: Two Children (example #2)
delete(23)
30
9
2
23
5
12
7 10
18
19
15 32
25
Changing as few nodes as possible, delete the following in order:
42, 14
Practice with insert, primer for delete
42
5
1
20
2
14
4
16
delete through Lazy Deletion
• Lazy deletion can work well for a BST
• Simpler
• Can do “real deletions” later as a batch
• Some inserts can just “undelete” a tree node
• But
• Can waste space and slow down find operations
• Make some operations more complicated:
• e.g., findMin and findMax?
buildTree for BST
Let’s consider buildTree (insert values starting from an empty tree)
Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST
• If inserted in given order,
what is the tree?
• What big-O runtime for
buildTree on this sorted input?
• Is inserting in the reverse order any better?
buildTree for BST
Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST
What we if could somehow re-arrange them
• median first, then left median, right median, etc.
5, 3, 7, 2, 1, 4, 8, 6, 9
• What tree does that give us?
• What big-O runtime?

More Related Content

Similar to Lecture 09 - Binary Search Trees.pptx mission Sk it

210 trees5
210 trees5210 trees5
210 trees5
Tanweer Page
 
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdf
SanghdipUdrake
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
Database Optimization Basic 1 : Index
Database Optimization Basic 1 : IndexDatabase Optimization Basic 1 : Index
Database Optimization Basic 1 : Index
Ardi Imawan
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptx
RaaviKapoor
 
Trees second part in data structures with examples
Trees second part in data structures with examplesTrees second part in data structures with examples
Trees second part in data structures with examples
rupanaveen24
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
Rai University
 
Lecture2-DT.pptx
Lecture2-DT.pptxLecture2-DT.pptx
Lecture2-DT.pptx
INyomanSwitrayana
 
Search tree & graph
Search tree & graphSearch tree & graph
Search tree & graph
Michael Jo
 
Binary tree
Binary treeBinary tree
Binary tree
Afaq Mansoor Khan
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
Rai University
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
Rai University
 
BTrees-fall2010.ppt
BTrees-fall2010.pptBTrees-fall2010.ppt
BTrees-fall2010.ppt
zalanmvb
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
AdityaK92
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
MouDhara1
 
Tree
TreeTree
How to choose a database
How to choose a databaseHow to choose a database
How to choose a database
Vsevolod Solovyov
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
Zia Ush Shamszaman
 
L 19 ct1120
L 19 ct1120L 19 ct1120
L 19 ct1120
Zia Ush Shamszaman
 

Similar to Lecture 09 - Binary Search Trees.pptx mission Sk it (20)

210 trees5
210 trees5210 trees5
210 trees5
 
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdf
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Database Optimization Basic 1 : Index
Database Optimization Basic 1 : IndexDatabase Optimization Basic 1 : Index
Database Optimization Basic 1 : Index
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptx
 
Trees second part in data structures with examples
Trees second part in data structures with examplesTrees second part in data structures with examples
Trees second part in data structures with examples
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
 
Lecture2-DT.pptx
Lecture2-DT.pptxLecture2-DT.pptx
Lecture2-DT.pptx
 
Search tree & graph
Search tree & graphSearch tree & graph
Search tree & graph
 
Binary tree
Binary treeBinary tree
Binary tree
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
 
BTrees-fall2010.ppt
BTrees-fall2010.pptBTrees-fall2010.ppt
BTrees-fall2010.ppt
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Tree
TreeTree
Tree
 
How to choose a database
How to choose a databaseHow to choose a database
How to choose a database
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
 
L 19 ct1120
L 19 ct1120L 19 ct1120
L 19 ct1120
 

Recently uploaded

Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
benishzehra469
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
pchutichetpong
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
NABLAS株式会社
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
AbhimanyuSinha9
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
mbawufebxi
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
jerlynmaetalle
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 

Recently uploaded (20)

Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 

Lecture 09 - Binary Search Trees.pptx mission Sk it

  • 1. Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 9: Binary Search Trees
  • 2. Today • Announcements • Binary Trees • Height • Traversals • Binary Search Trees • Definition • find • insert • delete • buildTree
  • 3. Announcements • Change to office hours for just this week • Tuesday’s “office” office hours / private office hours • 12:00pm – 12:30pm • (not at 1:30pm!) • Dorothy and I trading 2:00pm - 3:00pm office hours this week • Same time and location • Homework 1 Statistics • Mean: 39.7/50 (+1 extra credit) • Median: 42.5/50 (+0 extra credit) • Max: 49/50 (+1) or 47/50 (+4) • Standard Deviation: 10.18
  • 4. Reminder: Tree terminology A E B D F C G I H L J M K N Node / Vertex Edges Root Leaves Left subtree Right subtree
  • 5. Binary Trees • Binary tree: Each node has at most 2 children (branching factor 2) • Binary tree is • A root (with data) • A left subtree (may be empty) • A right subtree (may be empty) • Special Cases:
  • 6. (Last week’s practice) What does the following method do? int height(Node root){ if (root == null), return -1; return 1 + max(height(root.left), height(root.right); } A. It calculates the number of nodes in the tree. B. It calculates the depth of the nodes. C. It calculates the height of the tree. D. It calculates the number of leaves in the tree.
  • 7. Binary Trees: Some Numbers Recall: height of a tree = longest path from root to leaf (count edges) For binary tree of height h: • max # of leaves: • max # of nodes: • min # of leaves: • min # of nodes: For n nodes, the min height (best-case) is the max height (worst-case) is
  • 8. Tree Traversals A traversal is an order for visiting all the nodes of a tree • Pre-order: root, left subtree, right subtree • In-order: left subtree, root, right subtree • Post-order: left subtree, right subtree, root A B D E C F G
  • 9. Tree Traversals: Practice Which one makes sense for evaluating this expression tree? • Pre-order: root, left subtree, right subtree • In-order: left subtree, root, right subtree • Post-order: left subtree, right subtree, root + * 2 4 5
  • 10. 4 12 10 6 2 11 5 8 14 13 7 9 • Structure property (binary tree) • Each node has  2 children • Result: keeps operations simple • Order property • Result: straight-forward to find any given value A binary search tree is a type of binary tree (but not all binary trees are binary search trees!) Binary Search Tree (BST) Data Structure
  • 12. How do we find(value) in BST’s? 20 9 2 15 5 12 30 7 17 10
  • 13. find in BST: Recursive Version What is the running time? 20 9 2 15 5 12 30 7 17 10 Data find(Data value, Node root){ if(root == null) return null; if(key < root.value) return find(value, root.left); if(key > root.value) return find(value, root.right); return root.value; }
  • 14. find in BST: Iterative Version 20 9 2 15 5 12 30 7 17 10 Data find(Object value, Node root){ while(root != null && root.value != value) { if (value < root.value) root = root.left; else (value > root.value) root = root.right; } if(root == null) return null; return root.value; }
  • 15. Other BST “Finding” Operations findMin: Find minimum node findMax: Find maximum node 20 9 2 15 5 12 30 7 17 10
  • 16. insert in BST insert(13) insert(8) insert(31) Worst-case running time: 20 9 2 15 5 12 30 7 17 10
  • 17. Practice with insert, primer for delete Start with an empty tree. Insert the following values, in the given order: 14, 2, 5, 20, 42, 1, 4, 16 Then, changing as few nodes as possible, delete the following in order: 42, 14 What would the root of the resulting tree be? A. 2 B. 4 C. 5 D. 16
  • 18. (Extra space for scratch work / notes)
  • 19. delete in BST • Why might delete be harder than insert? • Basic idea: • Three potential cases to fix:
  • 21. delete case: One Child delete(15) 20 9 2 15 5 12 30 7 10
  • 22. delete case: Two Children delete(5) 20 9 2 5 12 30 7 10 What can we replace 5 with?
  • 23. delete case: Two Children What can we replace the node with? Options:
  • 24. delete case: Two Children (example #2) delete(23) 30 9 2 23 5 12 7 10 18 19 15 32 25
  • 25. Changing as few nodes as possible, delete the following in order: 42, 14 Practice with insert, primer for delete 42 5 1 20 2 14 4 16
  • 26. delete through Lazy Deletion • Lazy deletion can work well for a BST • Simpler • Can do “real deletions” later as a batch • Some inserts can just “undelete” a tree node • But • Can waste space and slow down find operations • Make some operations more complicated: • e.g., findMin and findMax?
  • 27. buildTree for BST Let’s consider buildTree (insert values starting from an empty tree) Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST • If inserted in given order, what is the tree? • What big-O runtime for buildTree on this sorted input? • Is inserting in the reverse order any better?
  • 28. buildTree for BST Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST What we if could somehow re-arrange them • median first, then left median, right median, etc. 5, 3, 7, 2, 1, 4, 8, 6, 9 • What tree does that give us? • What big-O runtime?

Editor's Notes

  1. 10
  2. 11
  3. Ah, now the hard case. How do we delete a two child node? We remove it and replace it with what? It has all these left and right children that need to be greater and less than the new value (respectively). Is there any value that is guaranteed to be between the two subtrees? Two of them: the successor and predecessor! So, let’s just replace the node’s value with it’s successor and then delete the succ.
  4. Options: successor minimum node from right subtree findMin(node.right) predecessor maximum node from left subtree findMax(node.left)
  5. Ah, now the hard case. How do we delete a two child node? We remove it and replace it with what? It has all these left and right children that need to be greater and less than the new value (respectively). Is there any value that is guaranteed to be between the two subtrees? Two of them: the successor and predecessor! So, let’s just replace the node’s value with it’s successor and then delete the succ.