# Algorithms_in_C++: others::iterative_tree_traversals::BinaryTree Class Reference

Loading...
Searching...
No Matches
others::iterative_tree_traversals::BinaryTree Class Reference

defines the functions associated with the binary tree More...

## Public Member Functions

Node * | createNewNode (int64_t)
| function that will create new node for insertion. More...
std::vector< int64_t > | preOrderIterative (Node *)
| preOrderIterative() function that will perform the preorder traversal iteratively, and return the result array that contain the preorder traversal of a tree. More...
std::vector< int64_t > | postOrderIterative (Node *)
| postOrderIterative() function that will perform the postorder traversal iteratively, and return the result array that contain the postorder traversal of a tree. More...
std::vector< int64_t > | inOrderIterative (Node *)
| inOrderIterative() function that will perform the inorder traversal iteratively, and return the result array that contain the inorder traversal of a tree. More...

## Detailed Description

defines the functions associated with the binary tree

## Member Function Documentation

## createNewNode()

Node * others::iterative_tree_traversals::BinaryTree::createNewNode | ( | int64_t | data | ) |

function that will create new node for insertion.

will allocate the memory for a node and, along the data and return the node.

Parameters

  data | value that a particular node will contain.

Returns
  pointer to the newly created node with assigned data.

{
Node *node = new Node();
node->data = data;
node->left = node->right = nullptr;
return node;
}
data
int data[MAX]
test data
Definition: hash_search.cpp:24
Node
Definition: linkedlist_implentation_usingarray.cpp:14
node
Definition: avltree.cpp:13

## inOrderIterative()

std::vector< int64_t > others::iterative_tree_traversals::BinaryTree::inOrderIterative | ( | Node * | root | ) |

inOrderIterative() function that will perform the inorder traversal iteratively, and return the result array that contain the inorder traversal of a tree.

function that takes root of the tree as an argument, and returns its inorder traversal.

Parameters

  root | head/root node of a tree

Returns
  result that is containing the inorder traversal of a tree

< is used to find and traverse the child nodes.

< List of values, sorted in in-order.

{
std::stack<Node *>
stack; ///< is used to find and traverse the child nodes.
std::vector<int64_t> result; ///< List of values, sorted in in-order.
Node *current = root;
while (!stack.empty() || current) {
while (current) {
stack.push(current);
current = current->left;
}
current = stack.top();
stack.pop();
result.push_back(current->data);
current = current->right;
}
return result;
}
stack
Definition: stack.h:26
stack::top
Type top()
Definition: stack.h:93
stack::push
void push(Type item)
Definition: stack.h:83
stack::pop
void pop()
Definition: stack.h:99
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
std::stack
std::vector
Here is the call graph for this function:

## postOrderIterative()

std::vector< int64_t > others::iterative_tree_traversals::BinaryTree::postOrderIterative | ( | Node * | root | ) |

postOrderIterative() function that will perform the postorder traversal iteratively, and return the result array that contain the postorder traversal of a tree.

function that takes root of the tree as an argument, and returns its postorder traversal.

Parameters

  root | head/root node of a tree

Returns
  result that is containing the postorder traversal of a tree

< is used to find and traverse the child nodes.

< List of values, sorted in post-order.

{
std::stack<Node *>
stack; ///< is used to find and traverse the child nodes.
std::vector<int64_t> result; ///< List of values, sorted in post-order.
stack.push(root);
while (!stack.empty()) {
result.push_back(stack.top()->data);
Node *current = stack.top();
stack.pop();
if (current->left) {
stack.push(current->left);
}
if (current->right) {
stack.push(current->right);
}
}
reverse(result.begin(), result.end());
return result;
}
std::reverse
T reverse(T... args)
Here is the call graph for this function:

## preOrderIterative()

std::vector< int64_t > others::iterative_tree_traversals::BinaryTree::preOrderIterative | ( | Node * | root | ) |

preOrderIterative() function that will perform the preorder traversal iteratively, and return the result array that contain the preorder traversal of a tree.

function that takes root of the tree as an argument, and returns its preorder traversal.

Parameters

  root | head/root node of a tree

Returns
  result that is containing the preorder traversal of a tree

< is used to find and traverse the child nodes.

< list of values, sorted in pre-order.

{
std::stack<Node *>
stack; ///< is used to find and traverse the child nodes.
std::vector<int64_t> result; ///< list of values, sorted in pre-order.
stack.push(root);
while (!stack.empty()) {
result.push_back(stack.top()->data);
Node *current = stack.top();
stack.pop();
if (current->right) {
stack.push(current->right);
}
if (current->left) {
stack.push(current->left);
}
}
return result;
}
Here is the call graph for this function:

The documentation for this class was generated from the following file:

- others/iterative_tree_traversals.cpp