-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab04TreeTraversals.cpp
152 lines (134 loc) · 3.79 KB
/
Lab04TreeTraversals.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//Stefan Emmons
//COSC 2030 - 01
//Lab 04
//Dr.Hill, or Pedro Marquez
//3-4-2019
#include<iostream>
using namespace std;
struct Node
{
int value;
struct Node * rightNode = NULL;
struct Node * leftNode = NULL;
};
Node * searchNode(Node * root, int value)
{
Node * rightResult;
Node * leftResult;
if(root == NULL)
{
return NULL;
}
else if (root->value == value)
{
return root;
}
else
{
rightResult = searchNode(root->rightNode, value);
leftResult = searchNode(root->leftNode, value);
if (rightResult == NULL && leftResult == NULL)
{
return NULL;
}
else if(rightResult != NULL)
{
return rightResult;
}
else
{
return leftResult;
}
}
}
void insertLeft(Node * Parent, Node * nodeToInsert)
{
//This may not be entirely neccessary, but it helps me to visualize how each new node is created and placed.
Parent->leftNode = nodeToInsert;
}
void insertRight(Node * Parent, Node * nodeToInsert)
{
//This may not be entirely neccessary, but it helps me to visualize how each new node is created and placed.
Parent->rightNode = nodeToInsert;
}
int inOrderTransversal(Node * root)
{
if(root != NULL)
{
inOrderTransversal(root->leftNode);
cout << root->value << endl;
inOrderTransversal(root->rightNode);
}
return 0;
}
int postOrderTransversal(Node * root)
{
//This follows much of the same style as the pre-created inOrder function. However instead of the
//left root right traversal style, this follows the left right root traversal style.
if (root != NULL)
{
postOrderTransversal(root->leftNode);
postOrderTransversal(root->rightNode);
cout << root->value << endl;
}
return 0;
}
int preOrderTransversal(Node * root)
{
//This follows much of the same style as the pre-created inOrder function. However instead of the
//left root right traversal style, this follows the root left righ traversal style.
if (root != NULL)
{
cout << root->value << endl;
preOrderTransversal(root->leftNode);
preOrderTransversal(root->rightNode);
}
return 0;
}
int main()
{
//This block of code represents the "original" tree formation. All other nodes will stem from this base.
Node * BinaryTree = new Node;
BinaryTree->value = 5;
Node * tmp = new Node;
tmp->value = 1;
BinaryTree->rightNode = tmp;
tmp = new Node;
tmp->value = 4;
BinaryTree->leftNode = tmp;
//This block of code is using a Binary Tree Search algorithm. We first create a new pointer to the Node struct
// and define it as the result for the Binary Tree Search.
//From there, each root/parent node is defined beforehand, and the corresponding child node is added via the
//tmp node.
//This process is repeated as neccessary until we create the tree defined in the assignment.
Node * searchResult = searchNode(BinaryTree, 1);
tmp = new Node;
tmp->value = 7;
insertRight(searchResult, tmp);
searchResult = searchNode(BinaryTree, 7);
tmp = new Node;
tmp->value = 9;
insertRight(searchResult, tmp);
searchResult = searchNode(BinaryTree, 4);
tmp = new Node;
tmp->value = 10;
insertLeft(searchResult, tmp);
searchResult = searchNode(BinaryTree, 4);
tmp = new Node;
tmp->value = 15;
insertRight(searchResult, tmp);
searchResult = searchNode(BinaryTree, 15);
tmp = new Node;
tmp->value = 8;
insertLeft(searchResult, tmp);
//Each individual print statement labels what traversal is being displayed, while each corresponding function is called.
cout << "The following is the 'In-order' traversal;" << endl;
inOrderTransversal(BinaryTree);
cout << "The following is the 'Pre-order' traversal;" << endl;
preOrderTransversal(BinaryTree);
cout << "The following is the 'Post-order' traversal;" << endl;
postOrderTransversal(BinaryTree);
cout << endl;
system("pause");
return 0;
}