Diameter of tree

closed account (1vf9z8AR)
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;
}
Didn't ne555 tell you how to do it in your previous post that you've apparently abandoned?
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
}

@salem c, It looks like he's building the tree incorrectly since he's "inserting" extra nodes.
@dutch, I was rather assuming a sane tree, oh well.
closed account (1vf9z8AR)
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;
}
not quite
consider this input
3 42
LL
13
L
54
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)
closed account (1vf9z8AR)
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.