From 953eaf8970718723cf182a7403da8eab6ce242f2 Mon Sep 17 00:00:00 2001 From: Moshe Date: Tue, 14 Aug 2018 08:20:54 -0400 Subject: [PATCH] Update README.md (#165) Add Pseudocode and Big O --- .../tree/binary-search-tree/README.md | 200 ++++++++++++++++++ 1 file changed, 200 insertions(+) diff --git a/src/data-structures/tree/binary-search-tree/README.md b/src/data-structures/tree/binary-search-tree/README.md index 21e76391..32b873d6 100644 --- a/src/data-structures/tree/binary-search-tree/README.md +++ b/src/data-structures/tree/binary-search-tree/README.md @@ -27,6 +27,206 @@ The leaves are not drawn. ![Binary Search Tree](https://upload.wikimedia.org/wikipedia/commons/d/da/Binary_search_tree.svg) +## Pseudocode + +### 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 + + 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 + +### 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 + +### 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 + +### 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 + +### 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 + +### 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 + + +## Big O + +### Time Complexity + +Access: O(log(n)) +Search: O(log(n)) +Insert: O(log(n)) +Delete: O(log(n)) + + +### Space Complexity + +O(n) + + ## References - [Wikipedia](https://en.wikipedia.org/wiki/Binary_search_tree)