-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.c
68 lines (68 loc) · 1.17 KB
/
Tree.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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *createNode(int data)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
p->data = data;
p->left = NULL;
p->right = NULL;
return p;
}
void preoder(struct node *root)
{
if (root != NULL)
{
printf("%d ", root->data);
preoder(root->left);
preoder(root->right);
}
}
void postOrder(struct node *root)
{
if (root == NULL)
return;
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->data);
}
void inOrder(struct node *root)
{
if (root == NULL)
return;
inOrder(root->left);
printf("%d ", root->data);
inOrder(root->right);
}
int main()
{
// Parent Node
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
p->data = 5;
p->left = NULL;
p->right = NULL;
struct node *p1 = createNode(7);
struct node *p2 = createNode(9);
struct node *p3 = createNode(10);
struct node *p4 = createNode(12);
p->left = p1;
p->right = p2;
p1->left = p3;
p1->right = p4;
printf("PreOrder\n");
preoder(p);
printf("\n");
printf("PostOrder \n");
postOrder(p);
printf("\n");
printf("InOrder \n");
inOrder(p);
printf("\n");
return 0;
}