Stephen
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • 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
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Week 9 - Binary search trees ## Team Team name: SampleText Date: 4/20/1337 Members : Onurkan Kanli, Stephen van Rumpt, Hlib Hryshko | Role | Name | |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------| | **Facilitator** keeps track of time, assigns tasks and makes sure all the group members are heard and that decisions are agreed upon. | Hlib | | **Spokesperson** communicates group’s questions and problems to the teacher and talks to other teams; presents the group’s findings. | Onurkan | | **Reflector** observes and assesses the interactions and performance among team members. Provides positive feedback and intervenes with suggestions to improve groups’ processes. | Onurkan | | **Recorder** guides consensus building in the group by recording answers to questions. Collects important information and data. | Stephen | ## Activities ### Activity 1: Maintaining order in Arrays and Linked Lists - reprise | Algorithm | Array | Linked List | | -------- | -------- | -------- | | Value insertion | O(n) | O(n) | | Value removal | O(n) | O(n) | | Membership testing | O(log(n))| O(n) | - Time complexity of value insertion: For Array it will be O(n), because the program must find the place for it and move all the elements, which are bigger, one index to the right. For Linked Linked it will be the same value of O(n), because the program must find the place for the element and just add new element. - Time complexity of value removal: For array it will be O(n), because the program firstly finds the value and then moves all the elements after it to the left. So it is just 1 for loop from 0 to n - 1, which equals to n. For Linked List it will be O(n), because the program needs just to find the element and further it is just O(1) operation to insert a new element. - Time complexity of membership testing For Array the time complexity is O(log(n)), because binary search could be implemented and its time complexity is O(log(n)). For Linked List the time complexity is O(n), because the program must check every element. There for it is one for loop from 0 to n - 1, which has time complexity of O(n). ### Activity 2: Recognizing BSTs ![](https://i.imgur.com/j9Bb34A.png) Out of these 3 trees, the Binary Search Tree is tree number 3. - The first one is wrong, because the root value is bigger than a value in the right side of the tree(72 > 71) - The second one is wrong, because a value in the right side is less then the parent value(84 < 85) ### Activity 3: Building a BST The BST with 3 levels : Code: ```c= bintree_node root{5, new bintree_node {3, new bintree_node {2}, nullptr }, new bintree_node {11, new bintree_node {7}, nullptr} }; ``` Photo: ![](https://i.imgur.com/1t4IrKz.jpg) The BST with 4 levels: Code: ```c= bintree_node root{7,new bintree_node {5, new bintree_node {3, new bintree_node {2}, nullptr}, nullptr}, new bintree_node {11} }; ``` Photo: ![](https://i.imgur.com/NbmXqAQ.jpg) The BST with 5 levels: Code: ```c= bintree_node root{11, new bintree_node {7, new bintree_node {5, new bintree_node {3, new bintree_node {2}, nullptr}, nullptr}, nullptr}, nullptr}; ``` Photo: ![](https://i.imgur.com/4faWVtO.jpg) ### Activity 4: Searching for values ```c= bintree_node *bintree_node::find(int value) { if (m_value == value){ return this ; } else if (m_value > value && left() != nullptr){ return left()->find(value) ; } else if (m_value < value && right() != nullptr){ return right()->find(value) ; } else { return nullptr ; } } ``` ### Activity 5: Inserting values ```c= bintree_node* bintree_node::insert(int value) { if (value > m_value){ if (m_right == nullptr){ m_right = new bintree_node(value, this); return m_right; } else { return m_right->insert(value); } } else if(value < m_value){ if (m_left == nullptr){ m_left = new bintree_node(value, this); return m_left; } else { return m_left->insert(value); } } else{ return nullptr; } } ``` Photo: ![](https://i.imgur.com/0a5KvFJ.png) ### Activity 6: Properties of the minimum value (Stephen) - Can the minimum value in a node’s left subtree be greater than the minimum value in a node’s right subtree? Why (not)? No because if the minimum value that we’re looking for is not contained in the root node, then it may be located either in the left or in the right subtree. To decide which of these two subtrees we must search, is determined by a simple comparison: if the value we’re looking for is smaller than the root value, we continue our search in the left subtree. The right subtree wil always have values greater than the left subtree. - Can the node containing the minimum value have a left subtree? Why (not)? No , The minimum value is the last left subtree because there is no value smaller than that one leftover. - Which traversal strategy will lead to the node containing the minimum value of a tree? If the minimum value that we’re looking for is not contained in the root node, Then its located in the left subtree. From there we can keep going left until there is no left subtree left. ### Activity 7: Finding the minimum value And that's how you insert code blocks: ```c= bintree_node &bintree_node::minimum() { if (m_left == nullptr){ return *this; } else{ return m_left->minimum(); } } ``` ### Activity 8: Removing leafs ```c= if (m_left == nullptr && m_right == nullptr) { m_parent->replace_child(this, nullptr); return this; } ``` Photo: ![](https://i.imgur.com/hPwdBA0.png) ### Activity 9: Removing nodes that have one child ```c= if (m_left == nullptr) { m_parent->replace_child(this, m_right); return this; } else if (m_right == nullptr) { m_parent->replace_child(this, m_left); return this; } ``` Photo: ![](https://i.imgur.com/zfs1KPt.png) ### Activity 10: Moving values around ... ... ### Activity 11: Removing *full* nodes ```c= auto removed = find(m_right->minimum().m_value); m_value = removed->m_value; return removed->remove(); ``` Photo: ![](https://i.imgur.com/uZogKVj.png) ### Activity 12: Performance analysis ... ... ### Activity 13: Restoring balance (optional) ```c= bintree_node *bintree_node::from_vector(const std::vector<int> &vector, int min_idx, int max_idx) { int mid_idx = min_idx + (max_idx - min_idx)/2; bintree_node * left{}; if (mid_idx - min_idx > 0){ left = from_vector(vector, min_idx, mid_idx); } bintree_node * right{}; if (max_idx - mid_idx > 1){ right = from_vector(vector, mid_idx + 1, max_idx); } return new bintree_node(vector[mid_idx], left, right); } ``` Photo: ![](https://i.imgur.com/1AoINbk.png) ## Look back ### What we've learnt This week we learnt about binray search trees and how to work with them. ### What were the surprises The only real surprise we encountered was the dev bug with activity 9 but besides that no real problems arose. What may have been a surprise to some was how to work with a binary tree in the beginning. ### What problems we've encountered As mentioned before, we only really got stuck at activity 9 with that bug, and some minor confusions surrounding activity 10 and 12. ### What was or still is unclear Everything seems fairly clear to us. Binary trees in general were tricky at first but now seem a little bit like a breeze to work with. ### How did the group perform? Everything seemed to be finished within 2 work days but those days were spread very far apart. Performance was questionable but in the end we managed to finish this week off pretty well.

    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