-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.cpp
More file actions
157 lines (142 loc) · 2.86 KB
/
3.cpp
File metadata and controls
157 lines (142 loc) · 2.86 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
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
/*Convert given binary tree into threaded binary tree. Analyze time and space complexity of the algorithm. */
#include<iostream>
using namespace std;
class node
{
public:
int data;
node *right,*left;
int rbit,lbit;
};
class tbt
{
public:
node *root,*head;
tbt()
{
root=head=NULL;
}
void preorder();
void inorder();
node* create()
{
node *temp,*newnode;
int flag;
char ans;
head=new node();
head->right=head->left=head;
head->data=999;
head->lbit=head->rbit=1;
root=new node();
cout<<"Enter root node = ";
cin>>root->data;
root->left=root->right=head;
root->lbit=root->rbit=1;
do{
flag=0;
newnode=new node();
cout<<"\nEnter data = ";
cin>>newnode->data;
newnode->lbit=newnode->rbit=1;
temp=root;
while(flag==0)
{
if(newnode->data<temp->data)
{
if(temp->lbit==1)
{
newnode->left=temp->left;
temp->left=newnode;
temp->lbit=0;
newnode->right=temp;
flag++;
}
else{
temp=temp->left;
}
}
else if(newnode->data>temp->data)
{
if(temp->rbit==1)
{
newnode->right=temp->right;
temp->right=newnode;
temp->rbit=0;
newnode->left=temp;
flag++;
}
else{
temp=temp->right;
}
}
else{
cout<<"\ndata already present!!!!";
flag++;
}
}
cout<<"\ndo you want to continue (Y/y)";
cin>>ans;
}while(ans=='Y' || ans=='y');
return root;
}
};
void tbt::preorder()
{
node *temp;
int flag=0;
temp=root;
cout<<"\nPreorder = ";
while(temp!=head)
{
if(flag==0)
cout<<" "<<temp->data;
if(temp->lbit==0 && flag==0)
temp=temp->left;
else if(temp->rbit==0)
{
temp=temp->right;
flag=0;
}
else
{
temp=temp->right;
flag=1;
}
}
cout<<"\n";
}
void tbt::inorder()
{
node *temp;
int flag=0;
temp=root;
cout<<"\nInorder = ";
while(temp!=head)
{
if(temp->lbit==1 && flag==0)
temp=temp->left;
else
{
cout<<" "<<temp->data;
if(temp->rbit==1)
{
temp=temp->right;
flag=0;
}
else
{
temp=temp->right;
flag=1;
}
}
}
cout<<"\n";
}
int main()
{
node *root;
tbt obj;
root=obj.create();
obj.preorder();
obj.inorder();
}