Binary Search tree

closed account (1vf9z8AR)
Question link-
https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/tutorial/

Issue-
I am new to actual trees. The answer is incorrect, I think it's because all the nodes are not being visited.

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
  #include<iostream>
using namespace std;
struct tree
{
    int val;
    struct tree *left,*right;
};
struct tree* insert(int x)
{
    tree *newnode=new tree;
    newnode->val=x;
    newnode->left=NULL;
    newnode->right=NULL;
    return newnode;
}
int maxdepth(int maxval,tree *node)
{
    if(node->val>0)
    {
        int ld=maxdepth(maxval,node->left);
        int rd=maxdepth(maxval,node->right);
        if(ld+rd>maxval)
            maxval=ld+rd;
        return maxval+1;
    }
    else
        return 0;
}
int main()
{
    int no,root;
    cin>>no>>root;
    tree *ptr,*parent;
    parent=insert(root);
    for(int i=0;i<no-1;i++)
    {
        ptr=parent;
        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);
    }
    cout<<maxdepth(0,parent);
    return 0;
}


This is what I could make from the net and youtube. Also what is the difference
between tree* insert and tree *insert?

Is there a better way to do this? Upon searching why there is no tree stl, I got that maps can be used as tree. But in this question how is it possible?
Last edited on
about the diameter
the empty tree is represented by a null pointer, not by 0 in `val', the condition of the basic case should be if(node)
for the recursion
The diameter of a tree is the number of nodes on the longest path between two leaves in the tree.
you have three situations depending on where are the leaves of the diameter
- both on the left subtree
- both on the right subtree
- one on each subtree
so the check should be something like
1
2
3
4
ld = diameter(node->left)
rd = diameter(node->right)
both = depth(node->left) + depth(node->right) + 1
return max(ld, rd, both)
however, as you need to reach a leaf, `both' is only valid is no depth is 0


about your tree construction
1
2
                if(ptr->left==NULL)
                    ptr->left=insert(0);
good noticing that a parent may appear later than its child on the input
however, when the loops ends you do ptr=insert(temp);. there are two errors there
first, you are creating an extra node
second, this last node is not linked to your tree

> I think it's because all the nodes are not being visited.
do a traversal and check it
closed account (1vf9z8AR)
Future viewers see the solution here-
http://www.cplusplus.com/forum/beginner/248321/
Topic archived. No new replies allowed.