Professional Documents
Culture Documents
lúc
Tình trạng Đã hoàn thành
Hoàn thành vào Thứ bảy, 28 Tháng mười 2023, 4:23 PM
lúc
Thời gian thực 3 giờ 44 phút
hiện
Điểm 17,00/17,00
Điểm 10,00 của 10,00 (100%)
Câu hỏi 1
Chính xác
Implement static methods Partition and QuickSort in class Sorting to sort an array in ascending order.
#ifndef SORTING_H
#define SORTING_H
#include <sstream>
#include <iostream>
#include <type_traits>
using namespace std;
template <class T>
class Sorting {
private:
static T* Partition(T* start, T* end) ;
public:
static void QuickSort(T* start, T* end) ;
};
#endif /* SORTING_H */
You can read the pseudocode of the algorithm used to in method Partition in the below image.
For example:
Test Result
int array[] = { 3, 5, 7, 10 ,12, 14, 15, 13, 1, 2, 9, 6, 4, 8, 11, 16, Index of pivots: 2 0 0 6 1 0 2 1 0 0 2 1 0 0 0 0 0 0
17, 18, 20, 19 }; 1 0
cout << "Index of pivots: "; Array after sorting: 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Sorting<int>::QuickSort(&array[0], &array[20]); 15 16 17 18 19 20
cout << "\n";
cout << "Array after sorting: ";
for (int i : array) cout << i << " ";
Reset answer
int array[] = { 3, 5, 7, 10 ,12, 14, 15, 13, 1, 2, Index of pivots: 2 0 0 6 1 0 2 Index of pivots: 2 0 0 6 1 0
9, 6, 4, 8, 11, 16, 17, 18, 20, 19 }; 1 0 0 2 1 0 0 0 0 0 0 1 0 2 1 0 0 2 1 0 0 0 0 0 0 1 0
cout << "Index of pivots: "; Array after sorting: 1 2 3 4 5 Array after sorting: 1 2 3 4
Sorting<int>::QuickSort(&array[0], &array[20]); 6 7 8 9 10 11 12 13 14 15 16 5 6 7 8 9 10 11 12 13 14 15
cout << "\n"; 17 18 19 20 16 17 18 19 20
cout << "Array after sorting: ";
for (int i : array) cout << i << " ";
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 2
Chính xác
The best way to sort a singly linked list given the head pointer is probably using merge sort.
Both Merge sort and Insertion sort can be used for linked lists. The slow random-access performance of a linked list makes other algorithms
(such as quick sort) perform poorly, and others (such as heap sort) completely impossible. Since worst case time complexity of Merge Sort is
O(nLogn) and Insertion sort is O(n^2), merge sort is preferred.
Additionally, Merge Sort for linked list only requires a small constant amount of auxiliary storage.
To gain a deeper understanding about Merge sort on linked lists, let's implement mergeLists and mergeSortList function below
Constraints:
struct ListNode {
int val;
ListNode* next;
ListNode(int _val = 0, ListNode* _next = nullptr) : val(_val), next(_next) { }
};
For example:
int size; 9 1 2 3 4 5 6 7 8 9
cin >> size; 9 3 8 2 1 6 7 4 5
int* array = new int[size];
for(int i = 0; i < size; i++) cin >> array[i];
unordered_map<ListNode*, int> nodeAddr;
ListNode* head = init(array, size, nodeAddr);
ListNode* sorted = mergeSortList(head);
try {
printList(sorted, nodeAddr);
}
catch(char const* err) {
cout << err << '\n';
}
freeMem(sorted);
delete[] array;
Reset answer
4
5▼ while (a != nullptr && b != nullptr) {
6▼ if (a->val <= b->val) {
7 *mergedTail = a;
8 mergedTail = &(a->next);
9 a = a->next;
10 ▼ } else {
11 *mergedTail = b;
12 mergedTail = &(b->next);
13 b = b->next;
14 }
15 }
16
17 *mergedTail = (a != nullptr) ? a : b;
18
19 return mergedHead;
20 }
21
22 ▼ ListNode* mergeSortList(ListNode* head) {
23 ▼ if (head == nullptr || head->next == nullptr) {
24 return head;
25 }
26
27 ListNode* slow = head;
28 ListNode* fast = head->next;
29
30 ▼ while (fast != nullptr && fast->next != nullptr) {
31 slow = slow->next;
32 fast = fast->next->next;
33 }
34
35 ListNode* secondHalf = slow->next;
36 slow->next = nullptr;
37
38 ListNode* sortedFirstHalf = mergeSortList(head);
39 ListNode* sortedSecondHalf = mergeSortList(secondHalf);
40
41 return mergeLists(sortedFirstHalf, sortedSecondHalf);
42 }
43
Test Input Expected Got
int size; 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
cin >> size; 9 3 8 2 1 6 7 4 5
int* array = new int[size];
for(int i = 0; i < size; i++) cin >> array[i];
unordered_map<ListNode*, int> nodeAddr;
ListNode* head = init(array, size, nodeAddr);
ListNode* sorted = mergeSortList(head);
try {
printList(sorted, nodeAddr);
}
catch(char const* err) {
cout << err << '\n';
}
freeMem(sorted);
delete[] array;
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 3
Chính xác
Implement static methods merge, InsertionSort and TimSort in class Sorting to sort an array in ascending order.
merge is responsible for merging two sorted subarrays. It takes three pointers: start, middle, and end, representing the left, middle, and right
portions of an array.
InsertionSort is an implementation of the insertion sort algorithm. It takes two pointers, start and end, and sorts the elements in the range
between them in ascending order using the insertion sort technique.
TimSort is an implementation of the TimSort algorithm, a hybrid sorting algorithm that combines insertion sort and merge sort. It takes two
pointers, start and end, and an integer min_size, which determines the minimum size of subarrays to be sorted using insertion sort. The
function first applies insertion sort to small subarrays, prints the intermediate result, and then performs merge operations to combine sorted
subarrays until the entire array is sorted.
#ifndef SORTING_H
#define SORTING_H
#include <sstream>
#include <iostream>
#include <type_traits>
using namespace std;
template <class T>
class Sorting {
private:
static void printArray(T* start, T* end)
{
int size = end - start;
for (int i = 0; i < size - 1; i++)
cout << start[i] << " ";
cout << start[size - 1];
cout << endl;
}
For example:
Test Result
int array[] = { 19, 20, 18, 17 ,12, 13, 14, 15, 1, 2, 9, 6, 4, 7, 11, 16, Insertion Sort: 17 18 19 20 12 13 14 15 1 2 6 9 4 7
10, 8, 5, 3 }; 11 16 3 5 8 10
int min_size = 4; Merge 1: 12 13 14 15 17 18 19 20 1 2 6 9 4 7 11 16 3
Sorting<int>::TimSort(&array[0], &array[20], min_size); 5 8 10
Merge 2: 12 13 14 15 17 18 19 20 1 2 4 6 7 9 11 16 3
5 8 10
Merge 3: 12 13 14 15 17 18 19 20 1 2 4 6 7 9 11 16 3
5 8 10
Merge 4: 1 2 4 6 7 9 11 12 13 14 15 16 17 18 19 20 3
5 8 10
Merge 5: 1 2 4 6 7 9 11 12 13 14 15 16 17 18 19 20 3
5 8 10
Merge 6: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 20
int array[] = { 3, 20, 18, 17 ,12, 13, 14, 15, 1, 2, 9, 6, 4, 7, 11, 16, Insertion Sort: 3 17 18 20 12 13 14 15 1 2 6 9 4 7
10, 8, 5, 19 }; 11 16 5 8 10 19
int min_size = 4; Merge 1: 3 12 13 14 15 17 18 20 1 2 6 9 4 7 11 16 5
Sorting<int>::TimSort(&array[0], &array[20], min_size); 8 10 19
Merge 2: 3 12 13 14 15 17 18 20 1 2 4 6 7 9 11 16 5
8 10 19
Merge 3: 3 12 13 14 15 17 18 20 1 2 4 6 7 9 11 16 5
8 10 19
Merge 4: 1 2 3 4 6 7 9 11 12 13 14 15 16 17 18 20 5
8 10 19
Merge 5: 1 2 3 4 6 7 9 11 12 13 14 15 16 17 18 20 5
8 10 19
Merge 6: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 20
Reset answer
int array[] = { 19, 20, 18, 17 ,12, 13, 14, 15, 1, Insertion Sort: 17 18 19 20 12 Insertion Sort: 17 18 19 20 12
2, 9, 6, 4, 7, 11, 16, 10, 8, 5, 3 }; 13 14 15 1 2 6 9 4 7 11 16 3 5 13 14 15 1 2 6 9 4 7 11 16 3 5
int min_size = 4; 8 10 8 10
Sorting<int>::TimSort(&array[0], &array[20], Merge 1: 12 13 14 15 17 18 19 Merge 1: 12 13 14 15 17 18 19
min_size); 20 1 2 6 9 4 7 11 16 3 5 8 10 20 1 2 6 9 4 7 11 16 3 5 8 10
Merge 2: 12 13 14 15 17 18 19 Merge 2: 12 13 14 15 17 18 19
20 1 2 4 6 7 9 11 16 3 5 8 10 20 1 2 4 6 7 9 11 16 3 5 8 10
Merge 3: 12 13 14 15 17 18 19 Merge 3: 12 13 14 15 17 18 19
20 1 2 4 6 7 9 11 16 3 5 8 10 20 1 2 4 6 7 9 11 16 3 5 8 10
Merge 4: 1 2 4 6 7 9 11 12 13 Merge 4: 1 2 4 6 7 9 11 12 13
14 15 16 17 18 19 20 3 5 8 10 14 15 16 17 18 19 20 3 5 8 10
Merge 5: 1 2 4 6 7 9 11 12 13 Merge 5: 1 2 4 6 7 9 11 12 13
14 15 16 17 18 19 20 3 5 8 10 14 15 16 17 18 19 20 3 5 8 10
Merge 6: 1 2 3 4 5 6 7 8 9 10 Merge 6: 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20 11 12 13 14 15 16 17 18 19 20
int array[] = { 3, 20, 18, 17 ,12, 13, 14, 15, 1, 2, Insertion Sort: 3 17 18 20 12 Insertion Sort: 3 17 18 20 12
9, 6, 4, 7, 11, 16, 10, 8, 5, 19 }; 13 14 15 1 2 6 9 4 7 11 16 5 8 13 14 15 1 2 6 9 4 7 11 16 5 8
int min_size = 4; 10 19 10 19
Sorting<int>::TimSort(&array[0], &array[20], Merge 1: 3 12 13 14 15 17 18 Merge 1: 3 12 13 14 15 17 18
min_size); 20 1 2 6 9 4 7 11 16 5 8 10 19 20 1 2 6 9 4 7 11 16 5 8 10 19
Merge 2: 3 12 13 14 15 17 18 Merge 2: 3 12 13 14 15 17 18
20 1 2 4 6 7 9 11 16 5 8 10 19 20 1 2 4 6 7 9 11 16 5 8 10 19
Merge 3: 3 12 13 14 15 17 18 Merge 3: 3 12 13 14 15 17 18
20 1 2 4 6 7 9 11 16 5 8 10 19 20 1 2 4 6 7 9 11 16 5 8 10 19
Merge 4: 1 2 3 4 6 7 9 11 12 Merge 4: 1 2 3 4 6 7 9 11 12
13 14 15 16 17 18 20 5 8 10 19 13 14 15 16 17 18 20 5 8 10 19
Merge 5: 1 2 3 4 6 7 9 11 12 Merge 5: 1 2 3 4 6 7 9 11 12
13 14 15 16 17 18 20 5 8 10 19 13 14 15 16 17 18 20 5 8 10 19
Merge 6: 1 2 3 4 5 6 7 8 9 10 Merge 6: 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20 11 12 13 14 15 16 17 18 19 20
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 4
Chính xác
For example:
Test Result
int arr[] = {10, 5, 7, 9, 15, 6, 11, 8, 12, 2}; (5, 6), (6, 7), (7, 8), (8, 9), (9, 10), (10, 11), (11, 12)
cout << minDiffPairs(arr, 10);
Reset answer
23
24 for (int i = 2; i < n; i++)
25 if (arr[i] - arr[i - 1] < minDiff)
26 minDiff = arr[i] - arr[i - 1];
27
28 for (int i = 0; i < n - 1; i++)
29 if (arr[i + 1] - arr[i] == minDiff)
30 count++;
31
32 for (int i = 0; i < n - 1; i++)
33 ▼ {
34 if (arr[i + 1] - arr[i] == minDiff)
35 ▼ {
36 count--;
37 result = result + "(" + to_string(arr[i]) + ", " + to_string(arr[i + 1]) + ")" + (count > 0 ? ", "
38
39 }
40 }
41
42 return result;
43
int arr[] = {10, 5, 7, 9, 15, 6, (5, 6), (6, 7), (7, 8), (8, 9), (9, (5, 6), (6, 7), (7, 8), (8, 9), (9,
11, 8, 12, 2}; 10), (10, 11), (11, 12) 10), (10, 11), (11, 12)
cout << minDiffPairs(arr, 10);
int arr[] = {10, -1, -150, 200}; (-1, 10) (-1, 10)
cout << minDiffPairs(arr, 4);
Passed all tests!
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 5
Chính xác
Print the elements of an array in the decreasing frequency order while preserving the relative order of the elements.
For example:
Test Result
sortByFrequency(arr, n);
sortByFrequency(arr, n);
Reset answer
\tsortByFrequency(arr, n);
\tsortByFrequency(arr, n);
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 6
Chính xác
Given a list of points on the 2-D plane (points[] with n elements) and an integer k. Your task in this exercise is to implement
the closestKPoints function to find K closest points to the given point (des_point) and print them by descending order of distances.
Prototype of closestKPoints:
void closestKPoints(Point points[], int n, Point& des_point, int k);
Note: The distance between two points on a plane is the Euclidean distance.
Template:
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
class Point{
public:
int x, y;
Point(int x = 0, int y = 0){
this->x = x;
this->y = y;
}
void display(){
cout << "("<<x<<", "<<y<<")";
}
};
For example:
Test Result
Reset answer
Given a Binary tree, the task is to traverse all the nodes of the tree using Breadth First Search algorithm and print the order of visited nodes (has no
blank space at the end)
#include<iostream>
#include<string>
#include<queue>
using namespace std;
private:
Node *root;
public:
BinaryTree() : root(nullptr) {}
~BinaryTree()
{
// You have to delete all Nodes in BinaryTree. However in this task, you can ignore it.
}
class Node
{
private:
K key;
V value;
Node *pLeft, *pRight;
friend class BinaryTree<K, V>;
public:
Node(K key, V value) : key(key), value(value), pLeft(NULL), pRight(NULL) {}
~Node() {}
};
Test Result
Reset answer
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 8
Chính xác
Class BTNode is used to store a node in binary tree, described on the following:
class BTNode {
public:
int val;
BTNode *left;
BTNode *right;
BTNode() {
this->left = this->right = NULL;
}
BTNode(int val) {
this->val = val;
this->left = this->right = NULL;
}
BTNode(int val, BTNode*& left, BTNode*& right) {
this->val = val;
this->left = left;
this->right = right;
}
};
Where val is the value of node (non-negative integer), left and right are the pointers to the left node and right node of it,
respectively.
Example:
The longest path from the root node to the leaf node is 1-4-7-4-2, so return the sum of this path, is 18.
Explanation of function createTree: The function has three parameters. The first two parameters take in an array containing the
parent of each Node of the binary tree, and the third parameter takes in an array representing the respective values of the Nodes.
After processing, the function will construct the binary tree and return the address of the root Node. Note that the root Node is
designated with a parent value of -1.
Example:
int arr[] = {-1,0,0,2,2};
int value[] = {3,5,2,1,4};
BTNode* root = BTNode::createTree(arr, arr + sizeof(arr)/sizeof(int), value);
arr[0]=-1 means the Node containing the value value[0]=3 will be the root Node. Also, since arr[1]=arr[2]=0, it implies that the
Nodes containing the values value[1]=5 and value[2]=2 will have the Node containing the value value[0]=3 as their parent. Lastly,
since arr[3]=arr[4]=2, it means the Nodes containing the values value[3]=1 and value[4]=4 will have the Node with the
value value[2]=2 as their parent. Final tree of this example are shown in the figure above.
Note that whichever Node appears first in the arr sequence will be the left Node, and the TestCase always ensures that the
resulting tree is a binary tree.
Note: In this exercise, the libraries iostream, utility, queue, stack and using namespace std are used. You can write helper
functions; however, you are not allowed to use other libraries.
For example:
Test Result
Reset answer
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 9
Chính xác
Class BTNode is used to store a node in binary tree, described on the following:
class BTNode {
public:
int val;
BTNode *left;
BTNode *right;
BTNode() {
this->left = this->right = NULL;
}
BTNode(int val) {
this->val = val;
this->left = this->right = NULL;
}
BTNode(int val, BTNode*& left, BTNode*& right) {
this->val = val;
this->left = left;
this->right = right;
}
};
Where val is the value of node (non-negative integer), left and right are the pointers to the left node and right node of it,
respectively.
More information:
- A node is called as the lowest ancestor node of node a and node b if node a and node b are its descendants.
- On the given binary tree, each node's val is distinguish from the others' val
Example:
Explanation of function createTree: The function has three parameters. The first two parameters take in an array containing
the parent of each Node of the binary tree, and the third parameter takes in an array representing the respective values of the
Nodes. After processing, the function will construct the binary tree and return the address of the root Node. Note that the root
Node is designated with a parent value of -1.
Example:
int arr[] = {-1,0,0,2,2};
int value[] = {3,5,2,1,4};
BTNode* root = BTNode::createTree(arr, arr + sizeof(arr)/sizeof(int), value);
arr[0]=-1 means the Node containing the value value[0]=3 will be the root Node. Also, since arr[1]=arr[2]=0, it implies that
the Nodes containing the values value[1]=5 and value[2]=2 will have the Node containing the value value[0]=3 as their parent.
Lastly, since arr[3]=arr[4]=2, it means the Nodes containing the values value[3]=1 and value[4]=4 will have the Node with the
value value[2]=2 as their parent. Final tree of this example are shown in the figure above.
Note that whichever Node appears first in the arr sequence will be the left Node, and the TestCase always ensures that the
resulting tree is a binary tree.
Note: In this exercise, the libraries iostream, stack, queue, utility and using namespace std are used. You can write helper
functions; however, you are not allowed to use other libraries.
For example:
Test Result
Reset answer
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 10
Chính xác
Class BTNode is used to store a node in binary tree, described on the following:
class BTNode {
public:
int val;
BTNode *left;
BTNode *right;
BTNode() {
this->left = this->right = NULL;
}
BTNode(int val) {
this->val = val;
this->left = this->right = NULL;
}
BTNode(int val, BTNode*& left, BTNode*& right) {
this->val = val;
this->left = left;
this->right = right;
}
};
Where val is the value of node (integer, in segment [0,9]), left and right are the pointers to the left node and right node of it,
respectively.
More information:
- A path is called as digit path if it is a path from the root node to the leaf node of the binary tree.
- Each digit path represents a number in order, each node's val of this path is a digit of this number, while root's val is
the first digit.
Example:
All of the digit paths are 3-5, 3-2-1, 3-2-4; and the number reprensted by them are 35, 321, 324, respectively. The sum of
them (after mod 27022001) is 680.
Explanation of function createTree: The function has three parameters. The first two parameters take in an array containing
the parent of each Node of the binary tree, and the third parameter takes in an array representing the respective values of the
Nodes. After processing, the function will construct the binary tree and return the address of the root Node. Note that the root
Node is designated with a parent value of -1.
Example:
int arr[] = {-1,0,0,2,2};
int value[] = {3,5,2,1,4};
BTNode* root = BTNode::createTree(arr, arr + sizeof(arr)/sizeof(int), value);
arr[0]=-1 means the Node containing the value value[0]=3 will be the root Node. Also, since arr[1]=arr[2]=0, it implies that the Nodes
containing the values value[1]=5 and value[2]=2 will have the Node containing the value value[0]=3 as their parent. Lastly, since
arr[3]=arr[4]=2, it means the Nodes containing the values value[3]=1 and value[4]=4 will have the Node with the value value[2]=2 as
their parent. Final tree of this example are shown in the figure above.
Note that whichever Node appears first in the arr sequence will be the left Node, and the TestCase always ensures that the resulting tree
is a binary tree.
Note: In this exercise, the libraries iostream, queue, stack, utility and using namespace std are used. You can write helper
functions; however, you are not allowed to use other libraries.
For example:
Test Result
Reset answer
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 11
Chính xác
Given class BinaryTree, you need to finish methods getHeight(), preOrder(), inOrder(), postOrder().
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
Test Result
Reset answer
53 ▼ string preOrder() {
54 // TODO: return the sequence of values of nodes in pre-order.
55 stringstream ss;
56 preOrder(root, ss);
57 return ss.str();
58 }
59
60 ▼ string inOrder() {
61 // TODO: return the sequence of values of nodes in in-order.
62 stringstream ss;
63 inOrder(root, ss);
64 return ss.str();
65 }
66
67 ▼ string postOrder() {
68 // TODO: return the sequence of values of nodes in post-order.
69 stringstream ss;
70 postOrder(root, ss);
71 return ss.str();
72 }
73
74 // STUDENT ANSWER END
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 12
Chính xác
In this question, you have to perform add and delete on binary search tree. Note that:
- When deleting a node which still have 2 children, take the inorder successor (smallest node of the right sub tree of that node) to replace it.
- When adding a node which has the same value as parent node, add it in the left sub tree.
Your task is to implement two functions: add and deleteNode. You could define one or more functions to achieve this task.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define SEPARATOR "#<ab@17943918#@>#"
template<class T>
class BinarySearchTree
{
public:
class Node;
private:
Node* root;
public:
BinarySearchTree() : root(nullptr) {}
~BinarySearchTree()
{
// You have to delete all Nodes in BinaryTree. However in this task, you can ignore it.
}
//Helping function
string inOrder(){
return inOrderRec(this->root);
}
class Node
{
private:
T value;
Node* pLeft, * pRight;
friend class BinarySearchTree<T>;
public:
Node(T value) : value(value), pLeft(NULL), pRight(NULL) {}
~Node() {}
};
};
For example:
Test Result
BinarySearchTree<int> bst; 2 10
bst.add(9);
bst.add(2);
bst.add(10);
bst.deleteNode(9);
cout << bst.inOrder();
BinarySearchTree<int> bst; 2 8 9 10
bst.add(9); 2 8 10 11
bst.add(2);
bst.add(10);
bst.add(8);
cout << bst.inOrder()<<endl;
bst.add(11);
bst.deleteNode(9);
cout << bst.inOrder();
Reset answer
1 //Helping functions
2 ▼ Node* addHelper(Node* node, T value) {
3 ▼ if (node == nullptr) {
4 return new Node(value);
5 }
6
7 ▼ if (value < node->value) {
8 node->pLeft = addHelper(node->pLeft, value);
9 ▼ } else {
10 node->pRight = addHelper(node->pRight, value);
11 }
12
13 return node;
14 }
15
16 ▼ Node* getInorderSuccessor(Node* node) {
17 Node* current = node;
18 ▼ while (current && current->pLeft != nullptr) {
19 current = current->pLeft;
20 }
21 return current;
22 }
23
24 ▼ Node* deleteHelper(Node* node, T value) {
25 ▼ if (node == nullptr) {
26 return node;
27 }
28
29 ▼ if (value < node->value) {
30 node->pLeft = deleteHelper(node->pLeft, value);
31 ▼ } else if (value > node->value) {
32 node->pRight = deleteHelper(node->pRight, value);
33 ▼ } else {
34
35 ▼ if (node->pLeft == nullptr) {
36 Node* temp = node->pRight;
37 delete node;
38 return temp;
39 ▼ } else if (node->pRight == nullptr) {
40 Node* temp = node->pLeft;
41 delete node;
42 return temp;
43 }
44
45 Node* successor = getInorderSuccessor(node->pRight);
46 node->value = successor->value;
47 node->pRight = deleteHelper(node->pRight, successor->value);
48 }
49
50 return node;
51 }
52 ▼ void add(T value){
53 root = addHelper(root, value);
54 }
55
56 ▼ void deleteNode(T value){
57 root = deleteHelper(root, value);
58
59 }
BinarySearchTree<int> bst; 2 10 2 10
bst.add(9);
bst.add(2);
bst.add(10);
bst.deleteNode(9);
cout << bst.inOrder();
BinarySearchTree<int> bst; 2 8 9 10 2 8 9 10
bst.add(9); 2 8 10 11 2 8 10 11
bst.add(2);
bst.add(10);
bst.add(8);
cout << bst.inOrder()<<endl;
bst.add(11);
bst.deleteNode(9);
cout << bst.inOrder();
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 13
Chính xác
Class BSTNode is used to store a node in binary search tree, described on the following:
class BSTNode {
public:
int val;
BSTNode *left;
BSTNode *right;
BSTNode() {
this->left = this->right = nullptr;
}
BSTNode(int val) {
this->val = val;
this->left = this->right = nullptr;
}
BSTNode(int val, BSTNode*& left, BSTNode*& right) {
this->val = val;
this->left = left;
this->right = right;
}
};
Where val is the value of node, left and right are the pointers to the left node and right node of it, respectively. If a repeated value is
inserted to the tree, it will be inserted to the left subtree.
Also, a static method named createBSTree is used to create the binary search tree, by iterating the argument array left-to-right and repeatedly
calling addNode method on the root node to insert the value into the correct position. For example:
int arr[] = {0, 10, 20, 30};
auto root = BSTNode::createBSTree(arr, arr + 4);
is equivalent to
auto root = new BSTNode(0);
root->addNode(10);
root->addNode(20);
root->addNode(30);
In the first level, we should traverse from left to right (order: 3) and in the second level, we traverse from right to left (order: 4, 0). After
traversing all the nodes, the result should be [3, 4, 0, 2, 1].
Note: In this exercise, the libraries iostream, vector, stack, queue, algorithm and using namespace std are used. You can write helper functions;
however, you are not allowed to use other libraries.
For example:
Test Result
Reset answer
14
15 ▼ for (int i = 0; i < levelSize; i++) {
16 BSTNode* node = q.front();
17 q.pop();
18
19 int index = leftToRight ? i : levelSize - i - 1;
20 levelValues[index] = node->val;
21
22 ▼ if (node->left != nullptr) {
23 q.push(node->left);
24 }
25 ▼ if (node->right != nullptr) {
26 q.push(node->right);
27 }
28 }
29
30 result.insert(result.end(), levelValues.begin(), levelValues.end());
31 leftToRight = !leftToRight;
32 }
33
34 return result;
35 }
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 14
Chính xác
Class BSTNode is used to store a node in binary search tree, described on the following:
class BSTNode {
public:
int val;
BSTNode *left;
BSTNode *right;
BSTNode() {
this->left = this->right = nullptr;
}
BSTNode(int val) {
this->val = val;
this->left = this->right = nullptr;
}
BSTNode(int val, BSTNode*& left, BSTNode*& right) {
this->val = val;
this->left = left;
this->right = right;
}
};
Where val is the value of node, left and right are the pointers to the left node and right node of it, respectively. If a repeated value is
inserted to the tree, it will be inserted to the left subtree.
Also, a static method named createBSTree is used to create the binary search tree, by iterating the argument array left-to-right and repeatedly
calling addNode method on the root node to insert the value into the correct position. For example:
int arr[] = {0, 10, 20, 30};
auto root = BSTNode::createBSTree(arr, arr + 4);
is equivalent to
auto root = new BSTNode(0);
root->addNode(10);
root->addNode(20);
root->addNode(30);
Example:
Given a binary search tree in the following:
With k = 2, the result should be 1.
Note: In this exercise, the libraries iostream, vector, stack, queue, algorithm, climits and using namespace std are used. You can write helper
functions; however, you are not allowed to use other libraries.
For example:
Test Result
Reset answer
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 15
Chính xác
Class BTNode is used to store a node in binary search tree, described on the following:
class BTNode {
public:
int val;
BTNode *left;
BTNode *right;
BTNode() {
this->left = this->right = NULL;
}
BTNode(int val) {
this->val = val;
this->left = this->right = NULL;
}
BTNode(int val, BTNode*& left, BTNode*& right) {
this->val = val;
this->left = left;
this->right = right;
}
};
Where val is the value of node (non-negative integer), left and right are the pointers to the left node and right node of it,
respectively.
Also, a static method named createBSTree is used to create the binary search tree, by iterating the argument array left-to-right
and repeatedly calling addNode method on the root node to insert the value into the correct position. For example:
int arr[] = {0, 10, 20, 30};
auto root = BSTNode::createBSTree(arr, arr + 4);
is equivalent to
auto root = new BSTNode(0);
root->addNode(10);
root->addNode(20);
root->addNode(30);
More information:
- If a node has val which is equal to its ancestor's, it is in the right subtree of its ancestor.
Example:
Note: In this exercise, the libraries iostream, stack, queue, utility and using namespace std are used. You can write helper
functions; however, you are not allowed to use other libraries.
For example:
Test Result
Reset answer
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 16
Chính xác
Class BSTNode is used to store a node in binary search tree, described on the following:
class BSTNode {
public:
int val;
BSTNode *left;
BSTNode *right;
BSTNode() {
this->left = this->right = nullptr;
}
BSTNode(int val) {
this->val = val;
this->left = this->right = nullptr;
}
BSTNode(int val, BSTNode*& left, BSTNode*& right) {
this->val = val;
this->left = left;
this->right = right;
}
};
Where val is the value of node, left and right are the pointers to the left node and right node of it, respectively. If a repeated value is
inserted to the tree, it will be inserted to the left subtree.
Also, a static method named createBSTree is used to create the binary search tree, by iterating the argument array left-to-right and repeatedly
calling addNode method on the root node to insert the value into the correct position. For example:
int arr[] = {0, 10, 20, 30};
auto root = BSTNode::createBSTree(arr, arr + 4);
is equivalent to
auto root = new BSTNode(0);
root->addNode(10);
root->addNode(20);
root->addNode(30);
- A node is called a single child if its parent has only one child.
Example:
Note: In this exercise, the libraries iostream and using namespace std are used. You can write helper functions; however, you are not allowed to
use other libraries.
For example:
Test Result
Reset answer
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 17
Chính xác
Class BSTNode is used to store a node in binary search tree, described on the following:
class BSTNode {
public:
int val;
BSTNode *left;
BSTNode *right;
BSTNode() {
this->left = this->right = nullptr;
}
BSTNode(int val) {
this->val = val;
this->left = this->right = nullptr;
}
BSTNode(int val, BSTNode*& left, BSTNode*& right) {
this->val = val;
this->left = left;
this->right = right;
}
};
Where val is the value of node, left and right are the pointers to the left node and right node of it, respectively. If a repeated value is
inserted to the tree, it will be inserted to the left subtree.
Also, a static method named createBSTree is used to create the binary search tree, by iterating the argument array left-to-right and repeatedly
calling addNode method on the root node to insert the value into the correct position. For example:
int arr[] = {0, 10, 20, 30};
auto root = BSTNode::createBSTree(arr, arr + 4);
is equivalent to
auto root = new BSTNode(0);
root->addNode(10);
root->addNode(20);
root->addNode(30);
Example:
Given a binary search tree in the following:
Test Result
Reset answer
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
BÁCH KHOA E-LEARNING
WEBSITE
HCMUT
MyBK
BKSI
LIÊN HỆ
elearning@hcmut.edu.vn