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
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))Node. Its methods are stored, but none of them run yet.class Node belongs to it.__init__ is stored in Node. It runs only when it is called on an object.Node is now defined, ready to make objects from.insert(). Its body does not run yet: it runs each time the function is called.contains(). Its body does not run yet: it runs each time the function is called.root is created but does not point to any object yet (null).key is created with the value 8.insert(), with node = null and key = 8. It gets its own names.node is None is true, so the block under it runs.__init__(), with self = {} and key = 8. It gets its own names.key with the value 8 is added to the Node that self points to.left with the value null is added to the Node that self points to.right with the value null is added to the Node that self points to.__init__() has finished, so its names are gone.key changes from 8 to 3.insert(), with node = {key: 8, left: null, right: null} and key = 3. It gets its own names.node is None is false, so the block under it is skipped.insert() #2, with node = null and key = 3. It gets its own names.node is None is true, so the block under it runs.__init__(), with self = {} and key = 3. It gets its own names.key with the value 3 is added to the Node that self points to.left with the value null is added to the Node that self points to.right with the value null is added to the Node that self points to.__init__() has finished, so its names are gone.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.node.insert() has finished, so its names are gone.key changes from 3 to 10.insert(), with node = {key: 8, left: a Node, right: null} and key = 10. It gets its own names.node is None is false, so the block under it is skipped.key < node.key is false, so the block under it is skipped.insert() #2, with node = null and key = 10. It gets its own names.node is None is true, so the block under it runs.__init__(), with self = {} and key = 10. It gets its own names.key with the value 10 is added to the Node that self points to.left with the value null is added to the Node that self points to.right with the value null is added to the Node that self points to.__init__() has finished, so its names are gone.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.node.insert() has finished, so its names are gone.key changes from 10 to 6.insert(), with node = {key: 8, left: a Node, right: a Node} and key = 6. It gets its own names.node is None is false, so the block under it is skipped.key < node.key is true, so the block under it runs.insert() #2, with node = {key: 3, left: null, right: null} and key = 6. It gets its own names.node is None is false, so the block under it is skipped.key < node.key is false, so the block under it is skipped.node is None is true, so the block under it runs.__init__(), with self = {} and key = 6. It gets its own names.key with the value 6 is added to the Node that self points to.left with the value null is added to the Node that self points to.right with the value null is added to the Node that self points to.__init__() has finished, so its names are gone.insert() #3 has finished, so its names are gone. right in the Node that node points to changes from null to a Node.node.insert() #2 has finished, so its names are gone.node.insert() has finished, so its names are gone.for key in [8, 3, 10, 6].contains(), with node = {key: 8, left: a Node, right: a Node} and key = 6. It gets its own names.node is not None is true, so the loop body runs.key == node.key is false, so the block under it is skipped.node is not None is true, so the loop body runs.key == node.key is false, so the block under it is skipped.node now points to a different Node: {key: 6, left: null, right: null}.node is not None is true, so the loop body runs.key == node.key is true, so the block under it runs.contains() has finished, so its names are gone. It prints True.Answer: A chain going right
Answer: Sorted keys
← Tree Traversal · Heaps and Priority Queues →