-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarytree.c
75 lines (61 loc) · 1.35 KB
/
Binarytree.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
#include<stdio.h>
#include<stdlib.h>
struct tree{
int data;
struct tree *left;
struct tree *right;
};
struct tree *newTree(int data)
{
struct tree *root=(struct tree*)malloc(sizeof(struct tree));
root -> data=data;
root -> left =NULL;
root -> right = NULL;
return root;
}
void PreOrder(struct tree *root){
if(root == NULL)
{
return;
}
printf("%d -> ",root -> data);
PreOrder(root -> left);
PreOrder(root -> right);
}
void InOrder(struct tree *root){
if(root == NULL){
return;
}
InOrder(root -> left);
printf("%d -> ",root -> data);
InOrder(root -> right);
}
void PostOrder(struct tree *root){
if(root == NULL)
{
return;
}
PostOrder(root -> left);
PostOrder(root -> right);
printf("%d -> ",root -> data);
}
int main()
{
struct tree *root=newTree(10);
root -> left =newTree(20);
root -> right = newTree(30);
root -> left -> left=newTree(40);
root -> left -> right=newTree(50);
root -> right -> left=newTree(60);
root -> right -> right=newTree(70);
printf("The Tree in PostOrder is : \n");
PostOrder(root);
printf("NULL");
printf("\nThe tree in InOrder is : \n");
InOrder(root);
printf("NULL");
printf("\nThe tree in PreOrder is : \n");
PreOrder(root);
printf("NULL");
return 0;
}