-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree2.c
120 lines (107 loc) · 1.68 KB
/
tree2.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*binary threaded tree application */
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct tree{
int info;
struct tree *left;
struct tree *right;
int thread;
};
typedef struct tree node;
void inorder(node *,int ct);
void main()
{
node *root,*temp,*prev1,*temp2;
int tem,ct=0;
char more;
root=(node *)malloc(sizeof(node));
clrscr();
gotoxy(5,5);
cprintf("Enter the root element : ");
scanf("%d",&root->info);
root->left=NULL;
root->right=NULL;
root->thread=1;
temp=root;
prev1=root;
while(1)
{
clrscr();
gotoxy(5,5);
ct++;
cprintf("Enter the element: ");
scanf("%d",&tem);
while(root!=NULL && prev1->thread==0)
{
if(tem>=root->info)
{
prev1=root;
root=root->right;
}
else if(tem<root->info)
{
prev1=root;
root=root->left;
}
}
root=(node *)malloc(sizeof(node));
if(prev1->info>tem)
{
root->info=tem;
prev1->left=root;
root->right=prev1;
root->left=NULL;
root->thread=1;
}
else if(prev1->info<=tem)
{
temp2=prev1->right;
prev1->thread=0;
root->info=tem;
root->left=NULL;
prev1->right=root;
root->thread=1;
root->right=temp2;
}
gotoxy(5,7);
cprintf("any other element Y/N ");
fflush(stdin);
more=getch();
if(toupper(more)=='N')
{
break;
}
root=temp;
}
inorder(temp,ct);
getch();
}
void inorder(node *start,int ct)
{
node *p,*q;
p=start;
q=start;
do
{
q=NULL;
while(p!=NULL)
{
q=p;
p=p->left;
}
if(q!=NULL)
{
printf("\n%d %d",q->info,q->thread);
ct--;
p=q->right;
while(q->thread && p!=NULL)
{
printf("\n%d %d",p->info,p->thread);
ct--;
q=p;
p=p->right;
}
}
}while(q!=NULL);
}