augustusmith
BAN USER{
/*Check if the binary tree is a full binary tree
Full binary tree: When all the nodes have no children or two children.*/
#include<iostream>
#include<conio.h>
using namespace std;
struct BinaryTree
{
int data;
struct BinaryTree *left;
struct BinaryTree *right;
};
typedef struct BinaryTree node;
node* insert(node *r,node *z)
{
if(!r)
return z;
if(z->data < r->data)
r->left=insert(r->left,z);
else
r->right=insert(r->right,z);
return r;
}
bool isFull(node *r)
{
if(!r)
return true;
bool lf=isFull(r->left);
bool rf=isFull(r->right);
if(r->left || r->right)
{
if(!r->left || !r->right)
return false;
else
return (lf && rf);
}
else
return (lf && rf);
}
int main()
{
int i,h=0;
node *root=0,*newnode;
int a[10]={5,9,7,3,1,4,12,13,0,6};
for(i=0;i<7;i++)
{
newnode=new node;
newnode->data=a[i];
newnode->left=newnode->right=0;
root=insert(root,newnode);
}
cout<<isFull(root);
}
}
- augustusmith July 07, 2014are we suppose to change links or just swapping the data would be enough?
- augustusmith June 25, 2014here is my implementation in c++
{{#include<iostream>
#include<conio.h>
using namespace std;
struct LinkedList
{
int data;
struct LinkedList *next;
};
typedef struct LinkedList node;
void modify(node* &h,node *t,bool &f)
{
if(!t)
return;
modify(h,t->next,f);
if(!f && h!=t)
{
h->data=t->data-h->data;
if(h->next==t || h->next->next==t)
f=1;
h=h->next;
}
}
int main()
{
node *head,*t;
int i;
bool f=0;
for(i=1;i<=6;i++)
{
if(i==1)
{
t=new node;
head=t;
}
else
{
t->next=new node;
t=t->next;
}
t->data=i;
}
t->next=0;
t=head;
while(t)
{
cout<<t->data<<"\t";
t=t->next;
}
cout<<"\n\n";
t=head;
modify(t,t,f);
t=head;
while(t)
{
cout<<t->data<<"\t";
t=t->next;
}
cout<<"\n\n";
getch();
}
}
this is to check if binary tree is full
- augustusmith July 07, 2014