-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinalTree(2).cpp
145 lines (126 loc) · 3.5 KB
/
FinalTree(2).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
/*
Name: FinalTree
Copyright: 2022
Author: Faizan Khan
Date: 12/12/22 12:01
Description: Created a class called FinalTree
with member items lchild, rchild, and data.
Also some basic tree operations and have
an instance called June13Tree
*/
#include <iostream>
using namespace std;
// Tree items and operations
class FinalTree
{
public:
int data;
FinalTree *lchild;
FinalTree *rchild;
FinalTree();
FinalTree(int, FinalTree*, FinalTree*);
FinalTree *createNode(int);
void preOrder(FinalTree *);
void inOrder(FinalTree *);
void postOrder(FinalTree *);
};
// Set data value to 0, the left and right child nodes are NULL before entering input
FinalTree::FinalTree()
{
data = 0;
lchild = rchild = nullptr;
}
// Pass data to the data value and the left and right child nodes
FinalTree::FinalTree(int value, FinalTree *left, FinalTree *right)
{
data = value;
lchild = left;
rchild = right;
}
// Set data value to 0 as no pre-existing data and get input to make the tree
FinalTree *FinalTree::createNode(int data)
{
data = 0;
cout << "Enter some data for the tree ";
cin >> data;
FinalTree *newNode = new FinalTree();
newNode->data = data;
newNode->lchild = newNode->rchild = nullptr;
return newNode;
}
// The preorder transversal will start at root, then left, then right
void FinalTree::preOrder(FinalTree *root)
{
if (root == NULL)
{
return;
}
cout << root->data << " ";
preOrder(root->lchild);
preOrder(root->rchild);
}
// The inorder transversal order is to go left, then root, then right
void FinalTree::inOrder(FinalTree *root)
{
if (root == NULL)
{
return;
}
inOrder(root->lchild);
cout << root->data << " ";
inOrder(root->rchild);
}
// The postorder transversal order is to go left, then right, then root
void FinalTree::postOrder(FinalTree *root)
{
if (root == NULL)
{
return;
}
postOrder(root->lchild);
postOrder(root->rchild);
cout << root->data << " ";
}
// Main function
int main()
{
int value;
int leftValue;
int rightValue;
FinalTree *root;
// Level 1 (Root)
FinalTree June13Tree(value, 0, 0);
root = June13Tree.createNode(value);
// Level 2 (Left and right node)
FinalTree *lchild = June13Tree.createNode(leftValue);
root->lchild = lchild;
FinalTree *rchild = June13Tree.createNode(rightValue);
root->rchild = rchild;
// Level 3 (Left and right node children)
lchild->lchild = June13Tree.createNode(leftValue);
lchild->rchild = June13Tree.createNode(leftValue);
rchild->lchild = June13Tree.createNode(rightValue);
rchild->rchild = June13Tree.createNode(rightValue);
// Level 4 (left and right node children's children)
lchild->lchild->lchild = June13Tree.createNode(leftValue);
lchild->lchild->rchild = June13Tree.createNode(leftValue);
lchild->rchild->lchild = June13Tree.createNode(leftValue);
lchild->rchild->rchild = June13Tree.createNode(leftValue);
rchild->lchild->lchild = June13Tree.createNode(rightValue);
rchild->lchild->rchild = June13Tree.createNode(rightValue);
rchild->rchild->lchild = June13Tree.createNode(rightValue);
rchild->rchild->rchild = June13Tree.createNode(rightValue);
// Display the preorder transversal
cout << "PreOrder: ";
June13Tree.preOrder(root);
cout << endl;
// Display the inorder transversal
cout << "InOrder: ";
June13Tree.inOrder(root);
cout << endl;
// Display the postorder transversal
cout << "PostOrder: ";
June13Tree.postOrder(root);
cout << endl;
return 0;
}