Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
BinarySearchTree
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ollie Rhodes
BinarySearchTree
Commits
6b1987d8
Commit
6b1987d8
authored
Jan 20, 2020
by
Ollie Rhodes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
da1f3c8c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
172 additions
and
0 deletions
+172
-0
binarySearchTree.c
binarySearchTree.c
+172
-0
No files found.
binarySearchTree.c
0 → 100644
View file @
6b1987d8
#include <stdio.h>
#include <stdlib.h>
FILE
*
file
;
char
info
[];
struct
node
{
int
info
;
struct
node
*
left
;
struct
node
*
right
;
};
//typedef void (*callback)(node*);
int
main
()
{
//read("pg03data.txt");
//write("output.txt");
//bool first = true;
struct
node
*
root
=
NULL
;
file
=
fopen
(
"pg03data.txt"
,
"r"
);
while
(
fgets
(
info
,
100
,
file
)
!=
NULL
){
// if (first == true){
// root = insert(root, info);
// first = false;
// }else{
insert_node
(
root
,
info
);
// }
}
fclose
(
file
);
file
=
fopen
(
"output.txt"
,
"w"
);
traverse
(
root
);
fclose
(
file
);
return
0
;
}
/*
int read(char fileName[]){
//bool first = true;
struct node *root = NULL;
file = fopen(fileName , "r");
while (fgets(info, 100, file) != NULL){
// if (first == true){
// root = insert(root, info);
// first = false;
// }else{
insert_node(root,info);
// }
}
fclose(file);
}
*/
/*
int write(char newFileName[]){
file = fopen(newFileName, "w");
traverse(root);
fclose(file);
}
*/
struct
node
*
insert_node
(
struct
node
*
root
,
int
data
){
if
(
root
==
NULL
)
{
root
=
create_node
(
info
);
}
else
{
int
is_left
=
0
;
int
r
=
0
;
struct
node
*
current
=
root
;
struct
node
*
prev
=
NULL
;
while
(
current
!=
NULL
)
{
r
=
compare
(
data
,
current
->
info
);
prev
=
current
;
if
(
r
<
0
)
{
is_left
=
1
;
current
=
current
->
left
;
}
else
if
(
r
>
0
)
{
is_left
=
0
;
current
=
current
->
right
;
}
}
if
(
is_left
)
prev
->
left
=
create_node
(
info
);
else
prev
->
right
=
create_node
(
info
);
}
return
root
;
}
int
compare
(
int
left
,
int
right
)
{
if
(
strcmp
(
left
,
right
)
<
0
)
return
1
;
if
(
strcmp
(
left
,
right
)
>
0
)
return
-
1
;
return
0
;
}
struct
node
*
create_node
(
char
lineInput
[
60
]){
struct
node
*
new_node
=
(
node
*
)
malloc
(
sizeof
(
struct
node
));
if
(
new_node
==
NULL
)
{
fprintf
(
stderr
,
"Out of memory!!! (create_node)
\n
"
);
exit
(
1
);
}
new_node
->
info
=
lineInput
;
new_node
->
left
=
NULL
;
new_node
->
right
=
NULL
;
return
new_node
;
}
void
traverse
(
struct
node
*
root
){
node
*
current
,
*
pre
;
if
(
root
==
NULL
)
return
;
current
=
root
;
while
(
current
!=
NULL
)
{
if
(
current
->
left
!=
NULL
)
{
cb
(
current
);
current
=
current
->
right
;
}
else
{
pre
=
current
->
left
;
while
(
pre
->
right
!=
NULL
&&
pre
->
right
!=
current
)
pre
=
pre
->
right
;
if
(
pre
->
right
!=
NULL
)
{
pre
->
right
=
current
;
current
=
current
->
left
;
}
else
{
pre
->
right
=
NULL
;
cb
(
current
);
current
=
current
->
right
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment