ru
Feedback
inactive

inactive

Закрытый канал
3 849
Подписчики
Нет данных24 часа
-297 дней
-12730 день
Архив постов
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* left; struct Node* right; }; struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } // Function to insert a node into the binary tree struct Node* insertNode(struct Node* root, int data) { if (root == NULL) { return createNode(data); } if (data < root->data) { root->left = insertNode(root->left, data); } else if (data > root->data) { root->right = insertNode(root->right, data); } return root; } // Function to compare two binary trees for structural and value equality int isIdentical(struct Node* root1, struct Node* root2) { if (root1 == NULL && root2 == NULL) { return 1; // Both trees are empty, so they are identical } if (root1 == NULL || root2 == NULL) { return 0; // One tree is empty while the other is not, so they are not identical } // Check if current node values are equal if (root1->data != root2->data) { return 0; // Values are different, so trees are not identical } // Recursively check left and right subtrees return isIdentical(root1->left, root2->left) && isIdentical(root1->right, root2->right); } int main() { int n1, n2; scanf("%d", &n1); struct Node* root1 = NULL; for (int i = 0; i < n1; i++) { int data; scanf("%d", &data); root1 = insertNode(root1, data); } scanf("%d", &n2); struct Node* root2 = NULL; for (int i = 0; i < n2; i++) { int data; scanf("%d", &data); root2 = insertNode(root2, data); } //awasthi if (isIdentical(root1, root2)) { printf("The two binary trees are identical.\n"); } else { printf("The two binary trees are not identical.\n"); } //awasthi return 0; }

Sara is an aspiring computer scientist working on a binary tree problem. Her task is to compare two binary trees to see if they are identical in structure and values. Can you assist her in solving this challenge? To accomplish this, you need to develop a function that takes the roots of two binary trees as input and performs a preorder traversal on both trees simultaneously. During the traversal, you will compare the values of the corresponding nodes in both trees. If at any point the values differ, the trees are not identical, and the function should return false. However, if the traversal completes without finding any differences, the trees are considered identical, and the function should return true. Input format : The first line of input consists of an integer n1, followed by a list of space-separated integers representing the elements to be inserted into the first binary tree. The third line consists of an integer n2, followed by a list of space-separated integers representing the elements to be inserted into the second binary tree. Output format : If the two binary trees are identical, the output prints, "The two binary trees are identical." Otherwise, print "The two binary trees are not identical."

#include <iostream> using namespace std; struct Node { int key; Node* left; Node* right; Node(int item) { key = item; left = right = nullptr; } }; // Function to insert a node into the BST Node* insert(Node* root, int key) { if (root == nullptr) { return new Node(key); } if (key < root->key) { root->left = insert(root->left, key); } else if (key > root->key) { root->right = insert(root->right, key); } return root; } // Function to perform in-order traversal void inorder(Node* root) { if (root == nullptr) { return; } //awasthi inorder(root->left); cout << root->key << " "; inorder(root->right); } int main() { int n; cin >> n; Node* root = nullptr; for (int i = 0; i < n; i++) { int bookID; cin >> bookID; root = insert(root, bookID); } //awasthi inorder(root); return 0; }

Amla is the librarian at a local library. She needs a tool to manage the library's catalog efficiently. She decides to use a program that can organize the book IDs in ascending order to help her locate books more easily. Write a program that allows Sarah to input the book IDs one by one. The program should then construct a binary search tree with the book IDs and display the book IDs in ascending order using an in-order traversal. Input format : The first line of input consists of an integer N, the number of books in the library. The next line consists of N positive integers separated by space, representing the unique book IDs.

#include <iostream> using namespace std; struct Node { int data; struct Node* left; struct Node* right; }; // Function to create a new node struct Node* createNode(int data) { struct Node* newNode = new Node(); newNode->data = data; newNode->left = newNode->right = NULL; return newNode; } // Function to insert a node into the BST struct Node* insertNode(struct Node* root, int data) { if (root == NULL) return createNode(data); if (data < root->data) root->left = insertNode(root->left, data); else if (data > root->data) root->right = insertNode(root->right, data); return root; } // Function to perform in-order traversal void inorderTraversal(struct Node* root) { if (root == NULL) return; inorderTraversal(root->left); cout << root->data << " "; inorderTraversal(root->right); } //awasthi int main() { int n; cin >> n; struct Node* root = NULL; for (int i = 0; i < n; i++) { int data; cin >> data; root = insertNode(root, data); } inorderTraversal(root); return 0; } //awasthi

Bindu is working on a problem involving tree traversal, particularly the in-order traversal of a tree. She needs to construct a binary search tree (BST) from a set of integers and then traverse it in an in-order manner. Can you help her? Write a program that takes a set of integers as input and constructs a binary search tree (BST) from those integers. After constructing the BST, perform an in-order traversal and display the elements in sorted order. Input format : The first line of input consists of an integer N, representing the number of integers to be inserted into the BST. The second line consists of N space-separated integers, which are the elements to be inserted into the BST.

#include <iostream> // Define the structure for a binary tree node struct Node { int data; Node* left; Node* right; }; // Function to count the number of nodes in the binary tree int nodeCount(Node* root) { if (root == nullptr) { return 0; } return 1 + nodeCount(root->left) + nodeCount(root->right); } // Function to create a new binary tree node Node* createNode(int data) { Node* newNode = new Node(); newNode->data = data; newNode->left = newNode->right = nullptr; return newNode; } // Function to build a binary tree from user input Node* buildBinaryTree() { int data; std::cin >> data; if (data == -1) { return nullptr; // Return nullptr for an empty node } Node* root = createNode(data); root->left = buildBinaryTree(); root->right = buildBinaryTree(); return root; } // Function to check if a binary tree has duplicate values bool hasDuplicateValuesUtil(Node* root, int* prev_values, int* prev_index) { if (root == nullptr) { return false; } // Check if the current node's data matches any previous value. for (int i = 0; i < *prev_index; i++) { if (prev_values[i] == root->data) { return true; } } // Store the current node's data in the array of previous values. prev_values[(*prev_index)++] = root->data; // Recursively check for duplicate values in left and right subtrees. return hasDuplicateValuesUtil(root->left, prev_values, prev_index) || hasDuplicateValuesUtil(root->right, prev_values, prev_index); } bool hasDuplicateValues(Node* root) { int prev_index = 0; int numNodes = nodeCount(root); int* prev_values = new int[numNodes]; // Dynamically allocate memory bool result = hasDuplicateValuesUtil(root, prev_values, &prev_index); delete[] prev_values; // Free dynamically allocated memory return result; } //awasthi int main() { Node* root = nullptr; root = buildBinaryTree(); if (hasDuplicateValues(root)) { std::cout << "Yes" << std::endl; } else { std::cout << "No" << std::endl; } return 0; }

You are working on a critical customer database system for a large e-commerce platform. The database stores customer information, like their unique customer IDs, in a binary tree structure. Ensuring data integrity is paramount, as duplicate customer IDs can lead to serious data inconsistencies and operational issues. Your task is to develop a program that checks whether the binary tree containing customer IDs has any duplicate values. Detecting duplicates is essential to maintain the accuracy of customer records. Input format : The first line of input consists of an integer representing the value of the root node. For each node in the tree, there are two integers, Left child data: an integer representing the value of the left child node. Use -1 to indicate no left child. Right child data: an integer representing the value of the right child node. Use -1 to indicate no right child.

#include <bits/stdc++.h> using namespace std; struct Node { int data; Node* left; Node* right; Node(int data) { this->data = data; this->left = NULL; this->right = NULL; } }; int isBSTUtil(Node* node, int min, int max); int isBST(Node* node) { return (isBSTUtil(node, INT_MIN, INT_MAX)); } int isBSTUtil(Node* node, int min, int max) { if (node == NULL) return 1; if (node->data < min || node->data > max) return 0; return isBSTUtil(node->left, min, node->data - 1) && // Allow only distinct values isBSTUtil(node->right, node->data + 1, max); // Allow only distinct values } Node* buildTree() { int data; cin >> data; if (data == -1) return NULL; Node* root = new Node(data); root->left = buildTree(); root->right = buildTree(); return root; } int main() { Node* root = buildTree(); //awasthi if (isBST(root)) cout << "The given binary tree is a BST" << endl; else cout << "The given binary tree is not a BST" << endl; //awasthi return 0; }

You are tasked with developing a software module for a critical application that involves binary trees. Your objective is to create a program that determines whether a given binary tree adheres to the Binary Search Tree (BST) property. In a Binary Search Tree (BST): Each node in the tree has a unique integer value. For any given node, All nodes in its left subtree have values less than the node's value. All nodes in its right subtree have values greater than the node's value. Your task is to implement a program that can take as input a binary tree and determine whether it is a valid BST according to the rules mentioned above. Input format : The first line of input consists of the root node's value as an integer. For each non-null node, there will be two inputs: the value of the left child (if exists) or -1 if there is no left child, the value of the right child (if exists) or -1 if there is no right child.

#include <stdio.h> #include <stdlib.h> #include <stdbool.h> // BST node struct Node { int data; struct Node* left; struct Node* right; }; // Utility function to create a new Node struct Node* newNode(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } // Function to insert a node into a BST struct Node* insert(struct Node* root, int data) { if (root == NULL) return newNode(data); if (data < root->data) root->left = insert(root->left, data); else if (data > root->data) root->right = insert(root->right, data); return root; } // Function to check if two BSTs are identical bool areIdentical(struct Node* root1, struct Node* root2) { if (root1 == NULL && root2 == NULL) return true; if (root1 != NULL && root2 != NULL) { return (root1->data == root2->data) && areIdentical(root1->left, root2->left) && areIdentical(root1->right, root2->right); } return false; } int main() { struct Node* root1 = NULL; struct Node* root2 = NULL; int data; // Input for City A's vehicle IDs while (1) { scanf("%d", &data); if (data == -1) break; root1 = insert(root1, data); } //awasthi // Input for City B's vehicle IDs while (1) { scanf("%d", &data); if (data == -1) break; root2 = insert(root2, data); } // Check if the vehicle fleets are identical if (areIdentical(root1, root2)) { printf("Both vehicle fleets are identical\n"); } else { printf("Vehicle fleets are not identical\n"); } return 0; }

You are developing software for a car rental agency that manages its vehicle fleet across two locations, City A and City B. Each location's vehicle fleet is represented as a Binary Search Tree (BST) containing unique vehicle IDs. Your program needs to determine whether the vehicle fleets in both cities are identical or not. Input format : The first line of input consists of the space-separated vehicle IDs for City A, terminated by -1. The second line consists of the space-separated vehicle IDs for City B, terminated by -1. Output format : Print "Both vehicle fleets are identical" if the vehicle fleets in both cities are identical. Otherwise, print "Vehicle fleets are not identical"

#include <stdio.h> #include <stdbool.h> #include <limits.h> //awasthi // Function to check if an array represents an inorder traversal of a BST bool isValidInorder(int arr[], int n) { // Initialize the minimum value to negative infinity int min_val = INT_MIN; // Traverse the array for (int i = 0; i < n; i++) { // If the current element is less than or equal to the minimum value, // it's not a valid BST inorder traversal if (arr[i] <= min_val) { return false; } // Update the minimum value to the current element min_val = arr[i]; } // If the loop completes without finding any violations, it's a valid BST inorder traversal return true; } int main() { int n; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } if (isValidInorder(arr, n)) { printf("Yes\n"); } else { printf("No\n"); } return 0; }

Imagine you are working on a coding competition platform where participants are required to solve various algorithmic problems. One of the challenges involves checking whether a given array of integers represents a valid in-order traversal of a Binary Search Tree (BST). Your task is to develop a program that can validate their solutions. Your program should take the input array and determine if it's a valid BST in order traversal. Input format : The first line of input consists of an integer N, representing the number of elements in the array. The second line consists of N space-separated integers, representing the elements of the array.

#include <iostream> using namespace std; void merge(int arr[], int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; int L[n1], R[n2]; for (int i = 0; i < n1; i++) L[i] = arr[left + i]; for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j]; int i = 0, j = 0, k = left; while (i < n1 && (L[i] % 2 == 0)) { arr[k] = L[i]; i++; k++; } while (j < n2 && (R[j] % 2 == 0)) { arr[k] = R[j]; j++; k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } void mergeSort(int arr[], int left, int right) { if (left < right) { int mid = left + (right - left) / 2; mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right); merge(arr, left, mid, right); } } int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } mergeSort(arr, 0, n - 1); for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; //awasthi return 0; }

Alex is a young computer science enthusiast who loves solving coding problems. One day, Alex stumbled upon a unique challenge related to sorting numbers. The challenge involves sorting a list of integers with a unique twist: Alex aims to place the even numbers in their original input order first, followed by the odd numbers in the same input order. Your task is to help Alex implement a logic of merge sort and a recursive function to arrange the even and odd numbers separately. Input format : The first line contains an integer n, the number of integers in the list. The second line contains n space-separated integers, a[i], representing the elements of the list.

#include <stdio.h> void merge(int arr[], int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; int left_arr[n1]; int right_arr[n2]; for (int i = 0; i < n1; i++) { left_arr[i] = arr[left + i]; } for (int i = 0; i < n2; i++) { right_arr[i] = arr[mid + 1 + i]; } int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (left_arr[i] <= right_arr[j]) { arr[k++] = left_arr[i++]; } else { arr[k++] = right_arr[j++]; } } while (i < n1) { arr[k++] = left_arr[i++]; } while (j < n2) { arr[k++] = right_arr[j++]; } } //awasthi void mergeSort(int arr[], int left, int right) { if (left < right) { int mid = left + (right - left) / 2; mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right); merge(arr, left, mid, right); } } int main() { int size; scanf("%d", &size); int array[size]; for (int i = 0; i < size; i++) { scanf("%d", &array[i]); } for (int i = 0; i < size; i++) { printf("%d ", array[i]); } printf("\n"); mergeSort(array, 0, size - 1); for (int i = 0; i < size; i++) { printf("%d ", array[i]); } printf("\n"); return 0; }

You are developing a leaderboard system for an online game. The scores of the players are stored in an array of integers. To display the leaderboard, you need to sort the scores in ascending order. Write code to implement a recursive merge sort algorithm for sorting the scores. Note: This kind of question will help in clearing Wipro recruitment. Input format : The first line of input consists of an integer n, representing the number of scores in the array. The second line of input consists of n space-separated integers, representing the scores of the players. Output format : The first line of output displays the initial array of scores before sorting. The second line of output displays the sorted array of scores after applying the merge sort algorithm.

#include <stdio.h> void merge_descending(char arr[], int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; char left_arr[n1]; char right_arr[n2]; for (int i = 0; i < n1; i++) { left_arr[i] = arr[left + i]; } for (int i = 0; i < n2; i++) { right_arr[i] = arr[mid + 1 + i]; } int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (left_arr[i] >= right_arr[j]) { arr[k] = left_arr[i]; i++; } else { arr[k] = right_arr[j]; j++; } k++; } while (i < n1) { arr[k] = left_arr[i]; i++; k++; } while (j < n2) { arr[k] = right_arr[j]; j++; k++; } } //awasthi void mergeSortDescending(char arr[], int left, int right) { if (left < right) { int mid = left + (right - left) / 2; mergeSortDescending(arr, left, mid); mergeSortDescending(arr, mid + 1, right); merge_descending(arr, left, mid, right); } } int main() { int n; scanf("%d", &n); char arr[n]; for (int i = 0; i < n; i++) { scanf(" %c", &arr[i]); } mergeSortDescending(arr, 0, n - 1); for (int i = 0; i < n; i++) { printf("%c ", arr[i]); } return 0; }

In a mystical land known as Eldoria, ancient wizards use magical runes to cast powerful spells. These runes are represented by single characters, each possessing a unique magical property. However, the wizards have a challenge: they need these magical runes sorted in a specific order for their spells to work correctly. Write a program to help the wizards of Eldoria sort their magical runes based on their potency. Each rune is represented by a single character, and each character holds a unique level of magical power, determined by its position in the ASCII table. Your task is to implement a merge sorting logic and recursive function to arrange these magical runes in descending order of their magical potency. Input format : The first line of input consists of an integer n, representing the number of magical runes to be sorted. The second line contains n space-separated characters, each representing a magical rune. Output format : The output displays a single line containing the magical runes sorted in descending order of their magical potency, separated by spaces.