From c4df15ac93a5bae6350a8701d2f0df3d5453fe0d Mon Sep 17 00:00:00 2001 From: EdmundGoodman Date: Mon, 10 May 2021 17:40:22 +0100 Subject: [PATCH] Fixed typos, and added binary trees, and AVL trees, excluding re-balancing protocols --- README.md | 3 +- cs126/part10.md | 100 +++++++++++++++++++++++++++++++++++++++++++++++- index.md | 3 +- 3 files changed, 101 insertions(+), 5 deletions(-) mode change 100644 => 100755 cs126/part10.md diff --git a/README.md b/README.md index 86f56d38..c2b5f1a5 100755 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ If you are still confused, try Google-Fu on how to make a pull request, or see t This table is generated by the [all-contributors bot](https://allcontributors.org). The emojis mean the following in this project: - "🐛" means making a pull request to fix mistakes in someone else's notes -- "🖋" means contributing notes to the project (this supercedes 🐛) +- "🖋" means contributing notes to the project (this supersedes 🐛) - "🎨" means doing design work for the website - "🔥" means being responsible for internal administrative tasks (note: 🔥 is a custom key named "admin") @@ -155,4 +155,3 @@ If you have any additional questions, or want to become more involved in contrib **Many thanks once again for visiting.** - diff --git a/cs126/part10.md b/cs126/part10.md old mode 100644 new mode 100755 index 481015e4..020468ae --- a/cs126/part10.md +++ b/cs126/part10.md @@ -2,7 +2,105 @@ layout: CS126 part: true math: true -title: "Self-balancing trees" +title: "Binary search and self-balancing trees" pre: part9 nex: part11 --- + + + +# Binary search trees + +- Binary search trees used as a concrete implementation of ordered maps, with items being stored in the tree ordered by their key + + - Search tables are another concrete implementation of ordered maps, but instead use a sorted sequence, normally an array, which is searchable with binary search in $$O(log\ n)$$, but requires $$O(n)$$ for insertion and removal. This means they are only effective for either small maps, or cases where there are few insertions and deletions + +- Supports nearest neighbour queries, finding next highest and next lowest items + +- Properties of binary search trees + + - External nodes store no items + - All left children of any internal node have a smaller key than their parent node + - All right children of any internal node have a larger key than their parent node + - In-order traversals yield a sequence of the keys in ascending order + +- Searching + + - Start at the root, and recursively proceed down the appropriate subtrees until the key or an external node is found + + ``` + Function Search(n, k) + If n is an external node + Return that the key is not in the tree + Else if the key being searched for is less than the value of the current node + Return Search called on the left child of n, and the same k + Else if the key being searched for is greater than the value of the current node + Return Search called on the right child of n, and the same k + Else (the key is equal to the one being search for) + Return that the key is in the tree + + Let r <- the root node of the tree to search + Let k <- the key to search for + Search(r, k) + ``` + +- Insertion + + - Perform the searching operation, but when an external node is found, instead of returning that the key is not present, set that internal node as the key to insert, and give it two external child nodes + +- Deletion + + - If the node has no internal children + - Replace it with an external key + - If the node has only one internal child + - Overwrite it with that child + - Discard the previous position of the child and its children, + - Add two external nodes to it + - If the node has two internal children + - Find the node that immediately follows it in an in-order traversal + - Overwrite it with that node + - Set that node to be external, and discard its left child, which must itself be an external node + +- Performance + + - In all cases, the space complexity is $$O(n)$$ + - The time complexity for searching, inserting and deleting is dependent on the height of the tree + - If the tree is balanced, then the height is $$log\ n$$, so the time for these operations is $$O(log\ n)$$ + - In the worst case, the tree can be totally unbalanced, just a straight line of internal nodes, in which case the height is $$n$$, so the time for these operations is $$O(n)$$ + + + +# AVL trees + +- AVL trees are a concrete implementation of self-balancing binary search tree + + - Insertion and deletion operations re-arrange the tree to ensure it remains balanced + - Named after its creators, Adelson, Velsky and Landis + - Other self-balancing binary search trees exist, such as red-black trees, but this is a common approach to implementing such an ADT + +- Requirements to be a self-balancing binary search tree + + - Each node has at most two child nodes + - For every internal node in the tree, the heights of the child subtrees can differ by at most one + +- These requirements ensure that the maximum height to store $$n$$ keys is $$log\ n$$, which can be proved by induction + +- Searching is approached as it is in a normal binary search tree + + + *incomplete* + +- Tri-node restructuring + +- Re-balancing VS restructuring + +- Insertion + +- Deletion + + +- Performance + + - In all cases, the space complexity is $$O(n)$$ + - Searching takes $$O(log\ n)$$ time + - Insertion and deletion are also $$O(log\ n)$$, as searching for the element is $$O(log\ n)$$, and then restructuring the tree to maintain the balance property is also $$O(log\ n)$$ \ No newline at end of file diff --git a/index.md b/index.md index 14ad4c47..006d6006 100755 --- a/index.md +++ b/index.md @@ -146,7 +146,7 @@ If you are still confused, try Google-Fu on how to make a pull request, or see t This table is generated by the [all-contributors bot](https://allcontributors.org). The emojis mean the following in this project: - "🐛" means making a pull request to fix mistakes in someone else's notes -- "🖋" means contributing notes to the project (this supercedes 🐛) +- "🖋" means contributing notes to the project (this supersedes 🐛) - "🎨" means doing design work for the website - "🔥" means being responsible for internal administrative tasks (note: 🔥 is a custom key named "admin") @@ -157,4 +157,3 @@ This table is generated by the [all-contributors bot](https://allcontributors.or Currently, the maintainers/admins for this project are: Akram Ahmad, Edmund Goodman, Justin Tan, Yijun Hu (founding members), and Josh Fitzmaurice and Leo Riviera. If you have any additional questions, or want to become more involved in contributing to this project, please feel free to DM us on WhatsApp or Discord (search for our names within the main first year CS group chat, or the grey or cult servers). -