張智孝
    • 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
    # 12728 - Container Tree > author: IntSys ###### tags: `BST` `iterator` `container` --- ## Brief :::spoiler **Class Definitions** ```cpp #include <cstdint> namespace oj { using size_type = uint64_t; using value_type = int32_t; using reference = value_type&; struct Node { size_type size; value_type* p_data; Node* lc; Node* rc; Node(value_type* pv) : size(1), p_data(pv), lc(nullptr), rc(nullptr) {} ~Node() { delete p_data; } }; struct iterator_impl_base { virtual reference operator*() const = 0; virtual bool operator!=(const iterator_impl_base&) const = 0; virtual iterator_impl_base* clone() const = 0; }; class set_iterator : public iterator_impl_base { protected: Node* _node; public: set_iterator(); set_iterator(Node* u); reference operator*() const; iterator_impl_base* clone() const; bool operator!=(const iterator_impl_base&) const; }; class iterator { protected: iterator_impl_base* _p; public: iterator(iterator_impl_base*); reference operator*() const; bool operator!=(const iterator&) const; }; struct container_base { virtual size_type size() const = 0; virtual bool empty() const = 0; virtual void clear() = 0; }; struct dynamic_size_container : container_base { virtual iterator begin() = 0; virtual iterator end() = 0; }; struct associative_container : dynamic_size_container { virtual void insert(const value_type&) = 0; }; struct sorted_container : associative_container { virtual iterator lower_bound(const value_type&) = 0; }; class set : public sorted_container { protected: // you have two pointers to spare :) do anything you wish Node* root; Node* infy; public: set(); ~set(); void clear(); size_type size() const; bool empty() const; iterator begin(); iterator end(); void insert(const value_type&); iterator lower_bound(const value_type&); }; }; ``` ::: ## Solution 0 ```cpp #include <cstdint> #include "function.h" #define INF INT32_MAX namespace oj { using size_type = uint64_t; using value_type = int32_t; using reference = value_type&; using const_reference = value_type const&; set_iterator::set_iterator() : _node(nullptr) {} set_iterator::set_iterator(Node* u) : _node(u) {} reference set_iterator::operator*() const { return *_node->p_data; } iterator_impl_base* set_iterator::clone() const { return new set_iterator(_node); } bool set_iterator::operator!=(const iterator_impl_base& rhs) const { return operator*() != *rhs; } iterator::iterator(iterator_impl_base* p) : _p(p->clone()) {} reference iterator::operator*() const { return **_p; } bool iterator::operator!=(const iterator& rhs) const { return *_p != *rhs._p; } size_type size_of_tree(Node* root); void delete_tree(Node* root); void insert_node(Node*& root, const_reference c_val); Node* search_least_upper(Node* root, Node* upper, const_reference key); // Return the number of nodes in a tree. size_type size_of_tree(Node* root) { if (root == nullptr) return 0; return size_of_tree(root->lc) + 1 + size_of_tree(root->rc); } // Delete a tree. void delete_tree(Node* root) { if (root != nullptr) { delete_tree(root->lc); delete_tree(root->rc); delete root; } } // Regular insertion. void insert_node(Node*& root, const_reference c_val) { if (root == nullptr) root = new Node(new value_type(c_val)); else if (*root->p_data == c_val) return; else if (*root->p_data < c_val) return insert_node(root->rc, c_val); else return insert_node(root->lc, c_val); } // Assumes a non-empty set. Node* search_least_upper(Node* root, Node* upper, const_reference key) { if (*root->p_data == key) return root; if (*root->p_data < key) { if (root->rc == nullptr) return upper; else return search_least_upper(root->rc, upper, key); } if (root->lc == nullptr) return (*root->p_data >= key) ? root : upper; else return search_least_upper(root->lc, root, key); } set::set() : root(nullptr), infy(new Node(new value_type(INF))) {} set::~set() { clear(); delete infy; } void set::clear() { delete_tree(root); root = nullptr; } size_type set::size() const { return empty() ? 0 : size_of_tree(root); } bool set::empty() const { return root == nullptr; } iterator set::begin() { set_iterator root_ptr(root); return iterator(&root_ptr); } iterator set::end() { set_iterator infy_ptr(infy); return iterator(&infy_ptr); } void set::insert(const_reference key) { insert_node(root, key); } iterator set::lower_bound(const_reference key) { set_iterator bound_ptr(search_least_upper(root, infy, key)); return iterator(&bound_ptr); } }; // By IntSys ``` ## Solution 1 ([Scapegoat Tree](https://en.wikipedia.org/wiki/Scapegoat_tree)) ```cpp #include <cstdint> #include <cmath> #include "function.h" #define INF INT32_MAX #define ALPHA 0.85 // Note: 0.5 < ALPHA < 1.0 // High ALPHA makes insertions quicker. // Low ALPHA makes lookups and deletions quicker. // Here, lower_bound() is a kind of lookup. namespace oj { using size_type = uint64_t; using value_type = int32_t; using reference = value_type&; using const_reference = value_type const&; set_iterator::set_iterator() : _node(nullptr) {} set_iterator::set_iterator(Node* u) : _node(u) {} reference set_iterator::operator*() const { return *_node->p_data; } iterator_impl_base* set_iterator::clone() const { return new set_iterator(_node); } bool set_iterator::operator!=(const iterator_impl_base& rhs) const { return operator*() != *rhs; } iterator::iterator(iterator_impl_base* p) : _p(p->clone()) {} reference iterator::operator*() const { return **_p; } bool iterator::operator!=(const iterator& rhs) const { return *_p != *rhs._p; } size_type size_of_tree(Node* root); void delete_tree(Node* root); void insert_node(Node*& root, const_reference c_val); bool scapegoat_insert(Node*& root, Node** path[], const_reference c_val, reference depth); Node* get_scapegoat(Node** path[], reference depth, size_type current_size); void BST_to_sorted_array(Node* root, value_type arr[], reference top); void sorted_array_to_balanced_BST(Node*& root, value_type arr[], value_type L, value_type R); Node* search_least_upper(Node* root, Node* upper, const_reference key); // Return the number of nodes in a tree. size_type size_of_tree(Node* root) { if (root == nullptr) return 0; return size_of_tree(root->lc) + 1 + size_of_tree(root->rc); } // Delete a tree. void delete_tree(Node* root) { if (root != nullptr) { delete_tree(root->lc); delete_tree(root->rc); delete root; } } // Regular insertion. void insert_node(Node*& root, const_reference c_val) { if (root == nullptr) root = new Node(new value_type(c_val)); else if (*root->p_data == c_val) return; else if (*root->p_data < c_val) return insert_node(root->rc, c_val); else return insert_node(root->lc, c_val); } // Insertion that records the path taken and the depth reached. Returns true upon success. bool scapegoat_insert(Node*& root, Node** path[], const_reference c_val, reference depth) { path[depth++] = &root; if (root == nullptr) { root = new Node(new value_type(c_val)); return true; } if (*root->p_data == c_val) return false; if (*root->p_data < c_val) return scapegoat_insert(root->rc, path, c_val, depth); return scapegoat_insert(root->lc, path, c_val, depth); } // Find the deepest scapegoat node in the tree by traveling back up the path given. Node* get_scapegoat(Node** path[], reference depth, size_type current_size) { if (--depth < 0) return nullptr; Node*& parent = *path[depth - 1]; Node* sibling = (parent->lc == *path[depth]) ? parent->rc : parent->lc; size_type sibling_size = size_of_tree(sibling); size_type parent_size = sibling_size + current_size + 1; if (current_size > ALPHA * parent_size || sibling_size > ALPHA * parent_size) return parent; current_size = parent_size; return get_scapegoat(path, depth, current_size); } // Move the data in a BST into the given array with inorder traversal, and delete the tree. void BST_to_sorted_array(Node* root, value_type arr[], reference top) { if (root != nullptr) { BST_to_sorted_array(root->lc, arr, top); arr[top++] = *root->p_data; BST_to_sorted_array(root->rc, arr, top); delete root; } } // Insert the data in a sorted array into a tree while making the new tree balanced. void sorted_array_to_balanced_BST(Node*& root, value_type arr[], value_type L, value_type R) { if (R - L == 1) return; value_type mid = (L + R) / 2; insert_node(root, arr[mid]); sorted_array_to_balanced_BST(root, arr, L, mid); sorted_array_to_balanced_BST(root, arr, mid, R); } // Assumes a non-empty set. Node* search_least_upper(Node* root, Node* upper, const_reference key) { if (*root->p_data == key) return root; if (*root->p_data < key) { if (root->rc == nullptr) return upper; else return search_least_upper(root->rc, upper, key); } if (root->lc == nullptr) return (*root->p_data >= key) ? root : upper; else return search_least_upper(root->lc, root, key); } set::set() : root(nullptr), infy(new Node(new value_type(INF))) {} set::~set() { clear(); delete infy; } void set::clear() { delete_tree(root); root = nullptr; } size_type set::size() const { return empty() ? 0 : root->size; } bool set::empty() const { return root == nullptr; } iterator set::begin() { set_iterator root_ptr(root); return iterator(&root_ptr); } iterator set::end() { set_iterator infy_ptr(infy); return iterator(&infy_ptr); } void set::insert(const_reference key) { value_type depth = 0; Node** path[100]; if (scapegoat_insert(root, path, key, depth)) root->size++; if (depth > floor(log2(root->size) / log2(1 / ALPHA) + 1)) { Node* scapegoat = get_scapegoat(path, depth, 1); Node*& scapegoat_parent = (depth > 2) ? *path[depth - 2] : root; if (scapegoat_parent->lc == *path[depth - 1]) scapegoat_parent->lc = nullptr; else scapegoat_parent->rc = nullptr; value_type val_arr[100]; value_type top = 0; BST_to_sorted_array(scapegoat, val_arr, top); sorted_array_to_balanced_BST(scapegoat_parent, val_arr, -1, top); } } iterator set::lower_bound(const_reference key) { set_iterator bound_ptr(search_least_upper(root, infy, key)); return iterator(&bound_ptr); } }; // By IntSys ``` ## Reference

    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