-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Postorder.c
144 lines (121 loc) · 3.95 KB
/
Postorder.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/******************************************************************************
Author: @Suvraneel Bhuin
Inorder Traversal of a Binary Tree
Postorder Traversal of a Binary Tree
Definition: Process all nodes of a tree by processing the root, then recursively processing all subtrees
Post-order traversal is one of the multiple methods to traverse a tree. It is mainly used for tree deletion.
Algorithm of Postorder(tree) Traversal
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int key;
struct node *lt;
struct node *rt;
}*root = NULL, *temp = NULL;
//root & temp are made global so that we dont need to pass those again & again
// Function to search the appropriate position to insert the new node
void search(struct node *t)
{
if ((temp->key > t->key) && (t->rt != NULL)) //key > root node move down more right
search(t->rt);
else if ((temp->key > t->key) && (t->rt == NULL)) //if right node NULL, insert key
t->rt = temp;
else if ((temp->key < t->key) && (t->lt != NULL)) //key < root node move down more left */
search(t->lt);
else if ((temp->key < t->key) && (t->lt == NULL)) //if left node NULL, insert key
t->lt = temp;
}
/* To insert a node in the tree */
void insert()
{
int data;
printf("Enter data of node to be inserted : ");
scanf("%d", &data);
temp = (struct node *)malloc(1*sizeof(struct node));
temp->key = data;
temp->lt = temp->rt = NULL; //initialise lt & rt child as null
if (root == NULL)
root = temp;
else
search(root);
}
/* To find the postorder (LRV) traversal */
void postorder(struct node *t)
{
if (root == NULL)
{
printf("No elements in a tree to display ");
return;
}
if (t->lt != NULL) // L (Left)
postorder(t->lt); // ↓ ↓
if (t->rt != NULL) // R (Right)
postorder(t->rt); // ↓ ↓
printf("%d => ", t->key); // V (Visit)
}
void main()
{
int choice;
printf("\n------------------------------------");
printf("\n1 - Insert an element into tree");
printf("\n2 - Postorder Traversal");
printf("\n3 - Exit");
printf("\n------------------------------------");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
printf("Postorder Traversal :\n");
postorder(root);
break;
case 3:
printf("Program terminated\n");
exit(0);
default :
printf("Invalid Choice entered");
break;
}
}
}
/*
Time Complexity : O(n)
Space Complexity: O(h) (where h is the height of the Tree)
Sample Run:
------------------------------------
1 - Insert an element into tree
2 - Postorder Traversal
3 - Exit
------------------------------------
Enter your choice : 1
Enter data of node to be inserted : 12
Enter your choice : 1
Enter data of node to be inserted : 4
Enter your choice : 1
Enter data of node to be inserted : 6
Enter your choice : 1
Enter data of node to be inserted : 9
Enter your choice : 1
Enter data of node to be inserted : 14
Enter your choice : 1
Enter data of node to be inserted : 17
Enter your choice : 1
Enter data of node to be inserted : 3
Enter your choice : 1
Enter data of node to be inserted : 19
Enter your choice : 2
Postorder Traversal :
3 => 9 => 6 => 4 => 19 => 17 => 14 => 12 =>
Enter your choice : 3
Program terminated
*/