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
|
using namespace std;
void printTree(string name);
void main()
{
Tree treeClass;
treeNode* theRoott = NULL;
treeNode* copyRoot = NULL;
treeClass.insertNode("Jinx",theRoott); // inserts the names into the binary tree;
treeClass.insertNode("Charmander",theRoott);
treeClass.insertNode("Snorlax",theRoott);
treeClass.insertNode("Clefairy",theRoott);
treeClass.insertNode("Diglett",theRoott);
treeClass.insertNode("Kakuna",theRoott);
treeClass.insertNode("Meowth",theRoott);
treeClass.insertNode("Nidorina",theRoott);
treeClass.insertNode("Pikachu",theRoott);
treeClass.insertNode("Blastoise",theRoott);
treeClass.insertNode("Squirtle",theRoott);
treeClass.insertNode("Ivysaur",theRoott);
treeClass.insertNode("Bulbasaur",theRoott);
treeClass.insertNode("Abra",theRoott);
treeClass.copyTree(copyRoot, theRoott);
treeClass.inOrderTraversal(printTree,copyRoot); // should print the copy tree
cin.get();
cin.ignore();
}
Tree::Tree(){
string data = "";
treeNode* left = NULL;
treeNode* right=NULL;
theRoot = NULL;
current = NULL;
Trail=NULL;
traversal=NULL;
nodeToSwap=NULL;
parentNodeToSwap=NULL;
}
bool Tree::insertNode(string name, treeNode* &theRoot)
{
treeNode* newNode = NULL;
treeNode* current = NULL;
treeNode* previous = NULL;
newNode = new treeNode;
newNode->data = name;
newNode->left = NULL;
newNode->right = NULL;
current = theRoot;
if (current == NULL) // if the current or the root is NULL that means its empty
{
theRoot = newNode;
return true;
}
else
{
while (current != NULL)
{
if (newNode->data < current->data) // we would go left
{
previous = current; // update our prev to equal current
current = current->left;// would go left because its smaller then current
}
else if (newNode->data> current->data) // we would go right
{
previous = current;
current = current->right;
}
else // the nodes are the same
{
return false;
}
}
if (newNode->data < previous->data)
{
previous->left = newNode;
}
else
{
previous->right = newNode;
}
return true;
}
}
void Tree::inOrderTraversal(const function<void(string)> &funcToCall, treeNode* theRoot)
{
if (theRoot != NULL)
{
// call the traversal function on the left child
inOrderTraversal(funcToCall,theRoot->left);
// print the node
//printTree(theRoot->data);
std::cout << theRoot->data << endl;
// call the traversal function on the right child
inOrderTraversal(funcToCall,theRoot->right);
}
}
void Tree::postOrderTraversal(const function<void(string)> &funcToCall, treeNode* theRoot)
{
if (theRoot != NULL)
{
// call the traversal function on the left child
postOrderTraversal(funcToCall,theRoot->left);
// call the traversal function on the right child
postOrderTraversal(funcToCall,theRoot->right);
// print the node
//std::cout << theRoot->data;
}
}
void Tree::preOrderTraversal(const function<void(string)> &funcToCall, treeNode* theRoot)
{
if (theRoot != NULL)
{
// print the node
//cout << theRoot->data;
// call the traversal function on the left child
preOrderTraversal(funcToCall,theRoot->left);
// call the traversal function on the right child
preOrderTraversal(funcToCall,theRoot->right);
}
}
void Tree::copyTree(treeNode* treeCopy,const treeNode * theRoot)
{
/*if (theRoot = NULL)
{
treeCopy = NULL;
}
else
{*/
while (theRoot != NULL)
{
treeCopy = new treeNode;
treeCopy->data = theRoot->data;
copyTree(treeCopy->left, theRoot->left);
copyTree(treeCopy->right, theRoot->right);
}
}
void printTree(string name)
{
cout << name<<','<< endl;
}
|