-
Notifications
You must be signed in to change notification settings - Fork 1
/
q2.c
96 lines (86 loc) · 1.65 KB
/
q2.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
/***********************************************************
Program to find number of nodes in a binary tree
Author: leonatwork(Noel Aby Das)
**********************************************************/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *lptr,*rptr;
};
struct node *root=NULL;
struct node *getNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = item;
temp->lptr = temp->rptr = NULL;
return temp;
}
struct node* insert(struct node* node, int data)
{
if (node == NULL)
return getNode(data);
if (data <= node->data)
node->lptr = insert(node->lptr, data);
else if (data > node->data)
node->rptr = insert(node->rptr, data);
return node;
}
int nodeno(struct node* node)
{
if (node==NULL)
return 0;
else
return nodeno(node->lptr) + 1 + nodeno(node->rptr);
}
void preorder(struct node *root)
{
if (root != NULL)
{
printf("%d -> ", root->data);
preorder(root->lptr);
preorder(root->rptr);
}
}
int main()
{
int ch;
while(1)
{
printf("\nMenu\n1.Insert\n2.Number of nodes\n3.Preorder\n4.Exit\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
int n;
printf("\nEnter data : ");
scanf("%d",&n);
if(root==NULL)
root= insert(root,n);
else
insert(root,n);
}
break;
case 2:
{
printf("No. of nodes in the tree : %d \n",nodeno(root));
}
break;
case 3:
{if(root==NULL)
printf("Empty tree\n");
else
{
printf("Preorder : \n");
preorder(root);
printf("\n");
}}
break;
case 4:
exit(1);
}
}
return 0;
}