Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit c827c50

Browse files
authored
add inorder and postorder traversal for cpp (#951)
1 parent 7d8517d commit c827c50

File tree

3 files changed

+325
-0
lines changed

3 files changed

+325
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include <iostream>
2+
#include <stack>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
// Every node of the binary tree is represented by a 'node' data type
8+
struct node
9+
{
10+
int key_value; // Value the node holds
11+
node *left; // Address of its left child
12+
node *right; // Address of its right child
13+
};
14+
15+
class binaryTree
16+
{
17+
private:
18+
node *root;
19+
void insertKey(int key, node *leaf);
20+
void inOrderTraversal_recursive(node *leaf);
21+
22+
public:
23+
binaryTree();
24+
void insertKeys(vector<int> keys);
25+
void insertKey(int key);
26+
void inOrderTraversal_recursive();
27+
void inOrderTraversal_iterative();
28+
};
29+
30+
// Constructor. Initializes root to NULL.
31+
binaryTree::binaryTree()
32+
{
33+
root = NULL;
34+
}
35+
36+
// Accepts a vector and calls insertKey(int) iteratively.
37+
void binaryTree::insertKeys(vector<int> keys)
38+
{
39+
int len = keys.size();
40+
for (int i = 0; i < len; i++)
41+
{
42+
insertKey(keys[i]);
43+
}
44+
}
45+
46+
// When called for the first time (when root IS initialized as NULL), insertKey(int)
47+
// initializes root to point to a node data type. This node data type gets its key_value
48+
// as the key (argument) and child nodes as NULL.
49+
// For all calls other than the first time, it further calls insertKey(int, node*).
50+
void binaryTree::insertKey(int key)
51+
{
52+
if (root != NULL)
53+
{
54+
insertKey(key, root);
55+
}
56+
else
57+
{
58+
root = new node;
59+
root->key_value = key;
60+
root->left = NULL;
61+
root->right = NULL;
62+
}
63+
}
64+
65+
// Inserts the key, such that the tree follows the definition of a binary tree.
66+
// The function traverses down the tree starting from the root to find a suitable position.
67+
// If the key is less than the key_value at a node, the function calls itself with its left subtree.
68+
// If the key is greater than or equal to the key_value at a node, the function calls itself with its right subtree.
69+
// When it reaches a suitable place, it creates a new node. This node gets key_value as the key and child nodes as NULL.
70+
void binaryTree::insertKey(int key, node *leaf)
71+
{
72+
if (key < leaf->key_value)
73+
{
74+
if (leaf->left != NULL)
75+
{
76+
insertKey(key, leaf->left);
77+
}
78+
else
79+
{
80+
leaf->left = new node;
81+
leaf->left->key_value = key;
82+
leaf->left->left = NULL;
83+
leaf->left->right = NULL;
84+
}
85+
}
86+
else if (key >= leaf->key_value)
87+
{
88+
if (leaf->right != NULL)
89+
{
90+
insertKey(key, leaf->right);
91+
}
92+
else
93+
{
94+
leaf->right = new node;
95+
leaf->right->key_value = key;
96+
leaf->right->left = NULL;
97+
leaf->right->right = NULL;
98+
}
99+
}
100+
}
101+
102+
// Calls inOrderTraversal_recursive, a private member-function.
103+
void binaryTree::inOrderTraversal_recursive()
104+
{
105+
cout << "InOrderTraversal (recursive): ";
106+
inOrderTraversal_recursive(root);
107+
cout << endl;
108+
}
109+
110+
// For a particular node, the function prints its key_value and recursively calls
111+
// its left and right subtrees (in that order).
112+
void binaryTree::inOrderTraversal_recursive(node *leaf)
113+
{
114+
if (leaf != NULL)
115+
{
116+
inOrderTraversal_recursive(leaf->left);
117+
cout << leaf->key_value << " ";
118+
inOrderTraversal_recursive(leaf->right);
119+
}
120+
}
121+
122+
// The iterative in-order traversal function.
123+
void binaryTree::inOrderTraversal_iterative()
124+
{
125+
cout << "InOrderTraversal (iterative): ";
126+
if (root == NULL)
127+
return;
128+
stack<node *> nodeAddr;
129+
node *currentNode = root;
130+
131+
while (currentNode != NULL || !nodeAddr.empty())
132+
{
133+
while (currentNode != NULL)
134+
{
135+
nodeAddr.push(currentNode);
136+
currentNode = currentNode->left;
137+
}
138+
if (!nodeAddr.empty())
139+
{
140+
currentNode = nodeAddr.top();
141+
nodeAddr.pop();
142+
cout << currentNode->key_value << " ";
143+
currentNode = currentNode->right;
144+
}
145+
}
146+
}
147+
148+
int main()
149+
{
150+
binaryTree Tree;
151+
vector<int> elements = {25, 15, 50, 10, 22, 35, 70, 4, 12, 18, 24, 31, 44, 66, 80};
152+
Tree.insertKeys(elements);
153+
154+
Tree.inOrderTraversal_recursive(); // Expected: 4 10 12 15 18 22 24 25 31 35 44 50 66 70 80
155+
Tree.inOrderTraversal_iterative(); // Expected: 4 10 12 15 18 22 24 25 31 35 44 50 66 70 80
156+
157+
cout << endl;
158+
159+
return 0;
160+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#include <iostream>
2+
#include <stack>
3+
#include <vector>
4+
using namespace std;
5+
6+
// Every node of the binary tree is represented by a 'node' data type
7+
struct node
8+
{
9+
int key_value; // Value the node holds
10+
node *left; // Address of its left child
11+
node *right; // Address of its right child
12+
};
13+
14+
class binaryTree
15+
{
16+
private:
17+
node *root;
18+
void insertKey(int key, node *leaf);
19+
void postOrderTraversal_recursive(node *leaf);
20+
21+
public:
22+
binaryTree();
23+
void insertKeys(vector<int> keys);
24+
void insertKey(int key);
25+
void postOrderTraversal_recursive();
26+
void postOrderTraversal_iterative();
27+
};
28+
29+
// Constructor. Initializes root to NULL.
30+
binaryTree::binaryTree()
31+
{
32+
root = NULL;
33+
}
34+
35+
// Accepts a vector and calls insertKey(int) iteratively.
36+
void binaryTree::insertKeys(vector<int> keys)
37+
{
38+
int len = keys.size();
39+
for (int i = 0; i < len; i++)
40+
{
41+
insertKey(keys[i]);
42+
}
43+
}
44+
45+
// When called for the first time (when root IS initialized as NULL), insertKey(int)
46+
// initializes root to point to a node data type. This node data type gets its key_value
47+
// as the key (argument) and child nodes as NULL.
48+
// For all calls other than the first time, it further calls insertKey(int, node*).
49+
void binaryTree::insertKey(int key)
50+
{
51+
if (root != NULL)
52+
{
53+
insertKey(key, root);
54+
}
55+
else
56+
{
57+
root = new node;
58+
root->key_value = key;
59+
root->left = NULL;
60+
root->right = NULL;
61+
}
62+
}
63+
64+
// Inserts the key, such that the tree follows the definition of a binary tree.
65+
// The function travels down the tree starting from the root to find a suitable position.
66+
// If the key is less than the key_value at a node, the function calls itself with its left subtree.
67+
// If the key is greater than or equal to the key_value at a node, the function calls itself with its right subtree.
68+
// When it reaches a suitable place, it creates a new node. This node gets key_value as the key and child nodes as NULL.
69+
void binaryTree::insertKey(int key, node *leaf)
70+
{
71+
if (key < leaf->key_value)
72+
{
73+
if (leaf->left != NULL)
74+
{
75+
insertKey(key, leaf->left);
76+
}
77+
else
78+
{
79+
leaf->left = new node;
80+
leaf->left->key_value = key;
81+
leaf->left->left = NULL;
82+
leaf->left->right = NULL;
83+
}
84+
}
85+
else if (key >= leaf->key_value)
86+
{
87+
if (leaf->right != NULL)
88+
{
89+
insertKey(key, leaf->right);
90+
}
91+
else
92+
{
93+
leaf->right = new node;
94+
leaf->right->key_value = key;
95+
leaf->right->left = NULL;
96+
leaf->right->right = NULL;
97+
}
98+
}
99+
}
100+
101+
// Recursive Post-Order Traversal
102+
void binaryTree::postOrderTraversal_recursive()
103+
{
104+
cout << "PostOrderTraversal (recursive): ";
105+
postOrderTraversal_recursive(root);
106+
cout << endl;
107+
}
108+
109+
void binaryTree::postOrderTraversal_recursive(node *leaf)
110+
{
111+
if (leaf != NULL)
112+
{
113+
postOrderTraversal_recursive(leaf->left);
114+
postOrderTraversal_recursive(leaf->right);
115+
cout << leaf->key_value << " ";
116+
}
117+
}
118+
119+
// Iterative Post-Order Traversal
120+
void binaryTree::postOrderTraversal_iterative()
121+
{
122+
cout << "PostOrderTraversal (iterative): ";
123+
if (root == NULL)
124+
return;
125+
126+
stack<node *> nodeAddr;
127+
node *currentNode = root;
128+
node *lastNodeVisited = NULL;
129+
130+
while (!nodeAddr.empty() || currentNode != NULL)
131+
{
132+
if (currentNode != NULL)
133+
{
134+
nodeAddr.push(currentNode);
135+
currentNode = currentNode->left;
136+
}
137+
else
138+
{
139+
node *topNode = nodeAddr.top();
140+
141+
if (topNode->right != NULL && lastNodeVisited != topNode->right)
142+
{
143+
currentNode = topNode->right;
144+
}
145+
else
146+
{
147+
cout << topNode->key_value << " ";
148+
lastNodeVisited = topNode;
149+
nodeAddr.pop();
150+
}
151+
}
152+
}
153+
}
154+
155+
int main()
156+
{
157+
binaryTree Tree;
158+
vector<int> elements = {25, 15, 50, 10, 22, 35, 70, 4, 12, 18, 24, 31, 44, 66, 80}; // The elements to be inserted in the tree
159+
Tree.insertKeys(elements);
160+
161+
Tree.postOrderTraversal_recursive(); // Expected: 4 12 10 18 24 22 15 31 44 35 66 80 70 50 25
162+
Tree.postOrderTraversal_iterative(); // Expected: 4 12 10 18 24 22 15 31 44 35 66 80 70 50 25
163+
164+
cout << endl;
165+
}

0 commit comments

Comments
 (0)