105 std::vector<int64_t> result;
109 while (!
stack.empty()) {
110 result.push_back(
stack.
top()->data);
114 if (current->
right) {
135 std::vector<int64_t> result;
139 while (!
stack.empty()) {
140 result.push_back(
stack.
top()->data);
147 if (current->
right) {
152 reverse(result.begin(), result.end());
167 std::vector<int64_t> result;
169 Node *current = root;
171 while (!
stack.empty() || current) {
174 current = current->
left;
178 result.push_back(current->
data);
179 current = current->
right;
183void deleteAll(
Node *root) {
185 std::stack<Node *>
stack;
188 while (!
stack.empty()) {
192 if (current->
right) {
212 std::vector<int64_t> actual_result{1, 2, 4, 5, 3};
221 for (
int i = 0; i < result.size(); i++) {
222 assert(actual_result[i] == result[i]);
226 std::cout <<
"\nPreOrder Traversal Is : " << std::endl;
227 for (
auto i : result) {
228 std::cout << i <<
" ";
239 std::vector<int64_t> actual_result{4, 5, 2, 3, 1};
248 for (
int i = 0; i < result.size(); i++) {
249 assert(actual_result[i] == result[i]);
253 std::cout <<
"\nPostOrder Traversal Is : " << std::endl;
254 for (
auto i : result) {
255 std::cout << i <<
" ";
266 std::vector<int64_t> actual_result{4, 2, 5, 1, 3};
275 for (
int i = 0; i < result.size(); i++) {
276 assert(actual_result[i] == result[i]);
280 std::cout <<
"\nInOrder Traversal Is : " << std::endl;
281 for (
auto i : result) {
282 std::cout << i <<
" ";
293 std::vector<int64_t> actual_result{-1, -2, -4, -5, -3};
302 for (
int i = 0; i < result.size(); i++) {
303 assert(actual_result[i] == result[i]);
307 std::cout <<
"\nPreOrder Traversal Is : " << std::endl;
308 for (
auto i : result) {
309 std::cout << i <<
" ";
321 std::vector<int64_t> actual_result{-4, -5, -2, -3, -1};
330 for (
int i = 0; i < result.size(); i++) {
331 assert(actual_result[i] == result[i]);
335 std::cout <<
"\nPostOrder Traversal Is : " << std::endl;
336 for (
auto i : result) {
337 std::cout << i <<
" ";
348 std::vector<int64_t> actual_result{-4, -2, -5, -1, -3};
357 for (
int i = 0; i < result.size(); i++) {
358 assert(actual_result[i] == result[i]);
362 std::cout <<
"\nInOrder Traversal Is : " << std::endl;
363 for (
auto i : result) {
364 std::cout << i <<
" ";
391 std::cout <<
"\n| Tests for positive data value |" << std::endl;
392 test1(binaryTree, root);
393 std::cout <<
"\nPre-order test Passed!" << std::endl;
395 test2(binaryTree, root);
396 std::cout <<
"\nPost-order test Passed!" << std::endl;
398 test3(binaryTree, root);
399 std::cout <<
"\nIn-order test Passed!" << std::endl;
403 root->left->data = -2;
404 root->right->data = -3;
405 root->left->left->data = -4;
406 root->left->right->data = -5;
408 std::cout <<
"\n| Tests for negative data values |" << std::endl;
409 test4(binaryTree, root);
410 std::cout <<
"\nPre-order test on-negative value Passed!" << std::endl;
412 test5(binaryTree, root);
413 std::cout <<
"\nPost-order test on-negative value Passed!" << std::endl;
415 test6(binaryTree, root);
416 std::cout <<
"\nIn-order test on-negative value Passed!" << std::endl;
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
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.
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.