CSCI0200
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Help
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
--- tags: labs, spr22 --- # Lab 2 (CS15): Binary (Search) Trees ### [Hey Ya!](https://www.youtube.com/watch?v=-7XnDlYY9qw) As you listen to more 2000s songs, you keep updating your ranking as to which song is the best. One way you can conveniently store and update your ranking is to use a binary search tree! A *binary tree* is a tree in which every node has 0, 1, or 2 children/direct subtrees. A *binary search tree* is a special kind of binary tree in which at every node, all elements with smaller values are in the left subtree and all elements with larger values are in the right subtree. A duplicate value can be in either the left or the right subtree. In this lab, you will implement, and learn to test programs for, binary search trees. ### The Datatype Here are two possible datatypes for a binary tree: ```=pyret # version 1 data BinTree1: | emptytree | node(value :: Number, left :: BinTree1, right :: BinTree1) end ``` ```=pyret # version 2 data BinTree2: | leaf(value :: Number) | node(value :: Number, left :: BinTree2, right :: BinTree2) end ``` We saw the first type in class, and now you will work with the second type! ## Implementing Binary Search Trees with `BinTree` At [code.pyret.org](https://code.pyret.org/), create a new file to complete your lab! Complete the following exercise using the `BinTree2` definition (copy over only the `BinTree2` definition as the code won't compile if you have both `BinTree1` and `BinTree2` definitions in one file). Remember to use the binary tree code template that was shown in lecture. **Task:** Write a function `tree-depth` that takes a binary tree as input and produces the height of the longest path down the tree. **Task:** Create a set of sample trees and write a `check` block to test your `tree-depth` function. **Task:** Write a function `has-elt` that takes a binary search tree and a number and returns a boolean indicating whether the number is in the BST. **Task:** Develop a good set of tests for `has-elt`. The point here is to think about what combination of tree shapes and numbers cover the different cases of the algorithm. You can draw your trees on paper, indicating which numbers you would use for good `has-elt` tests. Write a short description of the situation that each tree/input is trying to test (e.g., we want to see your testing strategy, not just that you came up with some collection of examples.) **Task:** Try to write a function `add-elt` that takes a binary search tree (with the structure of `BinTree2`) and a number as input and produces a new binary search tree. The new tree should have all the same elements as the original, as well as the given number. You should move on to the next task after about **ten minutes.** :::spoiler Click if you're really stuck How did we add an element to our `BinTree1`-style trees in lecture? How can we try to do something similar with `BinTree2`? What do we have to do differently? Are we somehow limited by this new `BinTree2`? ::: <br> ::: spoiler Click if you're really *really* stuck Although `BinTree1` and `BinTree2` are fairly similar, the key difference that makes the `add-elt` function for `BinTree2` so difficult is the fact that leaves have their own value. For instance, take this example of a `BinTree2` tree (also drawn out below): `node(4, node(1, leaf(0), leaf(3)), leaf(5))` If we were to insert a new value, say `2`, how would we do this? You can imagine the following steps: 1. `2` is less than `4`, so travel to its left branch 2. `2` is greater than `1` so travel to its right branch 3. `2` is greater than `0`, so turn `leaf(0)` into a `node(0, __, __)` and place a `leaf(2)` into its right branch to get `node(0, __, leaf(2))` But now, we are left wondering what to put into the left branch of our current node. Because of our `BinTree2` definition, our only options are a `node` or a `leaf`, and neither seem to be appropriate here. *This is the problem!* We do not encounter this when we choose to use the `BinTree1` implementation where a leaf does not contain a value for the tree. If you are still confused, feel free to call over a TA. ::: <br> **Task:** Discuss how you would modify either the `BinTree2` datatype or the assumptions on the `add-elt` function so that you could successfully add an element to `BinTree2`. What are the upsides and downsides to these ideas? What do you notice about the code from lecture vs the code you wrote? Which version do you prefer and why? :::success **Checkpoint:** Tell your TA what you came up with for the questions, and have a TA check your work! ::: ### Runtime Refer to the [guide to doing run-time annotations](https://hackmd.io/RR1Bkq1LQtirSZl_OLwpew) as needed. Your functions will metaphorically ["walk a thousand miles," and "I need you"](https://youtu.be/Cwkej79U3ek) to figure out how long this will take! **Task:** Look at this annotated `add-elt` from class. The annotations have some errors. Find and fix the errors. ```=pyret fun add-elt(bst :: BinTree1, val :: Number) -> BinTree1: doc: "adds val to bst, obeying Binary Search Tree rules" cases(BinTree1) bst: # 1 access | empty => node(val, emptytree, emptytree) # 1 comparison, 3 accesses, 1 instantiation | node(v, l-tree, r-tree) => # 1 comparison if val == v: # 2 accesses bst else if val < v: # 2 accesses add-elt(l-tree, val) # T-add-elt(N / 2), 2 accesses else: add-elt(r-tree, val) # T-add-elt(N / 2), 2 accesses end end end # Total: T-add-elt(N) = 1 + 5 + 1 + 2 + 2 + T-add-elt(N/2) + 2 ``` Now, you will plot some runtimes over different intervals and discuss the implications. **Task:** copy the entirety of the following code to a new Pyret file: :::info You do not have to understand this code, but it is commented if you are curious! ::: :::spoiler **Code to copy** ```=pyret include math include plot include tables import color as C color-list = [list: C.peru, C.deep-sky-blue, C.sea-green, C.yellow, C.dark-blue] fun graph-big-o(max-x :: Number, runtime-funs :: List<(Number -> Number)>) -> Image: # for every function in runtime-funs, match it to a color and create a # function-plot plots-list = map2(lam(f, c): function-plot(f, _.{color: c}) end, runtime-funs, # make sure we match the length of color_list to the length of runtime-funs color-list.take(runtime-funs.length())) # find the maximum value of y over all of the functions all-max = max(runtime-funs.map(lam(f): f(max-x) end)) # display the plots of all of the functions display-multi-plot( plots-list, _.{ title: 'runtime of all functions', x-min: 0, x-max: max-x, y-min: 0, y-max: all-max }) end ``` ::: <br> You can now graph the runtime of up to 5 functions simultaneously! For example, to compare the runtime of `2*N + 1` to `N + 20` for an input size up to `N=30`, you could run ```=pyret graph-big-o(30, [list: lam(n): (2 * n) + 1 end, lam(n): n + 20 end]) ``` You can also define recurrence relations in the Pyret file, and plot them. Copy the following relation into the file: ```=pyret fun rec-log(n :: Number) -> Number: if n <= 1: 40 # base case else: 40 + rec-log(n / 2) # recurrence end end ``` Now you can use this function as an argument to `graph-big-o`: `graph-big-o(20, [list: rec-log])`. What do you notice about the "smoothness" of this graph, as opposed to the others? Why do you think this is? **Task:** Compare the runtime of three functions: logarithmic, linear, and quadratic. Run: ```=pyret graph-big-o(10, [list: rec-log, lam(n): (4 * n) + 60 end, lam(n): n * (n - 2) end]) ``` and try increasing the value of the 1st input until you find the points at which the different runtimes overtake each other. What do you notice? :::success **Checkpoint:** Show a TA what you came up with! ::: ### Contrasting to Java The following Java classes correspond to `BinTree1`: ```=java interface IBinTree {} class EmptyTree implements IBinTree {} class Node implements IBinTree { int val; IBinTree left; IBinTree right; } ``` A more conventional Java binary tree class might look like: ```=java class BinTree { int val; BinTree left; // use null when out of tree BinTree right; // use null when out of tree } ``` **Task:** Contrast these two approaches to Java classes. If you were to implement `addElt` as a method in each, how would the code differ between the two? What do you see as the benefits and tradeoffs of each? *Part of the punchline here is that the datatypes we get from functional programming are pure object-oriented: they don't allow a null-based implementation, which is not pure object-oriented (because in pure OO, code never asks "what type of data is this"). A takeaway here is that datatype-based functional programming is strongly related to OO programming from a class-structure perspective.* ## [U + Me = Us](https://www.youtube.com/watch?v=pMWxnjgvUQM): Design practice with expression trees Consider an expression like `(4 + 5) * (7 - 2)`. Programs *interpret* this expression by splitting it into subexpressions, as follows: ![expression as a tree](https://i.imgur.com/14iexRq.png) **Task:** Design a datatype that represents mathematical expressions, i.e. ``` data Expression: | ... end ``` **Task:** Create each of the following expressions using your datatype and constructors: - `5` - `3 + 2 + 4` - `(4 + 5) * (7 - 2)` - `6 - (-4 * (8 - 2))` **Task:** If you have time, write a function `calc` that takes an Expression and returns a number (being the result of that computation). ``` fun calc(expr :: Expression) -> Number: cases (Expression) expr: | ... end end ``` ::: info *Note*: This is a taste of how languages work under the hood: programming tools convert your program into trees like this, and use programs like `calc` to run your programs. ::: :::success **Checkoff!** Congratulations! Once a TA signs off on your work, you've completed this week's lab. Nice job! ::: ______________________________ *Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CSCI0200 document by filling out the [anonymous feedback form](https://forms.gle/JipS5Y32eRUdZcSZ6).*

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully