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
|
#include <iostream>
#include <stack>
#include <queue>
#include <cstdlib>
using namespace std;
struct BSTNode {
int value;
BSTNode* left;
BSTNode* right;
};
void rInOrder(BSTNode* root);
void rPostOrder(BSTNode* root);
void unbalancedInsert(BSTNode* &head, int v)
{
//empty tree or foundlocation
if(head == NULL)
{
head = new BSTNode;
head->value = v;
head->left = NULL;
head->right = NULL;
}
else if(v < head->value)
{
unbalancedInsert(head->left, v);
}
else if(v > head->value)
{
unbalancedInsert(head->right, v);
}
else
{
cout << "Duplicate ignored" << endl;
}
}
void inOrder(BSTNode* root)
{
rInOrder(root); //calls recursive function
}
void postOrder(BSTNode* root)
{
rPostOrder(root); //calls recursive function
}
void preOrder(BSTNode* root)
{
stack<BSTNode*> Stacey;
BSTNode* top;
BSTNode* pop;
Stacey.push(root);
pop = Stacey.top();
while (!Stacey.empty() ) //while the stack is not empty traverse the tree using preorder traversal
{
top = Stacey.top();
cout << top->value << " ";
if (top->right->value == pop->value) //if the right node was just popped pop again (will eventually take to root)
{
pop = Stacey.top();
Stacey.pop();
}
else if (top->left != NULL && top->left->value != pop->value) //else if the left is not null and was not the last popped node
{ Stacey.push(top->left);}
else if (top->right != NULL) //else if the right node is not null
{ Stacey.push(top->right);}
else //else pop the stack
{
pop = top;
Stacey.pop();
}
}
}
void levelOrder(BSTNode* root)
{
BSTNode* next;
queue<BSTNode*> Quinn;
Quinn.push(root);
while (!Quinn.empty() ) //while the queue is not empty traverse the tree using level order traversal
{
next = Quinn.front();
Quinn.pop();
cout << next->value << " ";
if (next->left) //add left to the queue if not null
Quinn.push(next->left);
if (next->right) //and add right to the queue if not null
Quinn.push(next->right);
}
}
void rInOrder(BSTNode* root)
{
if (root == NULL) //if the passed in pointer is null go up a level
return;
else //else call the left recursive, display, and then call the right recursive
{
rInOrder(root->left);
cout << root->value << " ";
rInOrder(root->right);
}
}
void rPostOrder(BSTNode* root)
{
if (root == NULL) //if the passed in pointer is null go up a level
return;
else //else call the left recursive, call the right recursive, and then display the current
{
rPostOrder(root->left);
rPostOrder(root->right);
cout << root->value << " ";
}
}
int main() {
//create the tree
BSTNode* head = NULL;
unbalancedInsert(head, 54);
unbalancedInsert(head, 22);
unbalancedInsert(head, 17);
unbalancedInsert(head, 41);
unbalancedInsert(head, 36);
unbalancedInsert(head, 30);
unbalancedInsert(head, 74);
unbalancedInsert(head, 76);
unbalancedInsert(head, 26);
unbalancedInsert(head, 27);
unbalancedInsert(head, 45);
unbalancedInsert(head, 82);
cout << endl;
cout << "Ricky to the rescue:" << endl;
cout << "printing inorder: ";
inOrder(head);
cout << endl;
cout<< "Answer: 17 22 26 27 30 36 41 45 54 74 76 82";
cout << endl;
cout << endl;
cout << "printing postorder: ";
postOrder(head);
cout << endl;
cout <<"Answer: 17 27 26 30 36 45 41 22 82 76 74 54 ";
cout << endl;
cout << endl;
cout << endl;
cout << "Stacey saves the day" << endl;
cout << "printing preorder: ";
preOrder(head);
cout << endl;
cout <<"Answer: 54 22 17 41 36 30 26 27 45 74 76 82";
cout << endl;
cout << endl;
cout << "Quiet down, Quinn's in Town" << endl;
cout << "printing levelorder: ";
levelOrder(head);
cout << endl;
cout <<"Answer: 54 22 74 17 41 76 36 45 82 30 26 27";
cout << endl;
return 0;
}
|