binary search tree sorted order

Solutions on MaxInterview for binary search tree sorted order by the best coders in the world

showing results for - "binary search tree sorted order"
Kora
26 Jul 2019
1#include <iostream>
2
3using namespace std;
4class node
5{
6public:
7    int data;
8    node*left;
9    node*right;
10};
11node*getnewnode(int val)
12{
13    node*temp=new node;
14    temp->data=val;
15    temp->left=NULL;
16    temp->right=NULL;
17    return temp;
18}
19node*insertbst(node *root,int val)
20{
21    if(root==NULL)
22    {
23        return getnewnode(val);
24    }
25    if(root->data>val)
26    {
27        root->left=insertbst(root->left,val);
28    }
29    else if(root->data<val)
30    {
31        root->right=insertbst(root->right,val);
32    }
33    return root;
34}
35void inorder(node*root)
36{
37    if(root==NULL)
38    {
39        return;
40    }
41    else
42    {
43        inorder(root->left);
44        cout<<root->data<<" ";
45        inorder(root->right);
46    }
47}
48
49int main()
50{
51    node *root=new node;
52    root=NULL;
53    root=insertbst(root,100);
54    root=insertbst(root,200);
55    root=insertbst(root,50);
56    root=insertbst(root,90);
57    root=insertbst(root,150);
58    inorder(root);
59    return 0;
60}
61