-
Notifications
You must be signed in to change notification settings - Fork 0
/
LabExam1.cpp
99 lines (70 loc) · 1.11 KB
/
LabExam1.cpp
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
//MIN HEAP
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
int tree[100],n=-1;
void insert(int item)
{
int par;
n++;
int ptr=n;
while(ptr>0)
{
par=(ptr-1)/2;
if(item>tree[par])
{
tree[ptr]=item;
return;
}
else
{
tree[ptr]=tree[par];
ptr=par;
}
}
tree[ptr]=item;
}
void display()
{
int k;
for(k=0;k<=n;k++)
printf("%d \t",tree[k]);
}
int height(int root)
{
if(tree[root]==-1)
return 0;
int lh,rh;
lh=height(root*2+1);
rh=height(root*2+2);
if(lh>rh)
return ++lh;
else
return ++rh;
}
int main()
{
int i=0;
int choice,item;
for(;i<100;i++)
tree[i]=-1;
while(1)
{
printf("Enter your choice : \n 1. Insert element \n 2.Display the existing tree \n 3.Find the height \n 4.Exit \n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the element to be inserted \n");
scanf("%d",&item);
insert(item);
printf("Item inserted \n");
break;
case 2: display();
break;
case 3: printf("Height : %d ",height(0));
break;
case 4: exit(0);
default: printf("Invalid choice. Pick again!!! \n");
}
}
}