Can anyone tell me what is wrong with this Queue template? My program keeps locking up.
#include<iostream>
#include<fstream>
#include<iomanip>
#include<queue>
#include"BSTTreeAndyJackson.cpp"
using namespace std;
//template<class Type>
int main()
{
btree bt;
node nd;
int num=0;
char chr;
int count=0;
fin.open("input.txt");
fout.open("output1.txt");
// ifstream fin;
// ofstream fout;
fin>>num;
fin>>chr;
while(!fin.eof())
{
count++;
fout<<"Step: "<<count<<" "<<"Data Value = "<<num<<" "<<
"Activity Signal = "<<chr<<endl<<endl;
if(chr == 'A')
{
bt.insert(num);
}
else if(chr == 'D')
{
//bt.deletenode();
}
bt.Output("InOrderTranverse = ");
bt.InOrderTranverse();
fout<<endl<<endl;
bt.Output("PreOrder Tranverse = ");
bt.preOrderTranverse();
fout<<endl<<endl;
bt.Output("PostOrder Tranverse = ");
bt.PostOrderTranverse();
fout<<endl<<endl;
bt.Output("LevelOrderTranverse = ");
//bt.LevelOrder();
fout<<endl<<endl;
bt.Output("Total number of Nodes in the tree = ");
fout<<endl<<endl<<endl;
fin>>num;
fin>>chr;
}
return 0;
}
#include"BSTTreeAndyJackson.h"
#include<iostream>
ifstream fin;
ofstream fout;
//Constructor
///////////////////////////////////////////////////////////////////////
btree::btree()
//Post: root == NULL
{
root=NULL;
}
//Destructor
//////////////////////////////////////////////////////////////////////
btree::~btree()
//Post: Destroy all the nodes and free up the memorey.
{
destroy_tree();
}
//Delete Node
//////////////////////////////////////////////////////////////////////
void btree::deletenode(node *leaf)
{
if(leaf!=NULL){
delete root;
//destroy_tree(leaf->left);
//destroy_tree(leaf->right);
}
}
//Delete Node
//////////////////////////////////////////////////////////////////////
void btree::deletenode()
//Pre: Input into root
//Post: If root != NULL the call the Tranverse function
{
if(root != NULL)
{
deletenode(root);
}
else
return;
}
//Destriy tree function
//////////////////////////////////////////////////////////////////////
void btree::destroy_tree(node *leaf)
//Pre: The tree is built
//Post: Destroy all the nodes of the tree
{
if(leaf!=NULL)
{
destroy_tree(leaf->left);
destroy_tree(leaf->right);
delete leaf;
}
}
//Destroy Tree
//////////////////////////////////////////////////////////////////////
void btree::destroy_tree()
//Pre: If the root is not zero
//Post: Call the destroy tree function.
{
if(root != NULL)
destroy_tree(root);
else
return;
}
//Insert Tree
//////////////////////////////////////////////////////////////////////
void btree::insert(int key, node *leaf)
//Pre: Pointer != Null
//Post Assign the value to left or right of the tree
{
if(key< leaf->key_value)
{
if(leaf->left!=NULL)
insert(key, leaf->left);
else
{
leaf->left=new node;
leaf->left->key_value=key;
leaf->left->left=NULL; //Sets the left child of the child node to null
leaf->left->right=NULL; //Sets the right child of the child node to null
}
}
else if(key>=leaf->key_value)
{
if(leaf->right!=NULL)
insert(key, leaf->right);
else
{
leaf->right=new node;
leaf->right->key_value=key;
leaf->right->left=NULL; //Sets the left child of the child node to null
leaf->right->right=NULL; //Sets the right child of the child node to null
}
}
}
///////////////////////////////////////////////////////////////////////
void btree::insert(int key)
//Pre: If root != NULL
//Post: Assign the value to the left of the tree or the right of the tree
{
if(root!=NULL)
insert(key, root);
else
{
root=new node;
root->key_value=key;
root->left=NULL;
root->right=NULL;
}
}
//////////////////////////////////////////////////////////////////////
void btree::InOrderTranverse(node *leaf)
//Pre Pointer points to the root of the binary tree
//Post: Each node of the tree has been visited
{
if(leaf->left != NULL)
InOrderTranverse(leaf->left);
fout<<leaf->key_value<< " ";
if(leaf->right !=NULL)
InOrderTranverse(leaf->right);
}
//////////////////////////////////////////////////////////////////////
void btree::InOrderTranverse()
//Pre: Input into root
//Post: If root != NULL the call the Tranverse function
{
if(root!=NULL)
InOrderTranverse(root);
else
return;
}
//////////////////////////////////////////////////////////////////////
void btree:reOrderTranverse(node *leaf)
//Pre Pointer points to the root of the binary tree
//Post: Each node of the tree has been visited
{
if(leaf != NULL){
fout<<leaf->key_value<<" ";
preOrderTranverse(leaf->left);
preOrderTranverse(leaf->right);
}
}
///////////////////////////////////////////////////////////////////////
void btree:reOrderTranverse()
//Pre: Input into root
//Post: If root != NULL the call the Tranverse function
{
if(root!=NULL)
preOrderTranverse(root);
else
return;
}
//PostOrder
//////////////////////////////////////////////////////////////////////
void btree::PostOrderTranverse()
//Pre: Input into root
//Post: If root != NULL the call the Tranverse function
{
if(root!=NULL)
PostOrderTranverse(root);
else
return;
}
//Post Order Tranverse
//////////////////////////////////////////////////////////////////////
void btree::PostOrderTranverse(node *leaf)
//Pre Pointer points to the root of the binary tree
//Post: Each node of the tree has been visited
{
if(leaf != NULL){
PostOrderTranverse(leaf->left);
PostOrderTranverse(leaf->right);
fout<<leaf->key_value<<" ";
}
}
template<class node>
void btree::LevelOrder()
{
Queue<node>q;
node *leaf = root;
while(leaf){
cout<<leaf->key_value<<endl;
if(leaf->left)q.Add(leaf->left);
if(leaf->right)q.Add(leaf->right);
node=*q.Delete(node);
}
}
//Output Function
//////////////////////////////////////////////////////////////////////
void btree::Output(const char prompt[])
{
fout<<prompt;
}
#include<fstream.h>
struct node;
typedef node *Nodeptr;
struct node
{
int key_value;
node *left;
node *right;
node *leaf;
};
//template<class node>
class btree
{
public:
btree();
~btree();
void insert(int key);
void InOrderTranverse();
void preOrderTranverse();
void PostOrderTranverse();
void destroy_tree();
void deletenode();
void Output(const char prompt[]);
void LevelOrder();
private:
void insert(int key, node *leaf);
void InOrderTranverse(node *leaf);
void preOrderTranverse(node *leaf);
void PostOrderTranverse(node *leaf);
void destroy_tree(node *leaf);
void deletenode(node *leaf);
node *root;
};