TheAlgorithms/C++ 1.0.0
All the 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

Definition at line 67 of file iterative_tree_traversals.cpp.

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.

Definition at line 88 of file iterative_tree_traversals.cpp.

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

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

Definition at line 164 of file iterative_tree_traversals.cpp.

164 {
165 std::stack<Node *>
166 stack;
167 std::vector<int64_t> result;
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)
char stack[MAX]

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

Definition at line 132 of file iterative_tree_traversals.cpp.

132 {
133 std::stack<Node *>
134 stack;
135 std::vector<int64_t> result;
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}

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

Definition at line 102 of file iterative_tree_traversals.cpp.

102 {
103 std::stack<Node *>
104 stack;
105 std::vector<int64_t> result;
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}

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