Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9....
Transcript of Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9....
![Page 1: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/1.jpg)
Programming Languages and Techniques
(CIS120)
Lecture 7 September 11th, 2015
Binary Search Trees
(Lecture notes Chapter 7)
![Page 2: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/2.jpg)
Announcements • Homework 2 is online
– due Tuesday, Sept. 15th
• RecitaJon SecJon 208 Weds. 5-‐6 has moved from Moore 100B to Moore 207 – Note: SecJon 207, also Weds. 5-‐6, remains in Moore 100A
• My office hours next week: Tuesday 3:30 – 5:00 – (not Monday, this should be the last such change)
CIS120
![Page 3: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/3.jpg)
Trees as containers
Big idea: find things faster by searching less
![Page 4: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/4.jpg)
Trees as Containers • Like lists, trees aggregate (possibly ordered) data • As we did for lists, we can write a funcJon to determine
whether the data structure contains a parJcular element
CIS120
type tree = | Empty | Node of tree * int * tree
![Page 5: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/5.jpg)
Searching for Data in a Tree
• This funcJon searches through the tree, looking for n • In the worst case, it might have to traverse the en0re tree
– This version uses pre-‐order traversal (other traversal orders have the same worst case traversal Jme…why?)
CIS120
let rec contains (t:tree) (n:int) : bool = begin match t with | Empty -> false | Node(lt,x,rt) -> x = n || (contains lt n) || (contains rt n) end
![Page 6: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/6.jpg)
Search during (contains t 8)
CIS120
5
1
0 3
7
9
8 ✓
![Page 7: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/7.jpg)
Challenge: Faster Search?
CIS120
![Page 8: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/8.jpg)
Binary Search Trees • Key insight:
Ordered data can be searched more quickly – This is why telephone books are arranged alphabeJcally – But requires the ability to focus on half of the current data
• A binary search tree (BST) is a binary tree with some addiJonal invariants*:
CIS120
• Node(lt,x,rt) is a BST if -‐ lt and rt are both BSTs -‐ all nodes of lt are < x-‐ all nodes of rt are > x
• Empty is a BST
*An data structure invariant is a set of constraints about the way that the data is organized. “types” (e.g. list or tree) are one kind of invariant, but we ocen impose addiJonal constraints.
![Page 9: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/9.jpg)
An Example Binary Search Tree
5
1
0 3
7
9
8
<
<
<
>
> >
Note that the BST invariants hold for this tree.
• Node(lt,x,rt) is a BST if -‐ lt and rt are both BSTs -‐ all nodes of lt are < x-‐ all nodes of rt are > x
• Empty is a BST
![Page 10: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/10.jpg)
Search in a BST: (lookup t 8)
CIS120
5
1
0 3
7
9
8
<
<
<
>
> >
8 > 5
8 > 7
8 < 9
✓
![Page 11: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/11.jpg)
Searching a BST
• The BST invariants guide the search. • Note that lookup may return an incorrect answer if the input
is not a BST! – This funcJon assumes that the BST invariants hold of t.
(* Assumes that t is a BST *)let rec lookup (t:tree) (n:int) : bool = begin match t with | Empty -> false | Node(lt,x,rt) -> if x = n then true else if n < x then (lookup lt n) else (lookup rt n) end
![Page 12: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/12.jpg)
BST Performance • lookup takes Jme proporJonal to the height of the tree.
– not the size of the tree (as it does with contains) • In a balanced tree, the lengths of the paths from the root to
each leaf are (almost) the same. – no leaf is too far from the root – the height of the BST is minimized – the height of a balanced binary tree is roughly log2(N) where N is the
number of nodes in the tree
1 2 3 4 5 6
5
1
0 3
7
9
balanced unbalanced
![Page 13: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/13.jpg)
4
2
1 3
5
6
7 • Node(lt,x,rt) is a BST if
-‐ lt and rt are both BSTs -‐ all nodes of lt are < x-‐ all nodes of rt are > x
• Empty is a BST
Is this a BST??
1. yes 2. no
Answer: no, 7 to the lec of 6
![Page 14: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/14.jpg)
1
2
• Node(lt,x,rt) is a BST if -‐ lt and rt are both BSTs -‐ all nodes of lt are < x-‐ all nodes of rt are > x
• Empty is a BST
Is this a BST??
1. yes 2. no
Answer: Yes
3
4
5
6
![Page 15: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/15.jpg)
4
2
1 5
7
9
8 • Node(lt,x,rt) is a BST if
-‐ lt and rt are both BSTs -‐ all nodes of lt are < x-‐ all nodes of rt are > x
• Empty is a BST
Is this a BST??
1. yes 2. no
Answer: no, 5 to the lec of 4
![Page 16: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/16.jpg)
4
2
1 3
4
9
8 • Node(lt,x,rt) is a BST if
-‐ lt and rt are both BSTs -‐ all nodes of lt are < x-‐ all nodes of rt are > x
• Empty is a BST
Is this a BST??
1. yes 2. no
Answer: no, 4 to the right of 4
![Page 17: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/17.jpg)
4
• Node(lt,x,rt) is a BST if -‐ lt and rt are both BSTs -‐ all nodes of lt are < x-‐ all nodes of rt are > x
• Empty is a BST
Is this a BST??
1. yes 2. no
Answer: yes
![Page 18: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/18.jpg)
• Node(lt,x,rt) is a BST if -‐ lt and rt are both BSTs -‐ all nodes of lt are < x-‐ all nodes of rt are > x
• Empty is a BST
Is this a BST??
1. yes 2. no
Answer: yes
![Page 19: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/19.jpg)
ConstrucJng BSTs
InserJng an element
![Page 20: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/20.jpg)
How do we construct a BST? • OpJon 1:
– Build a tree – Check that the BST invariants hold (unlikely!) – ImpracJcally inefficient
• OpJon 2: – Write funcJons for building BSTs from other BSTs
• e.g. “insert an element”, “delete an element”, … – StarJng from some trivial BST (e.g. Empty), apply these
funcJons to get the BST we want – If each of these funcJons preserves the BST invariants, then any
tree we get from them will be a BST by construc0on • No need to check!
– Ideally: “rebalance” the tree to make lookup efficient (NOT in CIS 120, see CIS 121)
![Page 21: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/21.jpg)
InserJng a new node: (insert t 4)
5
1
0 3
7
9
8
<
<
<
>
> >
4 < 5
4 > 1
4 > 3
?
![Page 22: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/22.jpg)
InserJng a new node: (insert t 4)
5
1
0 3
7
9
8
<
<
<
>
> >
4
>
![Page 23: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/23.jpg)
InserJng Into a BST
• Note the similarity to searching the tree. • Note that the result is a new tree with one more Node; the
original tree is unchanged • Assuming that t is a BST, the result is also a BST. (Why?)
(* Insert n into the BST t *)let rec insert (t:tree) (n:int) : tree = begin match t with | Empty -> Node(Empty,n,Empty) | Node(lt,x,rt) ->
if x = n then t else if n < x then Node(insert lt n, x, rt) else Node(lt, x, insert rt n) end
![Page 24: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/24.jpg)
ConstrucJng BSTs
DeleJng an element
![Page 25: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/25.jpg)
DeleJon – No Children: (delete t 3)
5
1
0 3
7
9
8
<
<
<
>
> >
3 < 5
3 > 1
![Page 26: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/26.jpg)
DeleJon – No Children: (delete t 3)
5
1
0
7
9
8
<
<
<
>
>
If the node to be deleted has no children, simply replace it by the Empty tree.
![Page 27: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/27.jpg)
DeleJon – One Child: (delete t 7)
5
1
0 3
7
9
8
<
<
<
>
> >
7 > 5
![Page 28: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/28.jpg)
DeleJon – One Child: (delete t 7)
5
1
0 3
9
8
<
< <
>
>
If the node to be delete has one child, replace the deleted node by its child.
![Page 29: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/29.jpg)
DeleJon – Two Children: (delete t 5)
5
1
0 3
7
9
8
<
<
<
>
> >
![Page 30: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/30.jpg)
DeleJon – Two Children: (delete t 5)
3
1
0
7
9
8
<
<
<
>
>
3
If the node to be delete has two children, promote the maximum child of the lec tree.
![Page 31: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/15fa/lectures/... · 2015. 9. 20. · Programming)Languages)) and)Techniques) (CIS120)) Lecture)7) September)11th,2015)](https://reader035.fdocuments.in/reader035/viewer/2022071216/604729017de8a87af372b699/html5/thumbnails/31.jpg)
Would it also work to move the smallest label from the right-‐hand subtree?
1. yes 2. no
Answer: yes