Binary Search Tree

Read this in other languages: Português

In computer science, binary search trees (BST), sometimes called ordered or sorted binary trees, are a particular type of container: data structures that store “items” (such as numbers, names etc.) in memory. They allow fast lookup, addition and removal of items, and can be used to implement either dynamic sets of items, or lookup tables that allow finding an item by its key (e.g., finding the phone number of a person by name).

Binary search trees keep their keys in sorted order, so that lookup and other operations can use the principle of binary search: when looking for a key in a tree (or a place to insert a new key), they traverse the tree from root to leaf, making comparisons to keys stored in the nodes of the tree and deciding, on the basis of the comparison, to continue searching in the left or right subtrees. On average, this means that each comparison allows the operations to skip about half of the tree, so that each lookup, insertion or deletion takes time proportional to the logarithm of the number of items stored in the tree. This is much better than the linear time required to find items by key in an (unsorted) array, but slower than the corresponding operations on hash tables.

A binary search tree of size 9 and depth 3, with 8 at the root. The leaves are not drawn.

Binary Search Tree

Pseudocode for Basic Operations

Insertion

  1. insert(value)
  2. Pre: value has passed custom type checks for type T
  3. Post: value has been placed in the correct location in the tree
  4. if root = ø
  5. root node(value)
  6. else
  7. insertNode(root, value)
  8. end if
  9. end insert
  1. insertNode(current, value)
  2. Pre: current is the node to start from
  3. Post: value has been placed in the correct location in the tree
  4. if value < current.value
  5. if current.left = ø
  6. current.left node(value)
  7. else
  8. InsertNode(current.left, value)
  9. end if
  10. else
  11. if current.right = ø
  12. current.right node(value)
  13. else
  14. InsertNode(current.right, value)
  15. end if
  16. end if
  17. end insertNode

Searching

  1. contains(root, value)
  2. Pre: root is the root node of the tree, value is what we would like to locate
  3. Post: value is either located or not
  4. if root = ø
  5. return false
  6. end if
  7. if root.value = value
  8. return true
  9. else if value < root.value
  10. return contains(root.left, value)
  11. else
  12. return contains(root.right, value)
  13. end if
  14. end contains

Deletion

  1. remove(value)
  2. Pre: value is the value of the node to remove, root is the node of the BST
  3. count is the number of items in the BST
  4. Post: node with value is removed if found in which case yields true, otherwise false
  5. nodeToRemove findNode(value)
  6. if nodeToRemove = ø
  7. return false
  8. end if
  9. parent findParent(value)
  10. if count = 1
  11. root ø
  12. else if nodeToRemove.left = ø and nodeToRemove.right = ø
  13. if nodeToRemove.value < parent.value
  14. parent.left nodeToRemove.right
  15. else
  16. parent.right nodeToRemove.right
  17. end if
  18. else if nodeToRemove.left != ø and nodeToRemove.right != ø
  19. next nodeToRemove.right
  20. while next.left != ø
  21. next next.left
  22. end while
  23. if next != nodeToRemove.right
  24. remove(next.value)
  25. nodeToRemove.value next.value
  26. else
  27. nodeToRemove.value next.value
  28. nodeToRemove.right nodeToRemove.right.right
  29. end if
  30. else
  31. if nodeToRemove.left = ø
  32. next nodeToRemove.right
  33. else
  34. next nodeToRemove.left
  35. end if
  36. if root = nodeToRemove
  37. root = next
  38. else if parent.left = nodeToRemove
  39. parent.left = next
  40. else if parent.right = nodeToRemove
  41. parent.right = next
  42. end if
  43. end if
  44. count count - 1
  45. return true
  46. end remove

Find Parent of Node

  1. findParent(value, root)
  2. Pre: value is the value of the node we want to find the parent of
  3. root is the root node of the BST and is != ø
  4. Post: a reference to the prent node of value if found; otherwise ø
  5. if value = root.value
  6. return ø
  7. end if
  8. if value < root.value
  9. if root.left = ø
  10. return ø
  11. else if root.left.value = value
  12. return root
  13. else
  14. return findParent(value, root.left)
  15. end if
  16. else
  17. if root.right = ø
  18. return ø
  19. else if root.right.value = value
  20. return root
  21. else
  22. return findParent(value, root.right)
  23. end if
  24. end if
  25. end findParent

Find Node

  1. findNode(root, value)
  2. Pre: value is the value of the node we want to find the parent of
  3. root is the root node of the BST
  4. Post: a reference to the node of value if found; otherwise ø
  5. if root = ø
  6. return ø
  7. end if
  8. if root.value = value
  9. return root
  10. else if value < root.value
  11. return findNode(root.left, value)
  12. else
  13. return findNode(root.right, value)
  14. end if
  15. end findNode

Find Minimum

  1. findMin(root)
  2. Pre: root is the root node of the BST
  3. root = ø
  4. Post: the smallest value in the BST is located
  5. if root.left = ø
  6. return root.value
  7. end if
  8. findMin(root.left)
  9. end findMin

Find Maximum

  1. findMax(root)
  2. Pre: root is the root node of the BST
  3. root = ø
  4. Post: the largest value in the BST is located
  5. if root.right = ø
  6. return root.value
  7. end if
  8. findMax(root.right)
  9. end findMax

Traversal

InOrder Traversal

  1. inorder(root)
  2. Pre: root is the root node of the BST
  3. Post: the nodes in the BST have been visited in inorder
  4. if root != ø
  5. inorder(root.left)
  6. yield root.value
  7. inorder(root.right)
  8. end if
  9. end inorder

PreOrder Traversal

  1. preorder(root)
  2. Pre: root is the root node of the BST
  3. Post: the nodes in the BST have been visited in preorder
  4. if root != ø
  5. yield root.value
  6. preorder(root.left)
  7. preorder(root.right)
  8. end if
  9. end preorder

PostOrder Traversal

  1. postorder(root)
  2. Pre: root is the root node of the BST
  3. Post: the nodes in the BST have been visited in postorder
  4. if root != ø
  5. postorder(root.left)
  6. postorder(root.right)
  7. yield root.value
  8. end if
  9. end postorder

Complexities

Time Complexity

AccessSearchInsertionDeletion
O(log(n))O(log(n))O(log(n))O(log(n))

Space Complexity

O(n)

References