#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; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)