-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcs235_assign13.cpp
180 lines (161 loc) · 5.27 KB
/
cs235_assign13.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/******************************************************************************
* Program:
* Assignment 13, BTree class methods
* Brother Ercanbrack, CS 235
* Author:
* Tyler Scott
* Summary:
* This program defines the member functions for the BTree class
*
* Estimated time: 8.0hrs
* Actual time: 2.0hrs
******************************************************************************/
#include "tree.h"
#include <iostream>
using namespace std;
/*****************************************************************************
* Default BTree constructor
*****************************************************************************/
BTree :: BTree(int data)
{
this->data = data;
left = NULL;//= NULL;
right = NULL; //= NULL;
parent = NULL;// = NULL;
}
/*****************************************************************************
* Deconstructor left blank because it is not used
*****************************************************************************/
BTree :: ~BTree(void)
{
}
/*****************************************************************************
* Inserts a node on the left pointer of the current node
*****************************************************************************/
void BTree :: insertLeft(int item)
{
BTree* insert = new BTree(item);
left = insert;
insert->parent = this;
}
/*****************************************************************************
* Inserts a node on the right pointer of the current node
*****************************************************************************/
void BTree :: insertRight(int item)
{
BTree* insert = new BTree(item);
right = insert;
insert->parent = this;
}
/*****************************************************************************
* grants access to the private BTree node "left"
*****************************************************************************/
BTree* BTree :: getLeftChild(void)
{
return left;
}
/*****************************************************************************
* grants access to the private BTree node "right"
*****************************************************************************/
BTree* BTree :: getRightChild(void)
{
return right;
}
/*****************************************************************************
* grants access to "parent"
*****************************************************************************/
BTree* BTree :: getParent(void)
{
return parent;
}
/*****************************************************************************
* grants access to "data"
*****************************************************************************/
int BTree :: getData(void)
{
return data;
}
/*****************************************************************************
* sets the value of data to what is passed in
*****************************************************************************/
void BTree :: setData(int item)
{
data = item;
}
/*****************************************************************************
* sets left to the tree that is passed in
*****************************************************************************/
void BTree :: setLeft(BTree* tree)
{
left = tree;
if (tree != NULL)
left->parent = this;
}
/*****************************************************************************
* sets the right to the tree that is passed in
*****************************************************************************/
void BTree :: setRight(BTree* tree)
{
right = tree;
if (tree != NULL)
right->parent = this;
}
/*****************************************************************************
* left visit right traversal
*****************************************************************************/
void BTree :: infix(void)
{
if (left != NULL) left->infix();
cout << data << " ";
if (right != NULL) right->infix();
}
/*****************************************************************************
* visit left right traversal
*****************************************************************************/
void BTree :: prefix(void)
{
cout << data << " ";
if (left != NULL) left->prefix();
if (right != NULL) right->prefix();
}
/*****************************************************************************
* left right visit traversal
*****************************************************************************/
void BTree :: postfix(void)
{
if (left != NULL) left->postfix();
if (right != NULL) right->postfix();
cout << data << " ";
}
/*****************************************************************************
* level order traversal.
*****************************************************************************/
void BTree :: level(void)
{
const int MAX = 50;
BTree *queue[MAX];
BTree *temp;
int front = 0;
int back = 0;
queue[back++] = this;
while (front != back)
{
temp = queue[front];
front = (front + 1) % MAX;
if (temp != NULL)
{
// visit
cout.width(4);
cout << temp->data << " ";
if (temp->parent == NULL)
cout << " Parent = NULL! " << endl;
else
cout << " Parent = " << temp->parent->data << " " << endl;
// end Visit
queue[back] = temp->left;
back = (back + 1) % MAX;
queue[back] = temp->right;
back = (back + 1) % MAX;
}
}
}