about cpp.sh
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct node_tag
{
    int data;
    struct node_tag* left;
    struct node_tag* right;
} Tree;
void insert(Tree** rt, int num)
{
    Tree* tmp;
    if (*rt == NULL)
    {
        tmp = (Tree*)malloc(sizeof(Tree));
        if (tmp == NULL)
        {
            fprintf(stderr, "malloc error ");
            exit(1);
        }
        tmp->data = num;
        *rt = tmp;
    }
    else
    {
        if (num > (*rt)->data) {
            insert(&(*rt)->right, num);
        }    
        else {
            insert(&(*rt)->left, num);
        }
         
    }
}
void print_nodes(Tree* root)
{
    if (root == NULL)
    {
        return;
    }
    if (root->left != NULL)
    {
        print_nodes(root->left);
    }
    printf("data= %d\n", root->data);
    if (root->right != NULL)
    {
        print_nodes(root->right);
    }
}
int main(int argc, char** argv)
{
    Tree* root = NULL;
    int arr[] = {
        415,
        456,
        56,
        156,
        51,
        21,
        54,
        3,
        15,
        651,
    };
    int length;
    length = sizeof(arr) / sizeof(arr[0]);
    for (int i = 0; i < length; i++)
    {
        insert(&root, arr[i]);
    }
    print_nodes(root);
    return 0;
}
RunShort URL: cpp.sh/2vax5
  • options
  • compilation
  • execution
  • Standard
  • Warnings
  • Optimization level
  • Standard Input
C++ Shell, 2014-2015