0% found this document useful (0 votes)
1 views

dsa5

The document contains a C++ implementation of a Binary Search Tree (BST) with functionalities to insert nodes, calculate the height, find the minimum value, swap children of nodes, and perform level-order traversal. It also includes a function to parse input for node values and a search function to check for the presence of a value in the tree. The main function allows user interaction to build the tree and display its properties.

Uploaded by

abhinavsargar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

dsa5

The document contains a C++ implementation of a Binary Search Tree (BST) with functionalities to insert nodes, calculate the height, find the minimum value, swap children of nodes, and perform level-order traversal. It also includes a function to parse input for node values and a search function to check for the presence of a value in the tree. The main function allows user interaction to build the tree and display its properties.

Uploaded by

abhinavsargar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <iostream>

#include <queue>

using namespace std;

struct Node {

int data;

Node* left;

Node* right;

Node(int value) {

data = value;

left = NULL;

right = NULL;

};

struct ListNode {

int data;

ListNode* next;

ListNode(int value) {

data = value;

next = NULL;

};

Node* insert(Node* root, int value) {

if (!root) {

return new Node(value);

if (value < root->data) {

root->left = insert(root->left, value);

} else {
root->right = insert(root->right, value);

return root;

int height(Node* root) {

if (!root) {

return 0;

return 1 + max(height(root->left), height(root->right));

int findMin(Node* root) {

while (root->left) {

root = root->left;

return root->data;

void swapChildren(Node* root) {

if (!root) {

return;

swap(root->left, root->right);

swapChildren(root->left);

swapChildren(root->right);

void levelOrder(Node* root) {

if (!root) {

return;

}
queue<Node*> q;

q.push(root);

int level = 0;

while (!q.empty()) {

int levelSize = q.size();

cout << "Level " << level << ": ";

for (int i = 0; i < levelSize; ++i) {

Node* current = q.front();

q.pop();

cout << current->data << " ";

if (current->left) {

q.push(current->left);

if (current->right) {

q.push(current->right);

cout << endl;

++level;

ListNode* parseInput(string input) {

ListNode* head = NULL;

ListNode* tail = NULL;

int currentValue = 0;

bool inNumber = false;

for (int i = 0; i < input.length(); ++i) {

char c = input[i];
if (isdigit(c)) {

if (!inNumber) {

inNumber = true;

currentValue = c - '0';

} else {

currentValue = currentValue * 10 + (c - '0');

} else if (inNumber) {

ListNode* newNode = new ListNode(currentValue);

if (head == NULL) {

head = newNode;

tail = head;

} else {

tail->next = newNode;

tail = newNode;

inNumber = false;

if (inNumber) {

ListNode* newNode = new ListNode(currentValue);

if (head == NULL) {

head = newNode;

tail = head;

} else {

tail->next = newNode;

}
return head;

void storeInArray(Node* root, int* nodes, int* nodeCount) {

if (!root) {

return;

nodes[*nodeCount] = root->data;

(*nodeCount)++;

storeInArray(root->left, nodes, nodeCount);

storeInArray(root->right, nodes, nodeCount);

bool Search(int* nodes, int nodeCount, int value) {

for (int i = 0; i < nodeCount; ++i) {

if (nodes[i] == value) {

return true;

return false;

int main() {

Node* root = NULL;

string input;

ListNode* nodeList = NULL;

char continueInput;

cout << "Binary Search Tree is initially empty." << endl;

do {

cout << "Enter node values for the BST (separate by spaces or commas): ";

getline(cin, input);
ListNode* newNodes = parseInput(input);

if (nodeList == NULL) {

nodeList = newNodes;

} else {

ListNode* temp = nodeList;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNodes;

while (nodeList != NULL) {

root = insert(root, nodeList->data);

nodeList = nodeList->next;

cout << "Do you want to continue entering nodes? (y/n): ";

cin >> continueInput;

cin.ignore();

} while (continueInput == 'y' || continueInput == 'Y');

cout << "Level-order traversal of the constructed tree:" << endl;

levelOrder(root);

cout << "Height of the tree: " << height(root) << endl;

cout << "Minimum value in the tree: " << findMin(root) << endl;

swapChildren(root);

cout << "Level-order traversal after swapping left and right subtrees:" << endl;

levelOrder(root);

int nodes[100], nodeCount = 0;

storeInArray(root, nodes, &nodeCount);

int searchValue;
cout << "Enter a value to search for: ";

cin >> searchValue;

if (Search(nodes, nodeCount, searchValue)) {

cout << "Value " << searchValue << " found in the tree." << endl;

} else {

cout << "Value " << searchValue << " not found in the tree." << endl;

return 0;

OUTPUT:

You might also like