
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Binary Search Tree Using Linked Lists in C++
A linked list is a linear data structure in which we store a sequence of elements, where each element is called a node that contains data and a pointer (or link) to the next element in the sequence. In this C++ article, we will implement a Binary search tree using a linked list.
Binary Search Tree
A binary search tree is a hierarchical data structure that is constructed by nodes. Each node contains a value and its reference to the left and right child nodes. So the value in the left child node is less than the parent node, and the value in the right child node is greater than the parent node.
Binary search trees (BST) are structured so that values are ordered. The left subtree has values smaller than the root node, while the right subtree contains larger values. This organization allows for efficient searching and operations.
Binary Search Tree Using Linked List in C++
Linked lists provide a flexible and intuitive way to represent a binary search tree. Instead of using pointers to the left and right children, we can use the pointer to the next node in the linked list. We can implement binary search tree using linked list.
Here are the operations to create a BST using a linked list:
- Create Structure: First need to create a structure for the nodes to implement a binary search tree using linked lists.
- Insertion Operation: We create a insert function to insert a value or new node into the binary tree.
- Delete Operation: We create a remove function to remove a node from a BST. We first need to find the node that we want to delete. Once we find the node, we need to handle three cases: deleting a node with no children, deleting a node with one child, and deleting a node with two children.
- Traversal Techniques: Traversal is another important aspect of working with binary search trees. There are three common traversal techniques: in-order, pre-order, and post-order.
Example to Implement Binary Search Tree Using Linked List
Following is the implementation of the Binary Search Tree using a linked list in C++:
#include <iostream> using namespace std; // Define the structure for tree node struct node { node * left, * right; int data; }; node * root = NULL, * p = NULL; // insert values into a binary search tree void insert() { int values[6] = {50, 30, 70, 20, 40, 60}; for (int i = 0; i < 6; i++) { int v = values[i]; if (root == NULL) { // Insert the root node root = new node; root -> data = v; root -> left = NULL; root -> right = NULL; cout << "Inserted root: " << v << "\n"; } else { p = root; // Traverse the tree to find the correct position while (true) { if (v < p -> data) { if (p -> left == NULL) { // Insert to the left p -> left = new node; p = p -> left; p -> data = v; p -> left = NULL; p -> right = NULL; cout << "Inserted " << v << " to the left\n"; break; } else { p = p -> left; } } else if (v > p -> data) { if (p -> right == NULL) { // Insert to the right p -> right = new node; p = p -> right; p -> data = v; p -> left = NULL; p -> right = NULL; cout << "Inserted " << v << " to the right\n"; break; } else { p = p -> right; } } } } } } // Inorder traversal: left -> root -> right void inorder(node * p) { if (p != NULL) { inorder(p -> left); cout << p -> data << " "; inorder(p -> right); } } // Preorder traversal: root -> left -> right void preorder(node * p) { if (p != NULL) { cout << p -> data << " "; preorder(p -> left); preorder(p -> right); } } // Postorder traversal: left -> right -> root void postorder(node * p) { if (p != NULL) { postorder(p -> left); postorder(p -> right); cout << p -> data << " "; } } int main() { insert(); cout << "\nTraversal in inorder:\n"; inorder(root); cout << "\nTraversal in preorder:\n"; preorder(root); cout << "\nTraversal in postorder:\n"; postorder(root); return 0; }
Following is the output of the above code ?
Inserted root: 50 Inserted 30 to the left Inserted 70 to the right Inserted 20 to the left Inserted 40 to the right Inserted 60 to the left Traversal in inorder: 20 30 40 50 60 70 Traversal in preorder: 50 30 20 40 70 60 Traversal in postorder: 20 40 30 60 70 50