-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.txt
182 lines (161 loc) · 3.54 KB
/
04.txt
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//Convert given binary tree into threaded binary tree. Analyze time and space complexity of
the algorithm.
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
int data;
node *left,*right;
int lbit,rbit;
};
class tbt
{
node *temp=NULL,*t1=NULL,*s=NULL,*head=NULL,*t=NULL;
public:
node *create();
void insert();
node *insuc(node*);
node *inpre(node*);
void dis();
void display(node*);
void thr();
void thread(node*);
};
node *tbt::create()
{
node *p=new(struct node);
p->left=NULL;
p->right=NULL;
p->lbit=0;
p->rbit=0;
cout<<"\n enter the data";
cin>>p->data;
return p;
}
void tbt::insert()
{
temp=create();
if(head==NULL)
{ node *p=new(struct node);
head=p;
head->left=temp;
head->right=head;
head->lbit=1;
head->rbit=0;
temp->left=head;
temp->right=head;
temp->lbit=0;
temp->rbit=0;
}
else
{ t1=head;
t1=t1->left;
while(t1!=NULL)
{ s=t1;
if(((temp->data)>(t1->data))&&t1->rbit==1)
{ t1=t1->right; }
else if(((temp->data)<(t1->data))&&t1->lbit==1)
{ t1=t1->left; }
else
{ break; }
}
if(temp->data>s->data)
{
s->right=temp;
s->rbit=1;
temp->left=inpre(head->left);
temp->right=insuc(head->left);
}
else
{
s->left=temp;
s->lbit=1;
temp->left=inpre(head->left);
temp->right=insuc(head->left);
}
}
}
node *tbt::inpre(node *m)
{
if(m->lbit==1)
{
inpre(m->left);
}
if(m->data==temp->data&&t==NULL)
{ return head; }
if(m->data==temp->data)
{ return t; }
t=m;
if(m->rbit==1)
{ inpre(m->right);
}
}
node *tbt::insuc(node *m)
{
if(m->lbit==1)
{ t=m;
insuc(m->left);
}
if(m->data==temp->data&&t==NULL)
{ return head; }
if(m->data==temp->data)
{ return t; }
if(m->rbit==1)
{ insuc(m->right);
}
}
void tbt::dis()
{ display(head->left);
}
void tbt::display(node *m)
{
if(m->lbit==1)
{ display(m->left); }
cout<<"\n"<<m->data;
if(m->rbit==1)
{ display(m->right); }
}
void tbt::thr()
{ cout<<"\n thread are";
thread(head->left);
}
void tbt::thread(node *m)
{
if(m->lbit==1)
{ thread(m->left); }
if(m->lbit==0||m->rbit==0)
{
cout<<"\n"<<m->data;
}
if(m->rbit==1)
{ thread(m->right); }
}
int main()
{ tbt t; int ch;
while(1)
{
cout<<"\n enter the choice";
cout<<"\n 1.insert data";
cout<<"\n 2.display all data";
cout<<"\n 3.display threaded node";
cout<<"\n 4.exit";
cin>>ch;
switch(ch)
{
case 1:
t.insert();
break;
case 2:
t.dis();
break;
case 3:
t.thr();
break;
case 4: exit(0);
default:
cout<<"\n invalid entry";
}
}
return 0;
}