quartz/content/notes/binary-search-tree.md
Jet Hughes 8a667e5693 update
2022-05-27 14:12:53 +12:00

1.1 KiB

title aliases tags
binary-search-tree binary search tree, BST
cosc201
datastructure

code

bst-operations

a collection of nodes with one distinguished node called the root

rules:

  • the node data contains a key which comes from some ordered type e.g., string
  • each node can have at most who children - there are two fixed slots called left child and right child
  • the left child node and it descendents are called the left subtree
  • the key value at a nodes left child (and all its descendants) must be less than the key of the node
  • the key value at a nodes right child (and all its descendants) must be greater than the key of the node
  • we do not allow duplicate keys

Notation

  • n.key for key
  • n.l and n.r for left and right children
  • n.p for its parent
  • nil for a "not here" marker. e.g., n.r = nil means "n has no right child"

e.g., 300

In this example:

  • root.p = nil
  • root.r = n
  • n.p = root
  • n.key = emu
  • n.l = nil
  • n.r.key = rat