Binary Search Trees

Insert keys so that smaller ones go left and bigger ones go right, then find a key by walking down one path.

Python · intermediate · 9 min · 75 steps · free

What you'll learn

bst.py

class Node:
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None

def insert(node, key):
    if node is None:
        return Node(key)
    if key < node.key:
        node.left = insert(node.left, key)
    else:
        node.right = insert(node.right, key)
    return node

def contains(node, key):
    while node is not None:
        if key == node.key:
            return True
        node = node.left if key < node.key else node.right
    return False

root = None
for key in [8, 3, 10, 6]:
    root = insert(root, key)
print(contains(root, 6))

Step by step

  1. Python starts building the class Node. Its methods are stored, but none of them run yet.
  2. Python reads the class body: everything indented under class Node belongs to it.
  3. The method __init__ is stored in Node. It runs only when it is called on an object.
  4. The class Node is now defined, ready to make objects from.
  5. Python defines the function insert(). Its body does not run yet: it runs each time the function is called.
  6. Python defines the function contains(). Its body does not run yet: it runs each time the function is called.
  7. root is created but does not point to any object yet (null).
  8. key is created with the value 8.
  9. Python calls insert(), with node = null and key = 8. It gets its own names.
  10. node is None is true, so the block under it runs.
  11. Python calls __init__(), with self = {} and key = 8. It gets its own names.
  12. A new key key with the value 8 is added to the Node that self points to.
  13. A new key left with the value null is added to the Node that self points to.
  14. A new key right with the value null is added to the Node that self points to.
  15. __init__() has finished, so its names are gone.
  16. The first key becomes the root: 8.
  17. key changes from 8 to 3.
  18. Python calls insert(), with node = {key: 8, left: null, right: null} and key = 3. It gets its own names.
  19. node is None is false, so the block under it is skipped.
  20. 3 < 8, so 3 belongs in the left subtree.
  21. Python calls insert() #2, with node = null and key = 3. It gets its own names.
  22. node is None is true, so the block under it runs.
  23. Python calls __init__(), with self = {} and key = 3. It gets its own names.
  24. A new key key with the value 3 is added to the Node that self points to.
  25. A new key left with the value null is added to the Node that self points to.
  26. A new key right with the value null is added to the Node that self points to.
  27. __init__() has finished, so its names are gone.
  28. insert() #2 has finished, so its names are gone. left in the Node that root and node point to changes from null to a Node.
  29. The function returns node.
  30. insert() has finished, so its names are gone.
  31. key changes from 3 to 10.
  32. Python calls insert(), with node = {key: 8, left: a Node, right: null} and key = 10. It gets its own names.
  33. node is None is false, so the block under it is skipped.
  34. key < node.key is false, so the block under it is skipped.
  35. Python calls insert() #2, with node = null and key = 10. It gets its own names.
  36. node is None is true, so the block under it runs.
  37. Python calls __init__(), with self = {} and key = 10. It gets its own names.
  38. A new key key with the value 10 is added to the Node that self points to.
  39. A new key left with the value null is added to the Node that self points to.
  40. A new key right with the value null is added to the Node that self points to.
  41. __init__() has finished, so its names are gone.
  42. insert() #2 has finished, so its names are gone. right in the Node that root and node point to changes from null to a Node.
  43. The function returns node.
  44. insert() has finished, so its names are gone.
  45. key changes from 10 to 6.
  46. Python calls insert(), with node = {key: 8, left: a Node, right: a Node} and key = 6. It gets its own names.
  47. node is None is false, so the block under it is skipped.
  48. key < node.key is true, so the block under it runs.
  49. Python calls insert() #2, with node = {key: 3, left: null, right: null} and key = 6. It gets its own names.
  50. node is None is false, so the block under it is skipped.
  51. key < node.key is false, so the block under it is skipped.
  52. 6 > 3, so 6 goes to the right of 3.
  53. node is None is true, so the block under it runs.
  54. Python calls __init__(), with self = {} and key = 6. It gets its own names.
  55. A new key key with the value 6 is added to the Node that self points to.
  56. A new key left with the value null is added to the Node that self points to.
  57. A new key right with the value null is added to the Node that self points to.
  58. __init__() has finished, so its names are gone.
  59. insert() #3 has finished, so its names are gone. right in the Node that node points to changes from null to a Node.
  60. The function returns node.
  61. insert() #2 has finished, so its names are gone.
  62. The function returns node.
  63. insert() has finished, so its names are gone.
  64. Python moves to the next item of the loop: for key in [8, 3, 10, 6].
  65. Python calls contains(), with node = {key: 8, left: a Node, right: a Node} and key = 6. It gets its own names.
  66. node is not None is true, so the loop body runs.
  67. key == node.key is false, so the block under it is skipped.
  68. Searching for 6: 6 < 8, go left to 3.
  69. node is not None is true, so the loop body runs.
  70. key == node.key is false, so the block under it is skipped.
  71. node now points to a different Node: {key: 6, left: null, right: null}.
  72. node is not None is true, so the loop body runs.
  73. key == node.key is true, so the block under it runs.
  74. Found 6 after visiting only 3 nodes.
  75. contains() has finished, so its names are gone. It prints True.

Common mistakes

Best practices

Key takeaways

Check yourself

Insert 1, 2, 3, 4 in order. The tree looks like?

Answer: A chain going right

In-order traversal of a BST gives?

Answer: Sorted keys

·

More in Trees and Heaps