MCPcopy
hub / github.com/geekcomputers/Python / insert

Function insert

binary_search_trees/insert_in_bst.py:5–19  ·  view source on GitHub ↗

This function inserts a node with value val into the BST

(root: Optional[Node], val: int)

Source from the content-addressed store, hash-verified

3
4
5def insert(root: Optional[Node], val: int) -> Node:
6 """This function inserts a node with value val into the BST"""
7
8 # If the tree is empty, create a new node
9 if root is None:
10 return Node(val)
11
12 # If the value to be inserted is less than the root value, insert in the left subtree
13 if val < root.data:
14 root.left = insert(root.left, val)
15
16 # If the value to be inserted is greater than the root value, insert in the right subtree
17 else:
18 root.right = insert(root.right, val)
19 return root

Callers 1

mainFunction · 0.90

Calls 1

NodeClass · 0.90

Tested by

no test coverage detected