Client file BST TREE

This is a discussion on Client file BST TREE within the C++ Programming forums, part of the General Programming Boards category; This locks up. I am not sure how I should cll my functions in the client file to make the ...

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

    Client file BST TREE

    This locks up. I am not sure how I should cll my functions in the client
    file to make the BST and do the different functions.PreOrder()etc...
    Can someone give me a hint on what I should call?

    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include <cctype> // defines isalpha() function

    #include"TreeImplementation.cpp"

    using namespace std;


    int main()
    {

    node *temp;

    btree bt;
    int num=0;
    char chr;
    int count=0;
    int *leaf;

    ifstream fin("input.txt");
    ofstream fout("output1.txt");

    fin>>num;
    fin>>chr;
    while(fin)
    {
    temp=bt.insert(num);
    //bt.search(num);
    bt.InorderTranverse(temp);
    fout<<num;
    count++;
    fout<<"Step: "<<count<<" "<<"Data Value = "<<num<<" "<<"Activity Signal = "<<chr<<endl;


    fout<<"Preorder Transversal = "<<endl<<endl;
    fout<<"Inorder Transversal = "<<endl<<endl;
    fout<<"Post order Transversal = "<<endl<<endl;
    fout<<"Level by Level Transversal = "<<endl<<endl;
    fout<<"Total Number of nodes = "<<count<<endl<<endl;

    fin>>num;
    fin>>chr;
    }


    return 0;
    }

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


    class btree
    {
    public:
    // node *root;
    btree();
    // ~btree();
    void destroy_tree(node *leaf);
    void insert(int key, node *leaf);
    node *search(int key, node *leaf);
    //node *GetRoot(); {return (root);
    //public:
    void insert(int key);
    node *search(int key);
    //void destroy_tree();
    void InorderTranverse(node *leaf);
    //void preOrderTranverse(node *leaf);
    //void PostOrderTranverse(node *leaf);
    private:
    node *root;
    };
    node *RChild(node *leaf);
    node *LChild(node *leaf);

    Implementation

    #include<iostream.h>
    #include"Treeheader.h"



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


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

    //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
    }
    }
    }


    ///////////////////////////////////////////////////////////////////////

    node *btree::search(int key, node *leaf)
    {
    if(leaf!=NULL)
    {
    if(key==leaf->key_value)
    return leaf;
    if(key<leaf->key_value)
    return search(key, leaf->left);
    else
    return search(key, leaf->right);
    }
    else return 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;
    }
    }


    //////////////////////////////////////////////////////////////////////

    node *btree::search(int key)
    {
    return search(key, root);
    }


    ///////////////////////////////////////////////////////////////////////

    /*void btree::destroy_tree(node *root)
    {
    if(root != NULL)
    destroy_tree(
    destroy_tree(root);
    }
    */

    //////////////////////////////////////////////////////////////////////
    void btree::InorderTranverse(node *leaf)
    {

    if(leaf != NULL)
    InorderTranverse(LChild(leaf));
    cout<<leaf;
    InorderTranverse(RChild(leaf));
    }

    //////////////////////////////////////////////////////////////////////
    node *LChild(node *leaf)
    {
    return (leaf==NULL) ? NULL : leaf->left;
    }

    //////////////////////////////////////////////////////////////////////
    node *RChild(node *leaf)
    {
    return (leaf==NULL) ? NULL : leaf->right;
    }

    //PreOrder Tranverse
    ///////////////////////////////////////////////////////////////////////
    /*void btree:reOrderTranverse(node *leaf)
    {
    if(leaf != NULL){
    cout<<leaf;
    preOrderTranverse(LChild(leaf));
    preOrderTranverse(RChild(leaf));
    }
    }
    //PostOrderTranverse
    ///////////////////////////////////////////////////////////////////////
    void btree::PostOrderTranverse(node *leaf)
    {
    if(leaf != NULL){
    PostOrderTranverse(LChild(leaf));
    PostOrderTranverse(RChild(leaf));
    }
    }

    //////////////////////////////////////////////////////////////////////
    node *GetRoot(); {return (root);
    {


    }*/

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Add another function to your class declaration -

    void InorderTranverse();

    InorderTranverse() definitions -

    Code:
    ////////////////////////////////////////////////////////////////////// 
    void btree::InorderTranverse(node *leaf) 
    { 
    
    if(leaf->left != NULL) 
    InorderTranverse(leaf->left); 
    cout<<leaf->key_value<< " ";
    if(leaf->right !=NULL) 
    InorderTranverse(leaf->right); 
    } 
    
    void btree::InorderTranverse() 
    { 
    if(root!=NULL) 
    InorderTranverse(root); 
    else 
    return;
    
    }
    Get rid of the temp node in main() and just call -

    bt.InorderTranverse();

    Depending on how you want it to print you'll have to mess around with the

    cout<<leaf->key_value<< " ";

    statement.
    zen

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

    So I need to use both functions?

    My code is good up to this point and use both InorderTranverse functions?

    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include <cctype> // defines isalpha() function

    #include"TreeImplementation.cpp"

    using namespace std;


    int main()
    {

    node *temp;

    btree bt;
    int num=0;
    char chr;
    int count=0;
    int *leaf;

    ifstream fin("input.txt");
    ofstream fout("output1.txt");

    fin>>num;
    fin>>chr;
    while(fin)
    {
    bt.insert(num);
    bt.search(num);
    Is my code correct up to this point?

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Yes, if you want to output the Inorder Traversal every time a value is read you could carry it on like this(assuming you've added the code from my last reply) -

    Code:
    int main() 
    { 
    
    	btree bt; 
    	int num=0; 
    	char chr; 
    	int count=0; 
    
    	ifstream fin("input.txt"); 
    	ofstream fout("output1.txt"); 
    
    	fin>>num; 
    	fin>>chr; 
    	
    	while(fin) 
    	{ 
    		bt.insert(num); 
    		//bt.search(num); 
    		bt.InorderTranverse(); 
    		fout<<num; 
    		count++; 
    		fout<<"Step: "<<count<<" "<<"Data Value = "<<num<<" "<<"Activity Signal = "<<chr<<endl; 
    
    		fin>>num; 
    		fin>>chr; 
    
    	}
    main() won't directly call both InorderTranverse functions for the same reason that it doesn't call both insert functions.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM

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