![]() |
TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Testcases to check Union of Two Arrays. More...
Classes | |
class | CircularLinkedList |
A class that implements a Circular Linked List. More... | |
struct | Node |
A Node struct that represents a single Node in a Binary Tree. More... | |
Functions | |
void | test1 () |
A Test to check an simple case. | |
void | test2 () |
A Test to check an empty vector. | |
void | test3 () |
A Test to check an invalid shift value. | |
void | test4 () |
A Test to check a very large input. | |
void | test5 () |
A Test to check a shift of zero. | |
void | test6 () |
A Test to check correct functionality with an array sorted using std::sort. | |
void | print (const std::vector< int32_t > &array) |
Prints the values of a vector sequentially, ending with a newline character. | |
std::vector< int32_t > | shift_left (const std::vector< int32_t > &array, size_t shift) |
Shifts the given vector to the left by the shift amount and returns a new vector with the result. The original vector is not mutated. | |
void | print (const std::vector< int32_t > &array) |
Prints the values of a vector sequentially, ending with a newline character. | |
std::vector< int32_t > | shift_right (const std::vector< int32_t > &array, size_t shift) |
Shifts the given vector to the right by the shift amount and returns a new vector with the result. The original vector is not mutated. | |
std::vector< int32_t > | get_intersection (const std::vector< int32_t > &first, const std::vector< int32_t > &second) |
Gets the intersection of two sorted arrays, and returns them in a vector. | |
void | print (const std::vector< int32_t > &array) |
Prints the values of a vector sequentially, ending with a newline character. | |
std::vector< int32_t > | get_union (const std::vector< int32_t > &first, const std::vector< int32_t > &second) |
Gets the union of two sorted arrays, and returns them in a vector. | |
void | print (const std::vector< int32_t > &array) |
Prints the values of a vector sequentially, ending with a newline character. | |
Testcases to check Union of Two Arrays.
Testcases to check Reversal of Binary Tree.
Testcases to check intersection of Two Arrays.
Testcases to check Circular Linked List.
std::vector< int32_t > operations_on_datastructures::get_intersection | ( | const std::vector< int32_t > & | first, |
const std::vector< int32_t > & | second ) |
Gets the intersection of two sorted arrays, and returns them in a vector.
An algorithm is used that compares the elements of the two vectors, incrementing the index of the smaller of the two. If the elements are the same, the element is appended to the result array to be returned.
first | A std::vector of sorted integer values |
second | A std::vector of sorted integer values |
< Vector to hold the intersection
< Index for the first array
< Index for the second array
< Length of first array
< Length of second array
< Increment index of second array
< Increment index of second array
< Add the element if it is unique
< Increment index of first array
< Increment index of second array too
Definition at line 49 of file intersection_of_two_arrays.cpp.
std::vector< int32_t > operations_on_datastructures::get_union | ( | const std::vector< int32_t > & | first, |
const std::vector< int32_t > & | second ) |
Gets the union of two sorted arrays, and returns them in a vector.
An algorithm is used that compares the elements of the two vectors, appending the one that has a lower value, and incrementing the index for that array. If one of the arrays reaches its end, all the elements of the other are appended to the resultant vector.
first | A std::vector of sorted integer values |
second | A std::vector of sorted integer values |
< Vector to hold the union
< Index for the first array
< Index for the second array
< Length of first array
< Length of second array
< Integer to store value of the next element
< Append from first array
< Increment index of second array
< Append from second array
< Increment index of second array
< Element is the same in both
< Increment index of first array
< Increment index of second array too
< Add the element if it is unique
< Add remaining elements
< Add the element if it is unique
< Add remaining elements
< Add the element if it is unique
Definition at line 49 of file union_of_two_arrays.cpp.
void operations_on_datastructures::print | ( | const std::vector< int32_t > & | array | ) |
Prints the values of a vector sequentially, ending with a newline character.
array | Reference to the array to be printed |
Print each value in the array
Print newline
Print each value in the array
Print newline
Print each value in the array
Print newline
Definition at line 29 of file array_left_rotation.cpp.
void operations_on_datastructures::print | ( | const std::vector< int32_t > & | array | ) |
Prints the values of a vector sequentially, ending with a newline character.
array | Reference to the array to be printed |
Print each value in the array
Print newline
Print each value in the array
Print newline
Print each value in the array
Print newline
Definition at line 29 of file array_left_rotation.cpp.
void operations_on_datastructures::print | ( | const std::vector< int32_t > & | array | ) |
Prints the values of a vector sequentially, ending with a newline character.
array | Reference to the array to be printed |
Print each value in the array
Print newline
Print each value in the array
Print newline
Print each value in the array
Print newline
Definition at line 29 of file array_left_rotation.cpp.
void operations_on_datastructures::print | ( | const std::vector< int32_t > & | array | ) |
Prints the values of a vector sequentially, ending with a newline character.
array | Reference to the array to be printed |
Print each value in the array
Print newline
Print each value in the array
Print newline
Print each value in the array
Print newline
Definition at line 29 of file array_left_rotation.cpp.
std::vector< int32_t > operations_on_datastructures::shift_left | ( | const std::vector< int32_t > & | array, |
size_t | shift ) |
Shifts the given vector to the left by the shift amount and returns a new vector with the result. The original vector is not mutated.
Shifts the values of the vector, by creating a new vector and adding values from the shift index to the end, then appending the rest of the elements from the start of the vector.
array | A reference to the input std::vector |
shift | The amount to be shifted to the left |
< We got an invalid shift, return empty array
< Result array
< Add values after the shift index
< Add the values from the start
Definition at line 46 of file array_left_rotation.cpp.
std::vector< int32_t > operations_on_datastructures::shift_right | ( | const std::vector< int32_t > & | array, |
size_t | shift ) |
Shifts the given vector to the right by the shift amount and returns a new vector with the result. The original vector is not mutated.
Shifts the values of the vector, by creating a new vector and adding values from the shift index to the end, then appending the rest of the elements to the start of the vector.
array | A reference to the input std::vector |
shift | The amount to be shifted to the right |
< We got an invalid shift, return empty array
< Result array
< Add values after the shift index
< Add the values from the start
Definition at line 47 of file array_right_rotation.cpp.
void tests::test1 | ( | ) |
A Test to check an simple case.
< Use the BinaryTree
A Test to check an edge case (two empty arrays)
A Test to check a single value.
A Test to check an edge case (single element reversal)
< Should print 3 4 5 1 2
< Should print 4 5 1 2 3
< Check if result is empty
< Should only print newline
< Check for equal sizes
< Ensure that there is only one element
< Check if both elements are same
< Check if result is empty
< Should only print newline
Definition at line 75 of file array_left_rotation.cpp.
void tests::test2 | ( | ) |
A Test to check an empty vector.
A Test to check an edge case (NULL root element)
A Test to check an edge case (one empty array)
A Test to check a few values.
< Should print empty newline
< Check if result is equal to a
< Should only print newline
< Check for equal sizes
< Ensure that there is only one element
< Check if result is equal to b
< Should print 2 3
Definition at line 90 of file array_left_rotation.cpp.
void tests::test3 | ( | ) |
A Test to check an invalid shift value.
A Test to check correct reversal of a Binary Tree.
A Test to check correct functionality with a simple test case.
A Test to check an input array.
< 7 > 5
< Should print empty newline
< 7 > 5
< Should print empty newline
< Check if result is correct
< Should print 6
< Check for equality
< Check for equality
< Check if result is correct
< Should print 2 3 4 6
Definition at line 105 of file array_left_rotation.cpp.
void tests::test4 | ( | ) |
A Test to check a very large input.
A Test to check correct functionality with duplicate values.
A Test to check using a specific Node as the starting point.
< Should print {4, 6, ..., 420, 2}
< Should print {420, 2, 4, ..., 418}
< Node we will start printing from
< Check if result is correct
< Should print 4 6
< Check if result is correct
< Should print 2 3 4 6 7
Definition at line 120 of file array_left_rotation.cpp.
void tests::test5 | ( | ) |
A Test to check a shift of zero.
A Test to check correct functionality with a harder test case.
A Test to check an empty list.
< Should print 1 2 3 4 5
< Check if result is correct
< Should print 2 3 4
< Check if result is correct
< Should print 1 2 3 4 5 6 7 9
Definition at line 143 of file array_left_rotation.cpp.
void tests::test6 | ( | ) |
A Test to check correct functionality with an array sorted using std::sort.
< Sort vector a
< Sort vector b
< Check if result is correct
< Should print 3 7
< Sort vector a
< Sort vector b
< Check if result is correct
< Should print 1 2 3 4 5 6 7 8 9 11
Definition at line 166 of file intersection_of_two_arrays.cpp.