-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.c
116 lines (111 loc) · 2.35 KB
/
10.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
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *left;
struct node *right;
int RT;
};
typedef struct node *NODE;
NODE leftmost(NODE temp) {
if (temp == 0)
return 0;
while (temp->left != 0)
temp = temp->left;
return temp;
}
void inorder(NODE head) {
NODE temp = leftmost(head);
if (head->left == 0) {
printf("\n No nodes");
return;
}
while (temp != head) {
printf("%d=>", temp->data);
if (temp->RT)
temp = temp->right;
else
temp = leftmost(temp->right);
}
}
NODE getnode(int item) {
NODE newnode = (NODE)malloc(sizeof(struct node));
newnode->left = newnode->right = 0;
newnode->RT = 0;
newnode->data = item;
return newnode;
}
void insert_left(int item, NODE ptr) {
NODE newnode = getnode(item);
ptr->left = newnode;
newnode->right = ptr;
newnode->RT = 1;
}
void insert_right(int item, NODE ptr) {
NODE newnode = getnode(item);
newnode->right = ptr->right;
newnode->RT = 1;
ptr->right = newnode;
ptr->RT = 0;
}
NODE create(int item, NODE head) {
NODE curptr, ptr;
if (head->left == 0) {
insert_left(item, head);
return (head);
}
curptr = head->left;
while (curptr != head) {
ptr = curptr;
if (item < (curptr->data) && curptr->left != 0)
curptr = curptr->left;
else if (item > (curptr->data) && curptr->RT == 0)
curptr = curptr->right;
else
break;
}
if (item < (curptr->data)) {
insert_left(item, ptr);
return (head);
} else if (item > (curptr->data))
insert_right(item, ptr);
return (head);
}
int main() {
int ch, i, n, item;
NODE head;
head = (NODE)malloc(sizeof(struct node));
head->right = head;
head->left = 0;
head->RT = 0;
while (1) {
printf("\n1.Create tree\n2.Inorder\n3.Exit\n");
printf("Enter the choice\n");
scanf("%d", &ch);
switch (ch) {
case 1:
free(head);
head = (NODE)malloc(sizeof(struct node));
head->right = head;
head->left = 0;
head->RT = 0;
printf("Enter num of nodes to create\n");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter %d data\n", i + 1);
scanf("%d", &item);
head = create(item, head);
}
break;
case 2:
inorder(head);
break;
case 3:
exit(0);
default:
printf("Wrong choice\n");
break;
}
}
return 0;
}