How do you implement a binary search tree in multiple languages?

A Binary Search Tree is a binary tree where each node contains a key and an optional associated value.

It allows particularly fast lookup, addition, and removal of items.

svg viewer

Insertion of a Key

A new key in Binary Search Tree is always inserted at a leaf node. We start searching for the key to be inserted from the root until we hit a leaf node. Once the appropriate leaf node is found, the new node is added as a child of that leaf node.

svg viewer

The Node Class

To implement a BST, the first thing you’d need is a node. A node should have a key, a left child, a right child, and constructors. This node can be implemented as a class and here is what it would like in code.

class Node {
public:
int key; //value of the Node
Node* left; // leftChild (will also be of the Node class)
Node* right; // rightChild (will also be of the Node class)
Node(int item) // Constructor to initialize a node with given value
{
key = item;
left = NULL;
right = NULL;
}
};

The BinarySearchTree Class

Let’s create a tree class:

class BinarySearchTree {
Node* root; // The root node
public:
BinarySearchTree(int rootValue) { // constructor to initialize Tree and the root node
root = new Node(rootValue);
}
Node* getRoot() { // Getter - gets root node
return root;
}
};

Implementation of BST

We’ll be implementing Insert and In-order Traversal (Recursive) Functions.

Let’s put node and binary search tree classes together to implement a complete BST.

// BST Node
class Node {
public:
int key;
Node* left;
Node* right;
// Default Constructor
Node() {
key = 0;
left = NULL;
right = NULL;
}
// Parametrized Constructor
Node(int val) {
key = val;
left = NULL;
right = NULL;
}
};
// BST Class
class BinarySearchTree {
private:
Node * root;
public:
BinarySearchTree(int rootValue) {
root=new Node(rootValue);
}
Node* getRoot() {
return root;
}
// A utility function to do inorder traversal of BST
void inorder(Node *root) {
if (root != NULL) {
inorder(root->left);
cout << root->key << endl;
inorder(root->right);
}
}
/* A utility function to insert a new node with given key in BST */
Node* insert(Node* node, int key) {
/* If the tree is empty, return a new node */
if (node == NULL) return new Node(key);
/* Otherwise, recur down the tree */
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
/* return the (unchanged) node pointer */
return node;
}
};
// Driver Program to test above functions
int main()
{
// Let's create the above Binary Search Tree
BinarySearchTree BST(6);
BST.insert(BST.getRoot(), 3);
BST.insert(BST.getRoot(), 9);
BST.insert(BST.getRoot(), 1);
BST.insert(BST.getRoot(), 5);
BST.insert(BST.getRoot(), 7);
BST.insert(BST.getRoot(), 11);
// print inoder traversal of the BST
BST.inorder(BST.getRoot());
return 0;
}

It completes the BST insertion and In-Order traversal methods using recursion.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved