programming)languages)) and)techniques) (cis120))cis120/archive/14sp/lectures/lec06.pdf ·...

15
Programming Languages and Techniques (CIS120) Lecture 6 Jan 31, 2014 Binary Trees and Binary Search Trees

Upload: others

Post on 31-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

Programming  Languages    and  Techniques  

(CIS120)  

Lecture  6  

Jan  31,  2014  

Binary  Trees  and  Binary  Search  Trees  

Page 2: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

Announcements  •  HW  1  stats  

–  Avg:  6  ¼  hours,  min:  1,  max:  20,  n:  123  –  Post  quesJons  about  the  hw  to  piazza,  specific  notes  to  TAs  in  comments.  Feedback  is  for  general  comments.    

•  2nd  Homework  assignment  due  Tuesday  •  Lecture  aUendance  grade  (i.e.  clickers)  

–  Flexibility  for  occasional  missed  lectures  due  to  minor  emergencies  

–  No  need  to  inform  staff  (or  send  CAR)  unless  you  have  a  major  emergency  

•  Read  Chapter  6  and  7  

CIS120  

Page 3: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

Binary  Trees  

Page 4: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

Binary  Trees  

CIS120  

3  

2  

0   1  

2  

3   1  

root  node  

root’s  right  child  

root’s    le\  child  

le\  subtree  

leaf  node  

A  binary  tree  is  either  empty,  or  a  node  with  at  most    two  children,  both  of  which  are  also  binary  trees.  

A  leaf  is  a  node  whose  children  are  both    empty.  

empty  

Page 5: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

RepresenJng  trees!

CIS120  

5  

1  

0   3  

7  

9  

8  

type tree = | Empty | Node of tree * int * tree

Empty

Node (Empty, 0, Empty)

Node (Node (Empty, 0, Empty), 1, Node (Empty, 3, Empty))

Page 6: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

Demo  

trees.ml    treeExamples.ml  

Page 7: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

Trees  as  Containers  

•  Like  lists,  trees  aggregate  ordered  data  •  As  we  did  for  lists,  we  can  write  a  funcJon  to  determine  whether  the  data  structure  contains  a  parJcular  element  

•  CHALLENGE:  can  we  use  the  tree  structure  to  make  this  process  faster?  

CIS120  

Page 8: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

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  enJre  tree  

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 9: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

Search  during  (contains t 8)!

CIS120  

5  

1  

0   3  

7  

9  

8  ✓  

Page 10: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

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  

Page 11: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

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  CIS120  

Page 12: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

Search  in  a  BST:  (lookup t 8)!

CIS120  

5  

1  

0   3  

7  

9  

8  

<  

<  

<  

>  

>   >  

8  >  5  

8  >  7  

8  <  9  

✓  

Page 13: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

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  

CIS120  Answer:  no,  7  to  the  le\  of  6  

Page 14: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

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  

CIS120  Answer:  no,  5  to  the  le\  of  4  

Page 15: Programming)Languages)) and)Techniques) (CIS120))cis120/archive/14sp/lectures/lec06.pdf · Programming)Languages)) and)Techniques) (CIS120)) Lecture)6) Jan)31,)2014) Binary)Trees)and)Binary)Search)Trees)

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  

CIS120  Answer:  yes