Click here to Skip to main content
15,897,032 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Design and implement a class for binary trees, following the specification at the following URL http://www.cs.colorado.edu/~main/chapter10/bt_class.h
Your design should use a dynamic array to hold the data from the nodes in the same way that a complete binary tree is usually represented. However, these binary trees do not need to be complete. Instead, you should have a second private member variable that is a dynamic array of Boolean values called is_present. The is_present array indicates which nodes actually exist in the tree. For example, if the tree has a root node, then location is_present[0] is true. If the root has a left child, then is_present[1] is true. If the root has a right child, then is_present[2] is true.

here is my code and i need someone to help me correct it :
C++
#include <<iostream>>
using namespace std;

    class binary_tree
    {
    public:
        binary_tree( );
        void create_first_node(const int entry);
        void shift_to_root( );
        void shift_up( );
        void shift_left( );
        void shift_right( );
        void change(const int  entry);
        void add_left(const int entry);
        void add_right(const int  entry);
        int size( );
        void print();
        int retrieve( ) ;
        bool has_parent( ) ;
        bool has_left_child( ) ;
        bool has_right_child( ) ;
    private:
        int no_leaves;
        int current_node;
        int* BT;
        bool* is_present;
    };

binary_tree::binary_tree( ){
        no_leaves=0;
        current_node=0;
        BT=NULL;
        is_present=false;
}
void binary_tree::create_first_node(const int entry)
{
            //tree must be empty to insert the first node
        if (size()==0){
                BT=new int [10];
                BT[0]=entry;
                cout<<BT[0]<<" is the first node"<<endl; //creates new node and inserts as root ptr
                is_present=new bool[true];
                is_present[0]=true;
                //update current node
                no_leaves++;

            } //tree has one node
        else return;
}

void binary_tree::shift_to_root( ){
        if(size()>0)
        current_node=0;
}
void binary_tree::shift_up( ){
        if(has_parent()==true){
        current_node=current_node/2;
        }
        else return;

}
void binary_tree::shift_left( )
{
            if( has_left_child() == true)
            { //must have left child to shift left

            //current node is now the left child of the original current node
             current_node=2*current_node+1;
            }
            else return;

}
void binary_tree::shift_right( )
{
            if(has_right_child() == true){//must have right child to shift right
            //current node is now the right child of original current node
            current_node=2*current_node+2;
            }
                else return;

}
bool binary_tree::has_left_child( )
{
        if( size()>0 && (2*current_node+1)!=0)
           {
               //current node has a left child
               is_present[2*current_node+1]=true;
               return true;
           }
            else
                //current node does not have left child
                return false;
}

bool binary_tree::has_right_child( )
{
        if( size()>0 && (2*current_node+2)!=0)
        {
               //current node has right child
               is_present[2*current_node+2]=true;
               return true;
        }
            else
                //current node does not have right child
                return false;
}

bool binary_tree::has_parent( )
{

            if( size() > 0 && current_node==0)
            {
                //root node does not have a parent

                return false;
            }
            else{
                is_present[current_node/2]=true;
                //all other nodes have parents
                return true;
            }

        }
void binary_tree::add_left(const int entry)
        {
            //tree must be nonempty
            //current node must not have left child
            if( size()> 0 && has_left_child()==true){
            //create new left child node
            BT[2*current_node+1]= entry;
            int e=entry;
            cout<<e<<" is added to the left"<<endl;
            //update number of nodes in tree
            no_leaves++;

            }
            else cout<<" nothing changed"<<endl;

        }

void binary_tree::add_right(const int entry)
{
            //tree must be nonempty
            //current node cannot have right child
            if( size()> 0 && has_right_child()==true){
                BT[2*current_node+2]= entry;
                int e=entry;
                cout<<e<<" is added to the right"<<endl;
                //update number of nodes in tree
                no_leaves++;
            }
            else cout<<"nothing changed"<<endl;
}
void binary_tree::change(const int entry)
{
            if(size() > 0){ //must have nonempty tree
            //the current node's data is changed to the new entry
            BT[current_node]=entry;
            }
}

int binary_tree::size( )
{
            //return number of nodes in tree
            return no_leaves;
}
int binary_tree::retrieve( )
{
            if( size()> 0){
                cout<<BT[current_node]<<" value of the current node"<<endl;
                return BT[current_node];
            //returns data from current node
            }
        else return 0;

}
void binary_tree:: print(){
        cout<<"BT array: ";
        for(int i=0;i<size();i++){>
                cout<<BT[i]<<" ";
        }
        cout<<endl;
        cout<<"is_present array: ";
        for(int j=0;j<size();j++){>
                cout<<is_present[j]<<" ";
        }

}

int main (void){
        binary_tree b1;
        b1.create_first_node(3);
        b1.add_left(2);
        b1.shift_left();
        b1.retrieve();

        b1.print();
        return 0;
}
Posted
Updated 19-May-15 10:51am
v3
Comments
Matt T Heffron 19-May-15 16:53pm    
Here is where it is important to learn how to use the debugger!
That is a critical skill that you must master if you have any expectation of actually being able to write anything beyond the most trivial programs.

1 solution

Well, good luck with your own homework.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900