TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Recursive version of Inorder, Preorder, and Postorder [Traversal of the Tree] (https://en.wikipedia.org/wiki/Tree_traversal) More...
#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Go to the source code of this file.
Classes | |
struct | others::recursive_tree_traversals::Node |
The structure to hold Nodes of the tree. More... | |
class | others::recursive_tree_traversals::BT |
BT used to make the entire structure of the binary tree and the functions associated with the binary tree. More... | |
Namespaces | |
namespace | others |
for vector | |
namespace | interpolation_search |
Functions for the Recursive version of Inorder, Preorder, and Postorder Traversal of the Tree algorithm implementation. | |
Functions | |
void | others::recursive_tree_traversals::deleteAll (const Node *const root) |
void | test1 () |
1st test-case | |
void | test2 () |
2nd test-case | |
void | test3 () |
3rd test-case | |
static void | tests () |
Self-test implementations. | |
int | main () |
Main function. | |
Recursive version of Inorder, Preorder, and Postorder [Traversal of the Tree] (https://en.wikipedia.org/wiki/Tree_traversal)
For traversing a (non-empty) binary tree in an inorder fashion, we must do these three things for every node n starting from the tree’s root:
(L) Recursively traverse its left subtree. When this step is finished, we are back at n again. (N) Process n itself. (R) Recursively traverse its right subtree. When this step is finished, we are back at n again.
In normal inorder traversal, we visit the left subtree before the right subtree. If we visit the right subtree before visiting the left subtree, it is referred to as reverse inorder traversal.
For traversing a (non-empty) binary tree in a preorder fashion, we must do these three things for every node n starting from the tree’s root:
(N) Process n itself. (L) Recursively traverse its left subtree. When this step is finished, we are back at n again. (R) Recursively traverse its right subtree. When this step is finished, we are back at n again.
In normal preorder traversal, visit the left subtree before the right subtree. If we visit the right subtree before visiting the left subtree, it is referred to as reverse preorder traversal.
For traversing a (non-empty) binary tree in a postorder fashion, we must do these three things for every node n starting from the tree’s root:
(L) Recursively traverse its left subtree. When this step is finished, we are back at n again. (R) Recursively traverse its right subtree. When this step is finished, we are back at n again. (N) Process n itself.
In normal postorder traversal, visit the left subtree before the right subtree. If we visit the right subtree before visiting the left subtree, it is referred to as reverse postorder traversal.
Definition in file recursive_tree_traversal.cpp.
void others::recursive_tree_traversals::deleteAll | ( | const Node *const | root | ) |
Definition at line 185 of file recursive_tree_traversal.cpp.
int main | ( | void | ) |
Main function.
Definition at line 398 of file recursive_tree_traversal.cpp.
void test1 | ( | ) |
1st test-case
< result stores the inorder traversal of the binary tree
< result stores the preorder traversal of the binary tree
< result stores the postorder traversal of the binary tree
Definition at line 201 of file recursive_tree_traversal.cpp.
void test2 | ( | ) |
2nd test-case
< result stores the inorder traversal of the binary tree
< result stores the preorder traversal of the binary tree
< result stores the postorder traversal of the binary tree
Definition at line 266 of file recursive_tree_traversal.cpp.
void test3 | ( | ) |
3rd test-case
< result stores the inorder traversal of the binary tree
< result stores the preorder traversal of the binary tree
< result stores the postorder traversal of the binary tree
Definition at line 327 of file recursive_tree_traversal.cpp.
|
static |
Self-test implementations.
Definition at line 386 of file recursive_tree_traversal.cpp.