Learn Before
Code

Binary Search Tree Operations

The most common operations are searching for a node, inserting a new node, and removing a node. All code snippets are provided by Bhavya Jain from GeeksForGeeks. A node may be defined like this in Python:

class Node: def __init__(self, key): self.left = None self.right = None self.val = key
  • Searching for a node: the searching algorithm shown utilizes a recursive technique. The searching starts at the root and compares the key with the root. If it is less than the root, it continues searching to the left of the root. Otherwise, it must be greater than the root, so it continues searching to the right of the root. If the element is ever found, the search algorithm returns true. If it isn't found, it returns false.
# A utility function to search a given key in BST def search(root,key): # Base Cases: root is null or key is present at root if root is None or root.val == key: return root # Key is greater than root's key if root.val < key: return search(root.right,key) # Key is smaller than root's key return search(root.left,key)
  • Inserting a node: inserting, like searching, is recursive. We check if the root is empty, and if it is, we insert the new node as a root. This means that once we hit an available empty slot (the bottom of the tree) we can add our new node there. Otherwise we check if the root is less than the new node's key, and if it is, we go right. Otherwise, the root must be greater than the key, so we go left.
def insert(root, key): if root is None: return Node(key) else: if root.val == key: return root elif root.val < key: root.right = insert(root.right, key) else: root.left = insert(root.left, key) return root
  • Removing a node: removal can be simple or complex depending on where the node to be removed is. If the node to be removed is a leaf (meaning it is at the bottom of the tree), it can simply be deleted. If the node to be removed has one child, the value of that child node will be copied to the parent node and then the child will be deleted. Finally, if the node to be removed has two children, the inorder successor has to be found and its value must be copied to the node, after which the inorder successor can be deleted. An inorder successor is the next node in line to take the place of the node being removed.
# A utility function to do inorder traversal of BST def inorder(root): if root is not None: inorder(root.left) print root.key, inorder(root.right) # Given a non-empty binary # search tree, return the node # with minum key value # found in that tree. Note that the # entire tree does not need to be searched def minValueNode(node): current = node # loop down to find the leftmost leaf while(current.left is not None): current = current.left return current # Given a binary search tree and a key, this function # delete the key and returns the new root def deleteNode(root, key): # Base Case if root is None: return root # If the key to be deleted # is smaller than the root's # key then it lies in left subtree if key < root.key: root.left = deleteNode(root.left, key) # If the kye to be delete # is greater than the root's key # then it lies in right subtree elif(key > root.key): root.right = deleteNode(root.right, key) # If key is same as root's key, then this is the node # to be deleted else: # Node with only one child or no child if root.left is None: temp = root.right root = None return temp elif root.right is None: temp = root.left root = None return temp # Node with two children: # Get the inorder successor # (smallest in the right subtree) temp = minValueNode(root.right) # Copy the inorder successor's # content to this node root.key = temp.key # Delete the inorder successor root.right = deleteNode(root.right, temp.key) return root

0

1

Updated 2021-05-25

Tags

Python Programming Language

Data Science

Related