DSA Lab-03
DSA Lab-03
DSA Lab-03
1.Ans:
#include <iostream>
#include <vector>
int main()
{
int n;
cin>>n;
int arr[n];
quicksort(arr,0,n-1);
DATA STRUCTURE AND ALGORITHM LAB ASSESSMENT-03
return 0;
}
1. Q
#include <iostream>
#include <vector>
int main() {
int n;
cin >> n;
vector<int> arr(n);
radixSort(arr);
return 0;
}
3.Q
#include <iostream>
int low = 0;
int high = n - 1;
while (low <= high && key >= arr[low] && key <= arr[high]) {
if (low == high) {
if (arr[low] == key)
return low;
return -1;
if (arr[pos] == key)
return pos;
low = pos + 1;
else
high = pos - 1;
return -1;
int main() {
int n;
cin >> n;
int arr[n+1];
if (position == -1) {
else {
cout << "Element " << key << " found in position " << position+1<< endl;
return 0;
4.Q
#include <iostream>
return(a<b?a:b);
}
DATA STRUCTURE AND ALGORITHM LAB ASSESSMENT-03
if (arr[mid] == key)
return mid;
low = mid + 1;
else
high = mid - 1;
return -1;
if (arr[0] == key)
return 0;
int i = 1;
i *= 2;
return binarySearch(arr, i / 2, std::min(i, n - 1), key); // Use std::min from the <algorithm> header
int main() {
int n;
cin >> n;
int arr[n+1];
int key;
if (position != -1) {
cout << "Element " << key << " found in position " << position << endl;
} else {
return 0;
4.Q
#include <iostream>
return(a<b?a:b);
if (arr[mid] == key)
return mid;
DATA STRUCTURE AND ALGORITHM LAB ASSESSMENT-03
low = mid + 1;
else
high = mid - 1;
return -1;
if (arr[0] == key)
return 0;
int i = 1;
i *= 2;
return binarySearch(arr, i / 2, std::min(i, n - 1), key); // Use std::min from the <algorithm> header
int main() {
int n;
cin >> n;
int arr[n+1];
int key;
DATA STRUCTURE AND ALGORITHM LAB ASSESSMENT-03
if (position != -1) {
cout << "Element " << key << " found in position " << position << endl;
} else {
return 0;
5.q
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
};
if (root == nullptr) {
return root;
root = root->left;
return root;
if (root == nullptr) {
return root;
} else {
if (root->left == nullptr) {
delete root;
return temp;
DATA STRUCTURE AND ALGORITHM LAB ASSESSMENT-03
delete root;
return temp;
root->data = temp->data;
return root;
inOrder(root->left);
inOrder(root->right);
preOrder(root->left);
preOrder(root->right);
postOrder(root->left);
postOrder(root->right);
int main() {
int n;
cin >> n;
int val;
inOrder(root);
preOrder(root);
postOrder(root);
inOrder(root);
inOrder(root);
inOrder(root);
return 0;
6.q
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
};
DATA STRUCTURE AND ALGORITHM LAB ASSESSMENT-03
if (root == nullptr) {
return root;
root = root->left;
return root;
inOrder(root->left);
inOrder(root->right);
int main() {
DATA STRUCTURE AND ALGORITHM LAB ASSESSMENT-03
int n;
cin >> n;
int val;
inOrder(root);
return 0;
7.q
#include <iostream>
struct AVLNode {
int data;
AVLNode* left;
AVLNode* right;
int height;
};
return node->height;
newNode->data = key;
newNode->height = 1;
return newNode;
AVLNode* rightRotate(AVLNode* y) {
AVLNode* x = y->left;
AVLNode* T2 = x->right;
x->right = y;
y->left = T2;
return x;
DATA STRUCTURE AND ALGORITHM LAB ASSESSMENT-03
AVLNode* leftRotate(AVLNode* x) {
AVLNode* y = x->right;
AVLNode* T2 = y->left;
y->left = x;
x->right = T2;
return y;
} else {
return root;
if (balance > 1) {
return rightRotate(root);
root->left = leftRotate(root->left);
return rightRotate(root);
return leftRotate(root);
root->right = rightRotate(root->right);
return leftRotate(root);
return root;
inOrderTraversal(root->left);
inOrderTraversal(root->right);
}
DATA STRUCTURE AND ALGORITHM LAB ASSESSMENT-03
preOrderTraversal(root->left);
preOrderTraversal(root->right);
postOrderTraversal(root->left);
postOrderTraversal(root->right);
int main() {
int n, num;
cin >> n;
preOrderTraversal(root);
inOrderTraversal(root);
postOrderTraversal(root);
return 0;
8.Q
#include <iostream>
struct BTreeNode {
};
newNode->keys[0] = key;
newNode->children[i] = nullptr;
}
DATA STRUCTURE AND ALGORITHM LAB ASSESSMENT-03
newNode->numKeys = 1;
return newNode;
if (root == nullptr) {
return createNode(key);
int i = root->numKeys - 1;
root->keys[i + 1] = root->keys[i];
i--;
root->keys[i + 1] = key;
root->numKeys++;
} else {
int i = root->numKeys - 1;
i--;
if (root->children[i + 1] != nullptr) {
} else {
root->children[i + 1] = createNode(key);
}
DATA STRUCTURE AND ALGORITHM LAB ASSESSMENT-03
return root;
if (root != nullptr) {
traverseBTree(root->children[i]);
int main() {
int n, key;
n = 10;
traverseBTree(root);
return 0;
}
DATA STRUCTURE AND ALGORITHM LAB ASSESSMENT-03