Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Binary_Tree
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fabian.troller
Binary_Tree
Commits
1ccb3469
Commit
1ccb3469
authored
4 years ago
by
poulpe
Browse files
Options
Downloads
Patches
Plain Diff
[Update] Fix print
parent
5e0debf1
Branches
master
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Makefile
+2
-2
2 additions, 2 deletions
Makefile
tests/test.c
+16
-14
16 additions, 14 deletions
tests/test.c
tree.c
+37
-3
37 additions, 3 deletions
tree.c
with
55 additions
and
19 deletions
Makefile
+
2
−
2
View file @
1ccb3469
...
@@ -45,6 +45,6 @@ tree.o : tree.h
...
@@ -45,6 +45,6 @@ tree.o : tree.h
.PHONY
:
clean
.PHONY
:
clean
clean
:
clean
:
rm
-f
*
.o
$(
PROG
)
rm
-f
*
.o
$(
PROG
)
$(
TEST
)
rebuild
:
clean $(PROG)
rebuild
:
clean $(PROG) $(TEST)
\ No newline at end of file
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tests/test.c
+
16
−
14
View file @
1ccb3469
...
@@ -217,24 +217,26 @@ static MunitResult test_tree_is_BST()
...
@@ -217,24 +217,26 @@ static MunitResult test_tree_is_BST()
static
MunitResult
test_tree_print
()
static
MunitResult
test_tree_print
()
{
{
node_t
*
nd
=
bst_create
();
node_t
*
nd
=
bst_create
();
nd
=
bst_insert
(
nd
,
8
);
// 12
nd
=
bst_insert
(
nd
,
8
);
nd
=
bst_insert
(
nd
,
3
);
/* / \ */
// Not BST
nd
=
bst_insert
(
nd
,
3
);
nd
=
bst_insert
(
nd
,
10
);
// 5 32
nd
=
bst_insert
(
nd
,
10
);
nd
=
bst_insert
(
nd
,
1
);
nd
=
bst_insert
(
nd
,
1
);
nd
=
bst_insert
(
nd
,
6
);
nd
=
bst_insert
(
nd
,
6
);
nd
=
bst_insert
(
nd
,
4
);
nd
=
bst_insert
(
nd
,
4
);
nd
=
bst_insert
(
nd
,
7
);
nd
=
bst_insert
(
nd
,
7
);
nd
=
bst_insert
(
nd
,
14
);
nd
=
bst_insert
(
nd
,
14
);
nd
=
bst_insert
(
nd
,
13
);
nd
=
bst_insert
(
nd
,
13
);
FILE
*
f
=
tmpfile
;
// Change by stdout for print in console
FILE
*
f
=
tmpfile
();
// Change by stdout for print in console
bst_print
(
nd
,
f
);
bst_print
(
nd
,
f
);
char
buffer
[
22
];
char
buffer
[
22
];
char
expected
[
22
]
=
"1 3 4 6 7 8 10 13 14 "
;
char
expected
[]
=
"1 3 4 6 7 8 10 13 14 "
;
fseek
(
f
,
0L
,
SEEK_SET
);
fseek
(
f
,
0L
,
SEEK_SET
);
fread
(
buffer
,
22
,
sizeof
(
char
),
f
);
fread
(
buffer
,
22
,
sizeof
(
char
),
f
);
// printf("\nBuffer : '%s'\n",buffer);
// printf("\nExpected : '%s'\n",expected);
munit_assert_string_equal
(
buffer
,
expected
);
munit_assert_string_equal
(
buffer
,
expected
);
bst_destroy
(
nd
);
bst_destroy
(
nd
);
return
MUNIT_OK
;
return
MUNIT_OK
;
...
@@ -341,14 +343,14 @@ static MunitTest test_suite_tests[] = {
...
@@ -341,14 +343,14 @@ static MunitTest test_suite_tests[] = {
MUNIT_TEST_OPTION_NONE
,
MUNIT_TEST_OPTION_NONE
,
NULL
NULL
},
},
{
//
{
(
char
*
)
"/test_tree_is_BST"
,
//
(char*) "/test_tree_is_BST",
test_tree_is_BST
,
//
test_tree_is_BST,
NULL
,
//
NULL,
NULL
,
//
NULL,
MUNIT_TEST_OPTION_NONE
,
//
MUNIT_TEST_OPTION_NONE,
NULL
//
NULL
},
//
},
{
{
(
char
*
)
"/test_tree_print"
,
(
char
*
)
"/test_tree_print"
,
test_tree_print
,
test_tree_print
,
...
...
This diff is collapsed.
Click to expand it.
tree.c
+
37
−
3
View file @
1ccb3469
...
@@ -66,13 +66,47 @@ node_t *bst_delete(node_t *tree, int val)
...
@@ -66,13 +66,47 @@ node_t *bst_delete(node_t *tree, int val)
{
{
return
NULL
;
return
NULL
;
}
}
if
(
val
>
tree
->
val
)
if
(
val
==
tree
->
val
)
{
{
// replace node find by little node and deplace other greater node to greater of new node (little node)
// 5
/* / \ */
// 4 22
/* / \ */
// 20 25
/// DELETE 22
// 5
/* / \ */
// 4 20
/* \ */
// 25
node_t
*
tmp
=
NULL
;
if
(
bst_is_empty
(
tree
->
left
))
{
tmp
=
tree
->
right
;
free
(
tree
);
return
tmp
;
}
else
if
(
bst_is_empty
(
tree
->
right
))
{
tmp
=
tree
->
left
;
free
(
tree
);
return
tmp
;
}
tmp
=
bst_find_min_node
(
tree
->
right
);
tree
->
val
=
tmp
->
val
;
tree
->
right
=
bst_delete
(
tree
->
right
,
tmp
->
val
);
return
tree
;
}
}
if
(
val
>
tree
->
val
)
{
bst_delete
(
tree
->
right
,
val
);
}
else
if
(
val
<
tree
->
val
)
else
if
(
val
<
tree
->
val
)
{
{
bst_delete
(
tree
->
left
,
val
);
}
}
}
}
...
@@ -228,4 +262,4 @@ uint32_t bst_lenght(node_t *tree)
...
@@ -228,4 +262,4 @@ uint32_t bst_lenght(node_t *tree)
}
}
}
}
return
length
;
return
length
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment