Skip to main content

15. Maps - Using binary search trees

24/03/23

Note intended to help with coding

  • Global view of tree
    • Can look at entire tree in one go
    • Human seeing a picture of a (small) tree
  • Local view of tree
    • What your code sees: "code perspective"
    • Code generally only sees a local portion of a tree and must work with that
  • Search for an element within a sorted array is fast
    • Array to be searched is halved at each iteration, hence O(logn)O(\log n)
  • Only works because of the step of knowing whether to go left or right
  • But arrays suffer from being slow, O(n)O(n), to insert new elements. As needs to shift O(n)O(n) elements to make room for them
  • Search trees attempt to fix the inefficiency of insertion whilst keeping good O(logn)O(\log n) properties of binary search

Binary Search Trees

  • Binary search tree is a binary tree storing key-value entries at its internal nodes and satisfying the following "search tree" property:
    • Let uu, vv, and ww be any three nodes such that uu is in the left subtree of vv and ww is in the right subtree of vv;
    • key(u)key(v)key(w)key(u)\le key(v) \le key(w)
  • External nodes do not store items, and likely are not actually implemented, but are just null links from the parent

  • To search for the key kk, trace a downward path starting at the root
  • The next node visited depends on the outcome of the comparison of kk with the key of the current node
  • If we reach a leaf, the key is not found and we return null

Recursive vs. Iterative

  • Recursive programs are easier to implement, but less efficient because of the overhead of a function call
  • For best efficiency, will need to convert to an iterative program
  • while (test) {...}

Fundamental property of search tree - An inorder traversal of a (binary) search trees visits the keys in increase order. To access the minimum key, just need to 'always go left'

Insertion

  • Insert kk where a get(k) would find it.
  • If kk is already in tree then just replace the value, otherwise, kk is not already in the tree, and let ww be the leaf reached by the search

Deletion

  • We remove nn from the tree and connect it to the parent. Still works if nn is a right child and has a left child
  • For removing one with two children:
    1. Find the internal node ww that follows nn in an inorder traversal
    2. Copy key(w)key(w) into node nn
    3. Remove node ww and its left child zz (must be a leaf) by means of same procedure as before for one child

Balanced Trees

  • Binary search trees: if all levels filled, then search, insertion and deletion are O(logN)O(\log N)
  • However performance may deteriorate to linear if nodes are inserted in order

Performance

  • Binary search tree of height hh with nn items. The space used is O(n)O(n). Methods take O(h)O(h)
  • The height hh is O(n)O(n) in the worst case and O(logn)O(\log n) in the best case

Self-Balancing

  • Constantly re-structure of trees:
  • Keep the trees height balanced so that the height is logarithmic in the size
  • Performance always logarithmic

Issues

.....