->left =
node->right =
nullptr;
105std::vector<int64_t> result;
109 while(!
stack.empty()) {
110result.push_back(
stack.
top()->data);
114 if(current->
right) {
117 if(current->
left) {
135std::vector<int64_t> result;
139 while(!
stack.empty()) {
140result.push_back(
stack.
top()->data);
144 if(current->
left) {
147 if(current->
right) {
152reverse(result.begin(), result.end());
167std::vector<int64_t> result;
169 Node*current = root;
171 while(!
stack.empty() || current) {
174current = current->
left;
178result.push_back(current->
data);
179current = current->
right;
183voiddeleteAll(
Node*root) {
185std::stack<Node *>
stack;
188 while(!
stack.empty()) {
192 if(current->
right) {
195 if(current->
left) {
212std::vector<int64_t> actual_result{1, 2, 4, 5, 3};
221 for(
inti = 0; i < result.size(); i++) {
222assert(actual_result[i] == result[i]);
226std::cout <<
"\nPreOrder Traversal Is : "<< std::endl;
227 for(
autoi : result) {
228std::cout << i <<
" ";
239std::vector<int64_t> actual_result{4, 5, 2, 3, 1};
248 for(
inti = 0; i < result.size(); i++) {
249assert(actual_result[i] == result[i]);
253std::cout <<
"\nPostOrder Traversal Is : "<< std::endl;
254 for(
autoi : result) {
255std::cout << i <<
" ";
266std::vector<int64_t> actual_result{4, 2, 5, 1, 3};
275 for(
inti = 0; i < result.size(); i++) {
276assert(actual_result[i] == result[i]);
280std::cout <<
"\nInOrder Traversal Is : "<< std::endl;
281 for(
autoi : result) {
282std::cout << i <<
" ";
293std::vector<int64_t> actual_result{-1, -2, -4, -5, -3};
302 for(
inti = 0; i < result.size(); i++) {
303assert(actual_result[i] == result[i]);
307std::cout <<
"\nPreOrder Traversal Is : "<< std::endl;
308 for(
autoi : result) {
309std::cout << i <<
" ";
321std::vector<int64_t> actual_result{-4, -5, -2, -3, -1};
330 for(
inti = 0; i < result.size(); i++) {
331assert(actual_result[i] == result[i]);
335std::cout <<
"\nPostOrder Traversal Is : "<< std::endl;
336 for(
autoi : result) {
337std::cout << i <<
" ";
348std::vector<int64_t> actual_result{-4, -2, -5, -1, -3};
357 for(
inti = 0; i < result.size(); i++) {
358assert(actual_result[i] == result[i]);
362std::cout <<
"\nInOrder Traversal Is : "<< std::endl;
363 for(
autoi : result) {
364std::cout << i <<
" ";
391std::cout <<
"\n| Tests for positive data value |"<< std::endl;
392 test1(binaryTree, root);
393std::cout <<
"\nPre-order test Passed!"<< std::endl;
395 test2(binaryTree, root);
396std::cout <<
"\nPost-order test Passed!"<< std::endl;
398 test3(binaryTree, root);
399std::cout <<
"\nIn-order test Passed!"<< std::endl;
403root->left->data = -2;
404root->right->data = -3;
405root->left->left->data = -4;
406root->left->right->data = -5;
408std::cout <<
"\n| Tests for negative data values |"<< std::endl;
409 test4(binaryTree, root);
410std::cout <<
"\nPre-order test on-negative value Passed!"<< std::endl;
412 test5(binaryTree, root);
413std::cout <<
"\nPost-order test on-negative value Passed!"<< std::endl;
415 test6(binaryTree, root);
416std::cout <<
"\nIn-order test on-negative value Passed!"<< std::endl;
defines the functions associated with the binary tree
std::vector< int64_t > inOrderIterative(Node *)
inOrderIterative() function that will perform the inorder traversal iteratively, and return the resul...
Node * createNewNode(int64_t)
function that will create new node for insertion.
std::vector< int64_t > postOrderIterative(Node *)
postOrderIterative() function that will perform the postorder traversal iteratively,...
std::vector< int64_t > preOrderIterative(Node *)
preOrderIterative() function that will perform the preorder traversal iteratively,...
for std::invalid_argument
void push(const value_type &item)
static void test2()
Self-implementations, 2nd test.
static void test1()
Self-test implementations, 1st test.
static void test4(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
Test the computed preorder with the actual preorder on negative value.
static void test5(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
Test the computed postorder with the actual postorder on negative value.
int main()
Main function.
static void test6(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
Test the computed inorder with the actual inorder on negative value.
Functions for the Traversal of the Tree algorithm.
defines the structure of a node of the tree
struct Node * left
struct pointer to left subtree.
int64_t data
The value/key of the node.
struct Node * right
struct pointer to right subtree.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4