-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryTreeCreation.c
81 lines (61 loc) · 1.7 KB
/
binaryTreeCreation.c
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
#include<stdio.h>
#include<stdlib.h>
//Binary tree has two parts - left and right along with the value of the element its on
struct node {
int val;
struct node *left;
struct node *right;
};
//Prints the nodes until it reaches null
void treePrint(struct node *root) {
if (root != NULL) {
//Recursively goes to left and right elements
treePrint(root->left);
printf("%d\n", root->val);
treePrint(root->right);
}
}
//Inserts a node
struct node* leafInsert(struct node* root, int x)
{
//If the root is null, it inserts the root element
if (root == NULL) {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
//Sets root value to x and the left right values to NULL
temp->val = x;
temp->left = NULL;
temp->right = NULL;
return temp;
}
//If x is smaller, it goes to left
else if (x < root->val) {
root->left = leafInsert(root->left, x);
}
//If x is bigger, it goes to right
else if (x > root->val) {
root->right = leafInsert(root->right, x);
}
return root;
}
//Main function for input and calling functions
int main() {
struct node *root = NULL;
int x;
printf("Enter the root element : ");
scanf("%d", &x);
root = leafInsert(root, x);
int num;
printf("Enter number of elements you want to insert : ");
scanf("%d", &num);
int A[num];
for (int i=0; i<num; i++) {
int elem;
printf("Enter an element : ");
scanf("%d", &elem);
A[i] = elem;
leafInsert(root, A[i]);
}
treePrint(root);
return 0;
}