From b0c9057cdbc113cae469778861635a7257434035 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Tue, 14 Aug 2018 15:46:58 +0300 Subject: [PATCH] Fix pseudocode formatting. --- .../tree/breadth-first-search/README.md | 44 +- .../doubly-linked-list/README.md | 127 +++--- .../tree/binary-search-tree/README.md | 377 ++++++++++-------- 3 files changed, 291 insertions(+), 257 deletions(-) diff --git a/src/algorithms/tree/breadth-first-search/README.md b/src/algorithms/tree/breadth-first-search/README.md index 4c897cae..fe31217a 100644 --- a/src/algorithms/tree/breadth-first-search/README.md +++ b/src/algorithms/tree/breadth-first-search/README.md @@ -10,29 +10,27 @@ nodes first, before moving to the next level neighbors. ## Pseudocode - BFS(root) - Pre: root is the node of the BST - Post: the nodes in the BST have been visited in breadth first order - q ← queue - while root = ø - yield root.value - if root.left = ø - q.enqueue(root.left) - end if - if root.right = ø - q.enqueue(root.right) - end if - if !q.isEmpty() - root ← q.dequeue() - else - root ← ø - end if - end while - end BFS - -## Space and Time Complexity: - -O(bd + 1) +```text +BFS(root) + Pre: root is the node of the BST + Post: the nodes in the BST have been visited in breadth first order + q ← queue + while root = ø + yield root.value + if root.left = ø + q.enqueue(root.left) + end if + if root.right = ø + q.enqueue(root.right) + end if + if !q.isEmpty() + root ← q.dequeue() + else + root ← ø + end if + end while +end BFS +``` ## References diff --git a/src/data-structures/doubly-linked-list/README.md b/src/data-structures/doubly-linked-list/README.md index 83667e6a..da73f677 100644 --- a/src/data-structures/doubly-linked-list/README.md +++ b/src/data-structures/doubly-linked-list/README.md @@ -19,77 +19,84 @@ potentially more efficient (for nodes other than first nodes) because there is no need to keep track of the previous node during traversal or no need to traverse the list to find the previous node, so that its link can be modified. -## Pseudocode +## Pseudocode for Basic Operations ### Insert - Add(value) - Pre: value is the value to add to the list - Post: value has been placed at the tail of the list - n ← node(value) - if head = ø - head ← n - tail ← n - else - n.previous ← tail - tail.next ← n1 - tail ← n - end if - end Add +```text +Add(value) + Pre: value is the value to add to the list + Post: value has been placed at the tail of the list + n ← node(value) + if head = ø + head ← n + tail ← n + else + n.previous ← tail + tail.next ← n + tail ← n + end if +end Add +``` ### Delete - Remove(head, value) - Pre: head is the head node in the list - value is the value to remove from the list - Post: value is removed from the list, true; otherwise false - if head = ø - return false - end if - if value = head.value - if head = tail - head ← ø - tail ← ø - else - head ← head.Next - head.previous ← ∅ - end if - return true - end if - n ← head.next - while n = ø and value = n.value - n ← n.next - end while - if n = tail - tail ← tail.previous - tail.next ← ø - return true - else if n = ø - n.previous.next ← n.next - n.next.previous ← n.previous - return true - end if - return false - end Remove + +```text +Remove(head, value) + Pre: head is the head node in the list + value is the value to remove from the list + Post: value is removed from the list, true; otherwise false + if head = ø + return false + end if + if value = head.value + if head = tail + head ← ø + tail ← ø + else + head ← head.Next + head.previous ← ø + end if + return true + end if + n ← head.next + while n = ø and value = n.value + n ← n.next + end while + if n = tail + tail ← tail.previous + tail.next ← ø + return true + else if n = ø + n.previous.next ← n.next + n.next.previous ← n.previous + return true + end if + return false +end Remove +``` ### Reverse Traversal - ReverseTraversal(tail) - Pre: tail is the node of the list to traverse - Post: the list has been traversed in reverse order - n ← tail - while n = ø - yield n.value - n ← n.previous - end while - end Reverse Traversal + +```text +ReverseTraversal(tail) + Pre: tail is the node of the list to traverse + Post: the list has been traversed in reverse order + n ← tail + while n = ø + yield n.value + n ← n.previous + end while +end Reverse Traversal +``` -## Big O +## Complexities ## Time Complexity -Access: O(n) -Search: O(n) -Insert: O(1) -Delete: O(1) +| Access | Search | Insertion | Deletion | +| :-------: | :-------: | :-------: | :-------: | +| O(n) | O(n) | O(1) | O(1) | ### Space Complexity diff --git a/src/data-structures/tree/binary-search-tree/README.md b/src/data-structures/tree/binary-search-tree/README.md index 32b873d6..3ceb6ce6 100644 --- a/src/data-structures/tree/binary-search-tree/README.md +++ b/src/data-structures/tree/binary-search-tree/README.md @@ -27,206 +27,235 @@ The leaves are not drawn. ![Binary Search Tree](https://upload.wikimedia.org/wikipedia/commons/d/da/Binary_search_tree.svg) -## Pseudocode +## Pseudocode for Basic Operations ### Insertion - insert(value) - Pre: value has passed custome type checks for type T - Post: value has been placed in the correct location in the tree - if root = ø - root ← node(value) - else - insertNode(root, value) - end if - end insert +```text +insert(value) + Pre: value has passed custom type checks for type T + Post: value has been placed in the correct location in the tree + if root = ø + root ← node(value) + else + insertNode(root, value) + end if +end insert +``` - insertNode(current, value) - Pre: current is the node to start from - Post: value has been placed in the correct location in the tree - if value < current.value - if current.left = ø - current.left ← node(value) - else - InsertNode(current.left, value) - end if - else - if current.right = ø - current.right ← node(value) - end if - end if - end insertNode +```text +insertNode(current, value) + Pre: current is the node to start from + Post: value has been placed in the correct location in the tree + if value < current.value + if current.left = ø + current.left ← node(value) + else + InsertNode(current.left, value) + end if + else + if current.right = ø + current.right ← node(value) + else + InsertNode(current.right, value) + end if + end if +end insertNode +``` ### Searching - contains(root, value) - Pre: root is the root node of the tree, value is what we would like to locate - Post: value is either located or not - if root = ø - return false - end if - if root.value = value - return true - else if value < root.value - return contains(root.left, value) - else - return contains(root.right, value) - end if - end contains +```text +contains(root, value) + Pre: root is the root node of the tree, value is what we would like to locate + Post: value is either located or not + if root = ø + return false + end if + if root.value = value + return true + else if value < root.value + return contains(root.left, value) + else + return contains(root.right, value) + end if +end contains +``` + ### Deletion - remove(value) - Pre: value is the value of the node to remove, root is the node of the BST - count is the number of items in the BST - Post: node with value is removed if found in which case yields true, otherwise false - nodeToRemove ← findNode(value) - if nodeToRemove = ø - return false - end if - parent ← findParent(value) - if count = 1 - root ← ø - else if nodeToRemove.left = ø and nodeToRemove.right = ø - if nodeToRemove.value < parent.value - parent.left ← nodeToRemove.right - else - parent.right ← nodeToRemove.right - end if - else if nodeToRemove.left = ø and nodeToRemove.right = ø - if nodeToRemove.value < parent.value - parent.left ← nodeToRemove.left - else - parent.right ← nodeToRemove.left - end if - else - largestValue ← nodeToRemove.left - while largestValue.right = ø - largestValue ← largestValue.right - end while - findParent(largestValue.value).right ← ø - nodeToRemove.value ← largestValue.value - end if - count ← count - 1 - return true - end remove +```text +remove(value) + Pre: value is the value of the node to remove, root is the node of the BST + count is the number of items in the BST + Post: node with value is removed if found in which case yields true, otherwise false + nodeToRemove ← findNode(value) + if nodeToRemove = ø + return false + end if + parent ← findParent(value) + if count = 1 + root ← ø + else if nodeToRemove.left = ø and nodeToRemove.right = ø + if nodeToRemove.value < parent.value + parent.left ← nodeToRemove.right + else + parent.right ← nodeToRemove.right + end if + else if nodeToRemove.left = ø and nodeToRemove.right = ø + if nodeToRemove.value < parent.value + parent.left ← nodeToRemove.left + else + parent.right ← nodeToRemove.left + end if + else + largestValue ← nodeToRemove.left + while largestValue.right = ø + largestValue ← largestValue.right + end while + findParent(largestValue.value).right ← ø + nodeToRemove.value ← largestValue.value + end if + count ← count - 1 + return true +end remove +``` ### Find Parent of Node - findParent(value, root) - Pre: value is the value of the node we want to find the parent of - root is the root node of the BST and is != ø - Post: a reference to the prent node of value if found; otherwise ø - if value = root.value - return ø - end if - if value < root.value - if root.left = ø - return ø - else if root.left.value = value - return root - else - return findParent(value, root.left) - end if - else - if root.right = ø - return ø - else if root.right.value = value - return root - else - return findParent(value, root.right) - end if - end if - end findParent + +```text +findParent(value, root) + Pre: value is the value of the node we want to find the parent of + root is the root node of the BST and is != ø + Post: a reference to the prent node of value if found; otherwise ø + if value = root.value + return ø + end if + if value < root.value + if root.left = ø + return ø + else if root.left.value = value + return root + else + return findParent(value, root.left) + end if + else + if root.right = ø + return ø + else if root.right.value = value + return root + else + return findParent(value, root.right) + end if + end if +end findParent +``` ### Find Node - findNode(root, value) - Pre: value is the value of the node we want to find the parent of - root is the root node of the BST - Post: a reference to the node of value if found; otherwise ø - if root = ø - return ø - end if - if root.value = value - return root - else if value < root.value - return findNode(root.left, value) - else - return findNode(root.right, value) - end if - end findNode + +```text +findNode(root, value) + Pre: value is the value of the node we want to find the parent of + root is the root node of the BST + Post: a reference to the node of value if found; otherwise ø + if root = ø + return ø + end if + if root.value = value + return root + else if value < root.value + return findNode(root.left, value) + else + return findNode(root.right, value) + end if +end findNode +``` ### Find Minimum - findMin(root) - Pre: root is the root node of the BST - root = ø - Post: the smallest value in the BST is located - if root.left = ø - return root.value - end if - findMin(root.left) - end findMin - - -### Find Maximim - findMax(root) - Pre: root is the root node of the BST - root = ø - Post: the largest value in the BST is located - if root.right = ø - return root.value - end if - findMax(root.right) - end findMax - - ### Traversal - #### InOrder - inorder(root) - Pre: root is the root node of the BST - Post: the nodes in the BST have been visited in inorder - if root = ø - inorder(root.left) - yield root.value - inorder(root.right) - end if - end inorder - #### PreOrder - preorder(root) - Pre: root is the root node of the BST - Post: the nodes in the BST have been visited in preorder - if root = ø - yield root.value - preorder(root.left) - preorder(root.right) - end if - end preorder - #### PostOrder - postorder(root) - Pre: root is the root node of the BST - Post: the nodes in the BST have been visited in postorder - if root = ø - postorder(root.left) - postorder(root.right) - yield root.value - end if - end postorder +```text +findMin(root) + Pre: root is the root node of the BST + root = ø + Post: the smallest value in the BST is located + if root.left = ø + return root.value + end if + findMin(root.left) +end findMin +``` + +### Find Maximum + +```text +findMax(root) + Pre: root is the root node of the BST + root = ø + Post: the largest value in the BST is located + if root.right = ø + return root.value + end if + findMax(root.right) +end findMax +``` + +### Traversal + +#### InOrder Traversal + +```text +inorder(root) + Pre: root is the root node of the BST + Post: the nodes in the BST have been visited in inorder + if root = ø + inorder(root.left) + yield root.value + inorder(root.right) + end if +end inorder +``` + +#### PreOrder Traversal + +```text +preorder(root) + Pre: root is the root node of the BST + Post: the nodes in the BST have been visited in preorder + if root = ø + yield root.value + preorder(root.left) + preorder(root.right) + end if +end preorder +``` + +#### PostOrder Traversal + +```text +postorder(root) + Pre: root is the root node of the BST + Post: the nodes in the BST have been visited in postorder + if root = ø + postorder(root.left) + postorder(root.right) + yield root.value + end if +end postorder +``` - -## Big O +## Complexities ### Time Complexity -Access: O(log(n)) -Search: O(log(n)) -Insert: O(log(n)) -Delete: O(log(n)) - +| Access | Search | Insertion | Deletion | +| :-------: | :-------: | :-------: | :-------: | +| O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | ### Space Complexity O(n) - ## References - [Wikipedia](https://en.wikipedia.org/wiki/Binary_search_tree)