-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtree traversal
111 lines (83 loc) · 2.45 KB
/
tree traversal
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node {
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node
= (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
/* Given a binary tree, print its nodes in inorder*/
void printInorder(struct node* node)
{
if (node == NULL)
return;
/* first recur on left child */
printInorder(node->left);
/* then print the data of node */
printf("%d ", node->data);
/* now recur on right child */
printInorder(node->right);
}
/* Given a binary tree, print its nodes in preorder*/
void printPreorder(struct node* node)
{
if (node == NULL)
return;
/* then print the data of node */
printf("%d ", node->data);
/* first recur on left child */
printPreorder(node->left);
/* now recur on right child */
printPreorder(node->right);
}
/* Given a binary tree, print its nodes in posteorder*/
void printPostorder(struct node* node)
{
if (node == NULL)
return;
/* first recur on left child */
printPostorder(node->left);
/* now recur on right child */
printPostorder(node->right);
/* then print the data of node */
printf("%d ", node->data);
}
/* Driver code*/
int main()
{
struct node* root = newNode(25);
root->left = newNode(15);
root->right = newNode(50);
root->left->left = newNode(10);
root->left->right = newNode(22);
root->left->left->left = newNode(4);
root->left->left->right = newNode(12);
root->left->right->left = newNode(18);
root->left->right->right = newNode(24);
root->right->left = newNode(35);
root->right->right = newNode(70);
root->right->left->left = newNode(31);
root->right->left->right = newNode(44);
root->right->right->left = newNode(66);
root->right->right->right = newNode(90);
// Function call
printf("\nInorder traversal of binary tree is \n");
printInorder(root);
printf("\nPreorder traversal of binary tree is \n");
printPreorder(root);
printf("\nPostorder traversal of binary tree is \n");
printPostorder(root);
return 0;
}