Diameter of tree
Jan 2, 2019 at 5:35pm UTC
Question-
https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/tutorial/
at the bottom
My diameter is incorrect but close to actual diameter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include<iostream>
using namespace std;
struct node
{
int val;
node* left;
node* right;
};
void insert(node* &newnode,int data)
{
newnode=new node;
newnode->val=data;
newnode->left=NULL;
newnode->right=NULL;
}
int diameter=0;
int dia(node* current,int ans)
{
if (current)
{
//cout<<current->val<<endl;
int ld=dia(current->left,ans+1);
int rd=dia(current->right,ans+1);
if (ld+rd+1>diameter)
diameter=ld+rd+1;
if (ld>rd)
return ld;
else
return rd;
}
else
return ans-1;
}
int main()
{
node *root=NULL,**ptr;
int no,x;
cin>>no>>x;
insert(root,x);
for (int i=0;i<no-1;i++)
{
ptr=&root;
string str;
int temp;
cin>>str;
cin>>temp;
for (int j=0;str[j]!='\0' ;j++)
{
if (str[j]=='L' )
{
if ((*ptr)->left==NULL)
insert((*ptr)->left,0);
ptr=&((*ptr)->left);
}
else
{
if ((*ptr)->right==NULL)
insert((*ptr)->right,0);
ptr=&((*ptr)->right);
}
}
insert((*ptr),temp);
}
dia(root,0);
cout<<diameter;
return 0;
}
Jan 3, 2019 at 6:53am UTC
Didn't ne555 tell you how to do it in your previous post that you've apparently abandoned?
Jan 3, 2019 at 6:53am UTC
Get rid of the messy global variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int dia(node* current)
{
if (current)
{
//!! diameter is whatever the sub-trees are, +1
int ld=dia(current->left)+1;
int rd=dia(current->right)+1;
if (ld>rd)
return ld;
else
return rd;
}
else
return 0; //!! empty tree is zero diameter
}
Jan 3, 2019 at 6:56am UTC
@salem c, It looks like he's building the tree incorrectly since he's "inserting" extra nodes.
Jan 3, 2019 at 7:24am UTC
@dutch, I was rather assuming a sane tree, oh well.
Jan 3, 2019 at 2:15pm UTC
Is the insertion correct now?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include<iostream>
using namespace std;
struct node
{
int val;
struct node* left;
struct node* right;
};
node* insert(int data)
{
node* newnode=new node;
newnode->val=data;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
int diameter=0;
int dia(node* current)
{
if (current)
{
int ld=dia(current->left);
int rd=dia(current->right);
//cout<<current->val<<" "<<ld<<" "<<rd<<endl;
if (ld+rd+1>diameter)
diameter=ld+rd+1;
if (ld>rd)
return ld+1;
else
return rd+1;
}
else
return 0;
}
int main()
{
int no,x;
cin>>no>>x;
node *root,**ptr;
root=insert(x);
for (int i=0;i<no-1;i++)
{
ptr=&root;
string str;
int temp;
cin>>str;
cin>>temp;
for (int j=0;str[j]!='\0' ;j++)
{
if (str[j]=='L' )
{
if ((*ptr)->left==NULL)
(*ptr)->left=insert(0);
ptr=&((*ptr)->left);
}
else
{
if ((*ptr)->right==NULL)
(*ptr)->right=insert(0);
ptr=&((*ptr)->right);
}
}
(*ptr)=insert(temp);
}
dia(root);
cout<<diameter;
}
Jan 3, 2019 at 9:01pm UTC
not quite
consider this input
that sould give you 42 -> 54 -> 13 (all left children) but your code produces only 42 -> 54
at line 63 you are at the node described by the string, but instead of simply set its value you create a new node
again, create a function for printing the tree (I'll suggest preorder)
Jan 4, 2019 at 12:00pm UTC
ohhh Now I understand.
When I reached the last position, I created a 0 node at the position but also an extra node.
I replaced line 63 by
(*ptr)->val=temp;
Topic archived. No new replies allowed.