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
|
//main.cpp
#include "main.h"
#include <vector>
#include <iostream>
using namespace std;
vector <node> list;
//node::node (int valueIn)
/*{
value=valueIn;
lessThan=false;
greaterThan=false;
}*/
void initTree(int root)
{
list.push_back(node(root));
}
void deleteTree(int root)
{
int temp;
for (int c=0;root!=list[c].value;c++)
{
temp=c;
}
if (list[temp].lessThan==true)
{
deleteTree(list[temp].smallNode.value);
}
if (list[temp].greaterThan==true)
deleteTree(list[temp].bigNode.value);
else
list[temp].resize();
}
void insertItem(int root, int e)
{
int temp;
for (int c=0;root!=list[c].value;c++)
{
temp=c;
}
if (list[temp].value<e)
{
if(list[temp].greaterThan==true)
{
insertItem(list[temp].bigNode.value,e);
}
else
{
list.push_back(node(e));
list[temp].bigNode=list[list.size()];
}
}
else if (list[temp].value>e)
{
if(list[temp].lessThan==true)
{
insertItem(list[temp].smallNode.value,e);
}
else
{
list.push_back(node(e));
list[temp].smallNode=list[list.size()];
}
}
}
int countLeaves(int root)
{
int counter=0, current;
for(int c=0;list[c].value!=list[root].value;c++)
current=c;
if (list[current].lessThan==true)
counter=countLeaves(list[current].smallNode.value)+counter;
if (list[current].greaterThan==true)
counter=countLeaves(list[current].bigNode.value)+counter;
if (list[current].lessThan==false&&list[current].greaterThan==false)
counter++;
return counter;
}
int findHeight(int root, int counter)
{
int current, returnedLeftCounter=0, returnedRightCounter;
counter++;
for(int c=0;list[c].value!=list[root].value;c++)
current=c;
if (list[current].lessThan==true)
returnedLeftCounter=findHeight(list[current].smallNode.value, counter);
if (list[current].greaterThan==true)
returnedRightCounter=findHeight(list[current].bigNode.value, counter);
if (counter<returnedLeftCounter)
counter=returnedLeftCounter;
if (counter<returnedRightCounter)
counter=returnedRightCounter;
return counter;
}
int singleParent(int root)
{
int current, counter=0;
for(int c=0;list[c].value!=list[root].value;c++)
current=c;
if (list[current].lessThan==true)
counter=singleParent(list[current].smallNode.value)+counter;
if (list[current].greaterThan==true)
counter=singleParent(list[current].bigNode.value)+counter;
if (list[current].greaterThan==true&&list[current].lessThan==false)
counter++;
else if (list[current].greaterThan==false&&list[current].lessThan==true)
counter++;
return counter;
}
int findMin(int root)
{
int current, counter=0;
for(int c=0;list[c].value!=list[root].value;c++)
current=c;
counter=list[current].value;
if (list[current].lessThan==true)
counter=list[current].smallNode.value;
return counter;
}
int findMax(int root)
{
int current, counter=0;
for(int c=0;list[c].value!=list[root].value;c++)
current=c;
counter=list[current].value;
if (list[current].greaterThan==true)
counter=list[current].bigNode.value;
return counter;
}
void main()
{
bool finish=true;
while (finish)
{
int input, root;
cout<<"What would you like to do?"<<endl;
cout<<"0: Initialize the Tree"<<endl;
cout<<"1: Delete the tree from the root"<<endl;
cout<<"2: Add an item to the tree"<<endl;
cout<<"3: Count the leaves"<<endl;
cout<<"4: Find the height"<<endl;
cout<<"5: Count the single parents"<<endl;
cout<<"6: Find the minimum value"<<endl;
cout<<"7: Find the maximum value"<<endl;
cout<<"8: Quit"<<endl;
cin>>input;
switch (input)
{
case 0:
cout<<"What would you like your root to be?"<<endl;
cin>>root;
node initTree(root);
cout<<"Your tree has been initialized"<<endl;
break;
case 1:
cout<<"What would you like your root to be?"<<endl;
cin>>root;
deleteTree(root);
cout<<"Your tree has been deleted"<<endl;
break;
case 2:
cout<<"What would you like your root to be?"<<endl;
cin>>root;
cout<<"What would you like the element to be?"<<endl;
int element;
cin>>element;
insertItem(root, element);
cout<<"Your item has been added"<<endl;
break;
case 3:
cout<<"What would you like your root to be?"<<endl;
cin>>root;
int temp;
temp=countLeaves(root);
cout<<"Your tree has "<<temp<<" leaves."<<endl;
break;
case 4:
cout<<"What would you like your root to be?"<<endl;
cin>>root;
int temp=findHeight(root,0);
cout<<temp<<" is the height of the tree."<<endl;
break;
case 5:
cout<<"What would you like your root to be?"<<endl;
cin>>root;
int temp=singleParent(root);
cout<<"The tree has "<<temp<<" single parents."<<endl;
break;
case 6:
cout<<"What would you like your root to be?"<<endl;
cin>>root;
int temp=findMax(root);
cout<<"The trees max is "<<temp<<endl;
break;
case 7:
cout<<"What would you like your root to be?"<<endl;
cin>>root;
int temp=findMin(root);
cout<<"The trees minimum is "<<temp<<endl;
break;
case 8:
finish=false;
break;
}
}
}
| |