binary lab no 8
binary lab no 8
Faculty of Engineering
Objectives
A binary tree is a tree in which no node can have more than two children. In a binary
search tree ,for every node, X, in the tree, the values of all the keys in its left sub tree
are smaller than the key value of X, and the values of all the keys in its right sub tree
are larger than the key value of X. The basic operations on a binary search tree take
time proportional to the height of the tree.
In the linked list implementation of binary search trees: Each element is represented
by node with two link fields and a data field. Each connecting line (or edge) in a binary
tree drawing will be represented by a link field. A leaf node has a leftChild and
rightChild link of NULL. Root node will be pointed to by a pointer variable.
Algorithm:
1. Create a structure with an element (Element) and two pointers – Left and Right that
points to the left and right child node respectively in the tree.
2. Create a new node and assign the resultant pointer variable to the root node
pointer T and assign it to NULL.
a. If the value of T is NULL, assign T->Element to X, and the left and right child
pointers to NULL and exit the insertion operation.
b. Otherwise if the element to be inserted is less than the root element T, repeat the
step 3 recursively, with the new value of T as T->Left.
c. Otherwise if the element to be inserted is more than the root element T, repeat the
step 3 recursively, with the new value of T as T->Right.
d. If the element is already present in the tree, do nothing.
b. If the node has no left and right children, then the pointer to that node from the
parent is changed to NULL and the node is freed of its memory.
c. If the node has only one child, then the parent of the node is made to point to the
child of the node and the node is freed.
i. Look at the right subtree of the node (subtree rooted at the right child of the node).
iii. Replace the key of the node to be deleted by the minimum element.
a. If the root node T is initially NULL, then the tree is empty. So return NULL and exit.
b. Take the element X and compare it with the root node. If X is less than the element
found at the root node, then repeat step 5 recursively with the new value of T as T-
>Left.
c. Take the element X and compare it with the root node. If X is more than the element
found at the root node, then repeat step 5 recursively with the new value of T as T-
>Right.
Tasks:
a. Searching
b. Insertion
c. Deletion
Code :
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
};
node->data = data;
node->left = nullptr;
node->right = nullptr;
return node;
if (root == nullptr) {
return newNode(data);
} else {
return root;
if (root == nullptr) {
return false;
if (root->data == data) {
return true;
} else {
root = root->left;
return root;
//
if (root == nullptr) {
return root;
}
if (data < root->data) {
} else {
if (root->left == nullptr) {
delete root;
return temp;
delete root;
return temp;
root->data = temp->data;
return root;
if (root == nullptr) {
return;
inorderTraversal(root->left);
inorderTraversal(root->right);
}
int main() {
inorderTraversal(root);
if (search(root, value)) {
cout << "Value " << value << " found in the BST." << endl;
} else {
cout << "Value " << value << " not found in the BST." << endl;
inorderTraversal(root);
inorderTraversal(root);
inorderTraversal(root);
return 0;
Output:
2 :
Add the code find to minimum and maximum element in the tree.
struct Node {
int data;
Node* left;
Node* right;
};
node->data = data;
node->left = nullptr;
node->right = nullptr;
return node;
if (root == nullptr) {
return newNode(data);
} else {
return root;
if (root == nullptr) {
return false;
if (root->data == data) {
return true;
} else {
}
Node* findMin(Node* root) {
root = root->left;
return root;
root = root->right;
return root;
if (root == nullptr) {
return root;
} else {
if (root->left == nullptr) {
delete root;
return temp;
delete root;
return temp;
root->data = temp->data;
return root;
//
if (root == nullptr) {
return;
inorderTraversal(root->left);
inorderTraversal(root->right);
int main() {
inorderTraversal(root);
if (search(root, value)) {
cout << "Value " << value << " found in the BST." << endl;
} else {
cout << "Value " << value << " not found in the BST." << endl;
cout << "Minimum value in the BST: " << minNode->data << endl;
cout << "Maximum value in the BST: " << maxNode->data << endl;
inorderTraversal(root);
inorderTraversal(root);
inorderTraversal(root);
return 0;
}
Output: