TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Iterative version of Preorder, Postorder, and preorder [Traversal of the Tree] (https://en.wikipedia.org/wiki/Tree_traversal) More...
#include <algorithm>
#include <cassert>
#include <iostream>
#include <stack>
#include <vector>
Go to the source code of this file.
Classes | |
struct | others::iterative_tree_traversals::Node |
defines the structure of a node of the tree More... | |
class | others::iterative_tree_traversals::BinaryTree |
defines the functions associated with the binary tree More... | |
Namespaces | |
namespace | others |
for vector | |
namespace | iterative_tree_traversals |
Functions for the Traversal of the Tree algorithm. | |
Functions | |
void | others::iterative_tree_traversals::deleteAll (Node *root) |
static void | test1 (others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root) |
Test the computed preorder with the actual preorder. | |
static void | test2 (others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root) |
Test the computed postorder with the actual postorder. | |
static void | test3 (others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root) |
Test the computed inorder with the actual inorder. | |
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. | |
int | main () |
Main function. | |
Iterative version of Preorder, Postorder, and preorder [Traversal of the Tree] (https://en.wikipedia.org/wiki/Tree_traversal)
Create a Stack that will store the Node of Tree. Push the root node into the stack. Save the root into the variabe named as current, and pop and elemnt from the stack. Store the data of current into the result array, and start traversing from it. Push both the child node of the current node into the stack, first right child then left child. Repeat the same set of steps untill the Stack becomes empty. And return the result array as the preorder traversal of a tree.
Create a Stack that will store the Node of Tree. Push the root node into the stack. Save the root into the variabe named as current, and pop and elemnt from the stack. Store the data of current into the result array, and start traversing from it. Push both the child node of the current node into the stack, first left child then right child. Repeat the same set of steps untill the Stack becomes empty. Now reverse the result array and then return it to the calling function as a postorder traversal of a tree.
Create a Stack that will store the Node of Tree. Push the root node into the stack. Save the root into the variabe named as current. Now iterate and take the current to the extreme left of the tree by traversing only to its left. Pop the elemnt from the stack and assign it to the current. Store the data of current into the result array. Repeat the same set of steps until the Stack becomes empty or the current becomes NULL. And return the result array as the inorder traversal of a tree.
Definition in file iterative_tree_traversals.cpp.
void others::iterative_tree_traversals::deleteAll | ( | Node * | root | ) |
Definition at line 183 of file iterative_tree_traversals.cpp.
int main | ( | void | ) |
Main function.
< instace of BinaryTree, used to access its members functions.
Definition at line 372 of file iterative_tree_traversals.cpp.
|
static |
Test the computed preorder with the actual preorder.
binaryTree | instance of the BinaryTree class |
root | head/root node of a tree |
< result stores the preorder traversal of the binary tree
Definition at line 210 of file iterative_tree_traversals.cpp.
|
static |
Test the computed postorder with the actual postorder.
binaryTree | instance of BinaryTree class |
root | head/root node of a tree |
< result stores the postorder traversal of the binary tree.
Definition at line 237 of file iterative_tree_traversals.cpp.
|
static |
Test the computed inorder with the actual inorder.
binaryTree | instance of BinaryTree class |
root | head/root node of a tree |
< result stores the inorder traversal of the binary tree.
Definition at line 264 of file iterative_tree_traversals.cpp.
|
static |
Test the computed preorder with the actual preorder on negative value.
binaryTree | instance of BinaryTree class |
root | head/root node of a tree |
< result stores the preorder traversal of the binary tree
Definition at line 291 of file iterative_tree_traversals.cpp.
|
static |
Test the computed postorder with the actual postorder on negative value.
binaryTree | instance of BinaryTree class |
root | head/root node of a tree |
< result stores the postorder traversal of the binary tree.
Definition at line 319 of file iterative_tree_traversals.cpp.
|
static |
Test the computed inorder with the actual inorder on negative value.
binaryTree | instance of BinaryTree class |
root | head/root node of a tree |
< result stores the inorder traversal of the binary tree.
Definition at line 346 of file iterative_tree_traversals.cpp.