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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
#include <iostream>
using namespace std;
// TREE Class
class tree
{
private:
struct node //holds the value and position within the tree
{
int num; // holds the value for placement into tree
node* LC; // left child value
node* RC; // right child value
};
int newNode() // Creates a new node to be inserted in the tree
{
node* temp;
temp->LC = NULL;
temp->RC = NULL;
temp->num = 0;
}
node* root = NULL;//head
void insert(node* cp, int target) //inserting numbers into the tree
{
node* p = NULL;
while (cp != NULL)
{
p = cp;
if (target < cp->num)
cp = cp->LC;
else
cp = cp->RC;
}
cp = new node();
cp->num = target;
if (p == NULL)
root = cp;
if (p != NULL)
if (target < p->num)
p->LC = cp;
else
p->RC = cp;
}
bool ifNodeExists(struct node* root, int key) //bool function to search if a value is already in the tree
{
bool answer = ifNodeExists(root->LC, key)
, answer2 = ifNodeExists(root->RC, key);
if (root == NULL)
return false;
if (root->num == key)
return true;
if (answer) //if node is found, return true
return true;
//check right side of tree
return answer2;
}
int sum(node* root) //function to find the sum of values in a tree
{
if (root == NULL)
return 0;
return (root->num + sum(root->LC) + sum(root->RC));
}
int countNumNodes(node* root) //function to cnt the number of nodes
{
int c = 1;
if (root == NULL)
return 0;
else
{
c += countNumNodes(root->LC);
c += countNumNodes(root->RC);
return c;
}
}
int getLeafCount(node* root) //return the number of leaf nodes
{
if (root == NULL)
return 0;
if (root->LC == NULL && root->RC == NULL)
return 1;
else
return getLeafCount(root->LC) + getLeafCount(root->RC);
}
int oneChildCount(node* root) //counts the number of nodes with only 1 child
{
if (root == NULL)
return 0;
int cnt = 0;
if ((root->LC == NULL && root->RC != NULL) || (root->LC != NULL && root->RC == NULL))
cnt++;
cnt += (oneChildCount(root->LC) + oneChildCount(root->RC));
return cnt;
}
void find(node* root, int level, int& maxLevel, int& deepest) //finds the deepest node in the tree
{
if (root != NULL)
{
find(root->LC, ++level, maxLevel, deepest);
if (level > maxLevel)
{
deepest = root->num;
maxLevel = level;
}
find(root->RC, level, maxLevel, deepest);
}
}
int deepestNode(node* root) //returns the value of the deepest node in the tree
{
int deepest = -1;
int maxLevel = -1;
find(root, 0, maxLevel, deepest);
return deepest;
}
void Search( node*& root, int n)
{
if (!root)
return;
if (root->num == n)
Search(root->LC, n);
Search(root->RC, n);
}
node* FindMin(node* root)
{
while (root->LC != NULL)
root = root->LC;
return root;
}
//found this method online, incorporated it bc i wasn't sure if my method was wrong
node* Delete(node* root, int num) //deletes nodes in the tree <10
{
if (root == NULL)
return root;
else if (num < root->num) root->LC = Delete(root->LC, num);
else if (num > root->num) root->RC = Delete(root->RC, num);
// Wohoo... I found you, Get ready to be deleted
else {
// Case 1: No child
if (root->LC == NULL && root->RC == NULL) {
delete root;
root = NULL;
}
//Case 2: One child
else if (root->LC == NULL) {
node* temp = root;
root = root->RC;
delete temp;
}
else if (root->RC == NULL) {
node* temp = root;
root = root->LC;
delete temp;
}
// case 3: 2 children
else {
node* temp = FindMin(root->RC);
root->num = temp->num;
root->RC = Delete(root->RC, temp->num);
}
}
return root;
}
void printInorder(node* root, int& cnt) //function to print the tree in order
{
if (root == NULL)
return;
if (cnt < 20)
{
//cnt++;//commented out bc it would cause more issues
printInorder(root->LC, cnt);
cout << root->num << " ";
if(++cnt%10==0)//this is where i'm trying to do 10 values per line
cout<<endl;//instead it would print 8 then 10 then 2 on three lines
printInorder(root->RC, cnt);
}
}
void printPreorder(node* root, int& cnt) //function to print the tree in preorder
{
if (root == NULL)
return;
if (cnt < 20)
{
cnt++;
cout << root->num << " ";
printPreorder(root->LC, cnt);
printPreorder(root->RC, cnt);
}
}
void printPostorder(node* root, int& cnt) //function to print the tree in postorder
{
if (root == NULL)
return;
if (cnt < 20)
{
cnt++;
printPostorder(root->LC, cnt);
printPostorder(root->RC, cnt);
cout << root->num << " ";
}
}
public:
tree()
{
root = NULL;
}
void Search(int n)
{
Search(root, n);
}
void insert(int target)
{
insert(root, target);
}
bool ifNodeExists(int key)
{
return ifNodeExists(root, key);
}
int sum()
{
return sum(root);
}
int countNumNodes()
{
return countNumNodes(root);
}
int getLeafCount()
{
return getLeafCount(root);
}
int oneChildCount()
{
return oneChildCount(root);
}
int deepestNode()
{
return deepestNode(root);
}
node* Delete(int num)
{
return Delete(root,num);
}
void printInorder(int& cnt)
{
printInorder(root, cnt);
}
void printPreorder(int& count1)
{
printPreorder(root, count1);
}
void printPostorder(int& cnt)
{
printPostorder(root, cnt);
}
int sumDigits(int n) //recursive function to find the sum of a digit
{
if (n)
return ((n % 10) + sumDigits(n / 10));
else
return 0;
}
};//END of Class Tree
|