Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
others::iterative_tree_traversals::BinaryTree Class Reference

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

Public Member Functions

NodecreateNewNode (int64_t)
 function that will create new node for insertion.
 
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.
 
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.
 
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.
 

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
datavalue that a particular node will contain.
Returns
pointer to the newly created node with assigned data.
88 {
89 Node *node = new Node();
90 node->data = data;
91 node->left = node->right = nullptr;
92 return node;
93}
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13
int data[MAX]
test data
Definition hash_search.cpp:24
Definition linkedlist_implentation_usingarray.cpp:14
Definition binary_search_tree.cpp:11

◆ 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
roothead/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.

164 {
166 stack; ///< is used to find and traverse the child nodes.
167 std::vector<int64_t> result; ///< List of values, sorted in in-order.
168
169 Node *current = root;
170
171 while (!stack.empty() || current) {
172 while (current) {
173 stack.push(current);
174 current = current->left;
175 }
176 current = stack.top();
177 stack.pop();
178 result.push_back(current->data);
179 current = current->right;
180 }
181 return result;
182}
for std::invalid_argument
Definition stack.hpp:19
void pop()
Definition stack.hpp:62
void push(const value_type &item)
Definition stack.hpp:47
value_type top() const
Definition stack.hpp:56
uint64_t result(uint64_t n)
Definition fibonacci_sum.cpp:76
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
roothead/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.

132 {
134 stack; ///< is used to find and traverse the child nodes.
135 std::vector<int64_t> result; ///< List of values, sorted in post-order.
136
137 stack.push(root);
138
139 while (!stack.empty()) {
140 result.push_back(stack.top()->data);
141 Node *current = stack.top();
142 stack.pop();
143
144 if (current->left) {
145 stack.push(current->left);
146 }
147 if (current->right) {
148 stack.push(current->right);
149 }
150 }
151
152 reverse(result.begin(), result.end());
153
154 return result;
155}
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
roothead/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.

102 {
104 stack; ///< is used to find and traverse the child nodes.
105 std::vector<int64_t> result; ///< list of values, sorted in pre-order.
106
107 stack.push(root);
108
109 while (!stack.empty()) {
110 result.push_back(stack.top()->data);
111 Node *current = stack.top();
112 stack.pop();
113
114 if (current->right) {
115 stack.push(current->right);
116 }
117 if (current->left) {
118 stack.push(current->left);
119 }
120 }
121
122 return result;
123}
Here is the call graph for this function:

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