Why is my deletenode() function locking up?

This is a discussion on Why is my deletenode() function locking up? within the C++ Programming forums, part of the General Programming Boards category; I have two problems with this BST. The first is my delete node function locks up and I an not ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Why is my deletenode() function locking up?

    I have two problems with this BST. The first is my delete node function
    locks up and I an not sure why. The wecone is I ave to use a template
    to do a levelorder Tranverse and I don't have a clue about templates
    and what code should go where. The way I have the template code I get a
    unresolved external error. Can anyone give me a clue on this? How do I
    input the text file into the template?

    #include<iostream>
    #include<fstream>
    #include<iomanip>

    #include"BSTTreeAndyJackson.cpp"



    using namespace std;

    //template <int>

    int main()
    {
    btree bt;
    int num=0;
    char chr;
    int count=0;

    fin.open("input.txt");
    fout.open("output1.txt");


    /*struct link<int> {
    link* l, r;
    int num;
    }


    template <class int>
    void traverse(link<T> h, void visit(link<T>)) // perhaps visit should take a T (the type of a data) rather than a link<T> (the type of link)
    { QUEUE q(max);
    q.put(h);
    while (!q.empty())
    {
    visit(h = q.get());
    if (h->l != 0) q.put(h->l);
    if (h->r != 0) q.put(h->r);
    }
    }

    */

    // ifstream fin;
    // ofstream fout;




    fin>>num;
    fin>>chr;

    while(!fin.eof())

    {

    count++;
    fout<<"Step: "<<count<<" "<<"Data Value = "<<num<<" "<<
    "Activity Signal = "<<chr<<endl;
    if(chr == 'A')
    {
    bt.insert(num);
    }
    else if(chr == 'D')
    {
    bt.deletenode();
    }
    fout << "InOrderTranverse = ";
    bt.InOrderTranverse();
    fout<<endl<<endl;


    fout<<"PreOrder Tranverse = ";
    bt.preOrderTranverse();
    fout<<endl<<endl;

    fout<<"PostOrder Tranverse = ";
    bt.PostOrderTranverse();
    fout<<endl<<endl<<endl;

    fout<<"Total number of Nodes is the tree = ";

    fin>>num;
    fin>>chr;

    }



    return 0;
    }

    Implementation file
    #include"BSTTreeAndyJackson.h"
    #include<iostream>

    ifstream fin;
    ofstream fout;

    //Constructor
    ///////////////////////////////////////////////////////////////////////
    btree::btree()
    {
    root=NULL;
    }
    //Destructor
    //////////////////////////////////////////////////////////////////////
    btree::~btree()
    {

    destroy_tree();

    }
    //Delete Node
    //////////////////////////////////////////////////////////////////////
    void btree::deletenode(node *leaf)
    {
    delete leaf;
    }
    //Delete Node
    //////////////////////////////////////////////////////////////////////
    void btree::deletenode()
    {
    if(root != NULL)
    deletenode(root);

    else

    return;

    }
    //Destriy tree function
    //////////////////////////////////////////////////////////////////////
    void btree::destroy_tree(node *leaf)
    {
    if(leaf!=NULL)
    {
    destroy_tree(leaf->left);
    destroy_tree(leaf->right);
    delete leaf;
    }
    }
    //Destroy Tree
    //////////////////////////////////////////////////////////////////////
    void btree::destroy_tree()
    {

    if(root != NULL)
    destroy_tree(root);

    else

    return;
    }
    //Insert Tree
    //////////////////////////////////////////////////////////////////////
    void btree::insert(int key, node *leaf)
    {
    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)
    {
    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)
    {

    if(leaf->left != NULL)
    InOrderTranverse(leaf->left);

    fout<<leaf->key_value<< " ";

    if(leaf->right !=NULL)
    InOrderTranverse(leaf->right);
    }
    //////////////////////////////////////////////////////////////////////
    void btree::InOrderTranverse()
    {
    if(root!=NULL)
    InOrderTranverse(root);
    else

    return;
    }
    //////////////////////////////////////////////////////////////////////
    void btree:reOrderTranverse(node *leaf)
    {
    if(leaf != NULL){

    fout<<leaf->key_value<<" ";
    preOrderTranverse(leaf->left);
    preOrderTranverse(leaf->right);
    }
    }

    ///////////////////////////////////////////////////////////////////////
    void btree:reOrderTranverse()
    {
    if(root!=NULL)
    preOrderTranverse(root);
    else

    return;
    }
    //PostOrder
    //////////////////////////////////////////////////////////////////////
    void btree::PostOrderTranverse()
    {
    if(root!=NULL)
    PostOrderTranverse(root);
    else

    return;
    }

    //Post Order Tranverse
    //////////////////////////////////////////////////////////////////////
    void btree::PostOrderTranverse(node *leaf)
    {
    if(leaf != NULL){
    PostOrderTranverse(leaf->left);
    PostOrderTranverse(leaf->right);
    fout<<leaf->key_value<<" ";
    }
    }
    Headerfile
    #include<fstream.h>

    struct node;

    typedef node *Nodeptr;

    struct node
    {
    int key_value;
    node *left;
    node *right;
    node *leaf;
    };


    class btree
    {
    public:
    btree();
    ~btree();
    void insert(int key);
    void InOrderTranverse();
    void preOrderTranverse();
    void PostOrderTranverse();
    void destroy_tree();
    void deletenode();
    //int Print(ofstream& fout) const;

    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);
    //Nodeptr *root; ;
    node *root;

    };

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

    Red face Calling the current function....

    Check the overloaded forms and how the call eachother. I've noticed a lot of things that just don't look right in that area.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21