-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.c
More file actions
72 lines (70 loc) · 1.33 KB
/
Copy pathtempCodeRunnerFile.c
File metadata and controls
72 lines (70 loc) · 1.33 KB
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * left;
struct node * right;
};
struct node* createNode(int a)
{
//creating a node pointer
struct node *p;
//allocation memory in the heap
p=(struct node*)malloc (sizeof(struct node));
p->data=a;
p->left=NULL;
p->right=NULL;
return p;
}
void preorder(struct node* p)
{
if(p!=NULL)
{
printf("%d",p->data);
preorder(p->left);
preorder(p->right);
}
}
void postorder(struct node* p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("%d",p->data);
}
}
void inorder(struct node* p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%d",p->data);
inorder(p->right);
}
}
int main()
{
struct node *p=createNode(2);
struct node *p1=createNode(3);
struct node *p2=createNode(5);
struct node *p3=createNode(6);
struct node *p4=createNode(9);
// Finally The tree looks like this:
// 2
// / \
// 3 5
// / \
// 6 9
p->left=p1;
p->right=p2;
p1->left=p3;
p1->right=p4;
preorder(p);
printf("\n");
postorder(p);
printf("\n");
inorder(p);
return 0;
}