Monday, 7 October 2013

Complete Binary Tree Implementation and Program in C++

In my Last post I discussed about Algorithm for searching an element or node in Binary Search Tree . Binary Tree is a non-linear Data Structure which helps in traversing and improving timing complexity when compared to other Data Structures .
In this post I will discuss about Implementation of Complete Binary Tree and Program for Complete Binary tree in C++ .


Also check : Program for Linear search in C++



Implementation of Complete Binary Tree :

Download Source code

Here we discuss 3 major Implementations of Complete Binary Tree .
1. Inserting a Node or Insertion
2. Deleting a Node or Deletion
3. Display the elements in Complete Binary Tree 
    

1. Insertion :

Insert Operation involves inserting a new element or node in Complete Binary Tree . Insertion should be done in such a way that it does not effect the structure of the Tree . First element is inserted in a node known as root node . Next elements are inserted according to the index order of array .
for eg : consider an array a[10] 
     1st element is inserted in a[0]
     2nd element is inserted in a[1] and it goes to maximum insertion of a[9].    
Code for Insertion :

void insert()
{
if(pos<size)
{
cout<<"\n enter element \n";
cin>>a[pos++];
cout<<"\n inserted value successfully \n";
}
else
{
cout<<"\n overflow -- Cannot insert \n";
}
}


2. Deletion :

Deletion implies Deleting a Node from the Complete Binary Tree . Node cannot be completely Deleted if you implement using arrays , Node will be logically deleted . Node can be permanently deleted by implementing complete Binary Tree using LinkedList.
Code for Deletion :

void remove()
{
int i,x;
cout<<"\n enter element to be deleted \n";
cin>>x;
for(i=0;i<size;i++)
{
if(x==a[i])
break;
}
if(i>=size)
cout<<"\n element not found \n";
a[i]=a[pos-1];
a[pos-1]=-1;
pos--;
}

3. Display : 

Display function displays all the elements of Binary Tree . Display helps in displaying the root nodes , child nodes and also leaf nodes .  
Code for Display :      

void display()
{
int i;
for(i=0;i<size;i++)
{
if(2*i+1<size && a[2*i+1]!=-1 && a[2*i+2]!=-1)
{
cout<<"\n Parent is "<<a[i]<<" left is "<<a[2*i+1]<<" right is "<<a[2*i+2]<<"\n";
}
if(a[2*i+1]!=-1 && a[2*i+2]==-1)
{
cout<<"\n Parent is "<<a[i]<<" left is "<<a[2*i+1]<<"\n";
}
if(a[i]!=-1 && a[2*i+1]==-1 && a[2*i+2]==-1)
{
cout<<"\n Leaf node is "<<a[i]<<"\n";
}
}
}

To get the complete code of Complete Binary Tree Download here
If you feel I have missed anything please get it to me through comments . Also Share your ideas and logics with us through comments .  


3 comments:

  1. Nice post with great information and I really enjoyed this post while reading. it is a nice one because it deals with an interesting ideas and information. I like it so much as from the first time i read it, the information that are used here attracted me a lot. They take my attention from the first look, thank you so much for sharing with us this great topic in this great website. You are doing a great work here. I must say you've done a wonderful job by sharing your article with us.

    And yes, please give your comment on my blog too, hope you visit and give me better suggestion.

    Thanks

    ReplyDelete
  2. Hi two questions :

    1. Is the display function doing an inorder traversal .

    2. If yes ? then can u tell how to go about for pre order and post order traversals ?

    PS: good post anyways ..

    ReplyDelete