-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASSIGN2.C
More file actions
70 lines (66 loc) · 1.04 KB
/
ASSIGN2.C
File metadata and controls
70 lines (66 loc) · 1.04 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
#include<stdio.h>
#include<conio.h>
struct Node
{
int info;
struct Node *left;
struct Node *right;
};
typedef struct Node* node;
node getnode()
{
node t=(node)malloc(sizeof(struct Node));
return (t);
}
node insert(int,node);
void postorder(node root);
void main()
{
int n,i,item;
node root=NULL;
clrscr();
printf("\n How many nodes \n");
scanf("%d",&n);
printf("\nEnter %d nodes \n",n);
for(i=0;i<n;i++)
{
scanf("%d",&item);
root=insert(item,root);
}
printf("\n Traversal of given tree in post-order is \n");
postorder(root);
getch();
}
node insert(int item,node root)
{
node temp,cur,prev;
temp=getnode();
temp->info=item;
temp->left=temp->right=NULL;
if(root==NULL)
{
return temp;
}
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(item<cur->info)
cur=cur->left;
else
cur=cur->right;
}
if(item<prev->info)
prev->left=temp;
else prev->right=temp;
return root;
}
void postorder(node root)
{
if(root==NULL)
return;
postorder(root->left);
postorder(root->right);
printf("%d\t",root->info);
}