-
Notifications
You must be signed in to change notification settings - Fork 0
/
bintree_duplicate.c
157 lines (147 loc) · 3.51 KB
/
bintree_duplicate.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
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
/*
This program initialises a tree data structre such that left child's data is smaller
than parent's data and right child's data is larger than parent's data.
This program checks for data duplicacy and ignores duplicate entries.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct tree_node
{
struct tree_node * lchild ;
struct tree_node * rchild ;
int data ;
}node;
node *root=NULL;
void insert( node * , node *);
void display( node *);
void delete_tree(node *);
void buildtree1by1();
void buildtreebyarray();
void main()
{
buildtreebyarray();
//buildtree1by1();
display(root);
delete_tree(root);
printf("\n");
printf("\n");
}
void buildtree1by1()
{
int i;
do
{
node * new_node ;
new_node = ( node *)malloc(sizeof( node));
printf("Add the value : ");
scanf("%d",&(new_node->data));
if(root==NULL)
{
root = new_node ;
root->lchild = NULL;
root->rchild = NULL;
}
else
{
insert(new_node,root);
}
printf("want to add more [1/0] ");
scanf("%d",&i);
}while(i);
}
void buildtreebyarray()
{
int i;
int a[] = {14,15,4,9,7,18,3,5,16,4,20,17,9,14,5,1,0,21};
printf("The input is:\n");
for(i=0;i<18;i++)
{
printf("%d ",a[i]);
}
printf("\n");
for(i=0;i<18;i++)
{
node * new_node ;
new_node = ( node *)malloc(sizeof( node));
new_node->data = a[i];
new_node->lchild = NULL ;
new_node->rchild = NULL ;
if(root==NULL)
{
root = new_node ;
printf("%d is the root data\n",root->data);
}
else
{
insert(new_node,root);
}
}
}
void insert( node * new_node, node *current)
{
//checks if the current node is the lead node.
if(new_node->data > current->data && current->rchild!=NULL)
{
insert(new_node , current->rchild );
return ;
}
else if(new_node->data < current->data && current->lchild!=NULL)
{
insert(new_node , current->lchild );
return ;
}
//current node is the leaf node who is the correct parent for the new node.
//checks if the new node should be the left child of the current node.
if(new_node->data > current->data)
{
printf("%d is right child of %d\n",new_node->data,current->data);
current->rchild = new_node ;
return;
}
//or is it the right child.
else if(new_node->data < current->data)
{
printf("%d is the left child of %d\n",new_node->data,current->data);
current->lchild = new_node ;
return ;
}
//so, this is a duplicate entry!
else
{
//EDIT:earlier, there was a memory leak when duplicate entry
//was added, because memory was allocated first and check for duplicacy was
//done later, the following line of code ensures that this allocated
//memory is freed
free(new_node);
return ;
}
}
//a preorder traversal
void display( node * current)
{
if(current==NULL)
{
return;
}
else
{
printf("%d ",current->data);
display(current->lchild);
display(current->rchild);
}
}
void delete_tree(node * current)
{
if(current==NULL)
{
return;
}
else
{
delete_tree(current->lchild);
delete_tree(current->rchild);
free(current);
}
}
/*sample input 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 */