-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprg18.cpp
More file actions
96 lines (96 loc) · 1.46 KB
/
Copy pathprg18.cpp
File metadata and controls
96 lines (96 loc) · 1.46 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
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
//binarray
#include<iostream>
#define NUMNODE 100
using namespace std;
struct treearr
{
int data;
int flag;
}node[NUMNODE];
class binarray
{
public: void insert(int num);
void makenode(int n);
void display();
void setleft(int data,int p);
void setright(int data,int p);
};
void binarray::insert(int num)
{
int n;
int p,q;
cin>>n;
makenode(n);
for(int i=1;i<num;i++){
cin>>n;
p=0;
q=0;
while(q<NUMNODE && node[q].flag==1 && n!=node[p].data){
p=q;
if(n<node[p].data)
q=2*p+1;
else
q=2*p+2;
}
if(n<node[p].data)
setleft(n,p);
else
setright(n,p);
}
}
void binarray::setleft(int data,int p)
{
int q;
q=2*p+1;
if(q>=NUMNODE)
cout<<"Array overflow";
else if(node[q].flag)
cout<<"invalid location";
else{
node[q].data=data;
node[q].flag=1;
}
}
void binarray::setright(int data,int p)
{
int q;
q=2*p+2;
if(q>=NUMNODE)
cout<<"Array overflow";
else if(node[q].flag)
cout<<"invalid location";
else{
node[q].data=data;
node[q].flag=1;
}
}
void binarray::makenode(int n)
{
node[0].data=n;
node[0].flag=1;
for(int i=1;i<NUMNODE;i++)
node[i].flag=0;
}
void binarray::display()
{
if(node[0].flag==0)
cout<<"No elements";
else{
for(int i=0;i<NUMNODE;i++){
if(node[i].flag)
cout<<node[i].data<<" ";
else
cout<<"0 ";
}
}
}
main()
{
binarray ba;
int num;
cout<<"Enter the number of elements to be inserted into the tree: ";
cin>>num;
cout<<"Enter n elements:\n";
ba.insert(num);
ba.display();
}