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
cfa4b3ba
Commit
cfa4b3ba
authored
4 years ago
by
poulpe
Browse files
Options
Downloads
Patches
Plain Diff
[Update] Add print function + test
parent
af965ba7
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
tests/test.c
+61
-8
61 additions, 8 deletions
tests/test.c
tree.c
+53
-10
53 additions, 10 deletions
tree.c
tree.h
+1
-1
1 addition, 1 deletion
tree.h
with
115 additions
and
19 deletions
tests/test.c
+
61
−
8
View file @
cfa4b3ba
...
...
@@ -214,6 +214,43 @@ static MunitResult test_tree_is_BST()
return
MUNIT_OK
;
}
static
MunitResult
test_tree_print
()
{
node_t
*
nd
=
bst_create
();
nd
=
bst_insert
(
nd
,
8
);
// 12
nd
=
bst_insert
(
nd
,
3
);
/* / \ */
// Not BST
nd
=
bst_insert
(
nd
,
10
);
// 5 32
nd
=
bst_insert
(
nd
,
1
);
nd
=
bst_insert
(
nd
,
6
);
nd
=
bst_insert
(
nd
,
4
);
nd
=
bst_insert
(
nd
,
7
);
nd
=
bst_insert
(
nd
,
14
);
nd
=
bst_insert
(
nd
,
13
);
FILE
*
f
=
tmpfile
();
bst_print
(
nd
,
f
);
char
buffer
[
22
];
char
expected
[
22
]
=
"1 3 4 6 7 8 10 13 14 "
;
fseek
(
f
,
0L
,
SEEK_SET
);
fread
(
buffer
,
22
,
sizeof
(
char
),
f
);
munit_assert_string_equal
(
buffer
,
expected
);
bst_destroy
(
nd
);
return
MUNIT_OK
;
}
static
MunitResult
test_tree_delete
()
{
node_t
*
nd
=
bst_create
();
nd
=
bst_insert
(
nd
,
12
);
// 12
nd
=
bst_insert
(
nd
,
5
);
/* / \ */
// Not BST
nd
=
bst_insert
(
nd
,
32
);
// 5 32
munit_assert_int32
(
bst_delete
(
nd
,
32
)
->
val
,
==
,
32
);
bst_destroy
(
nd
);
return
MUNIT_OK
;
}
static
MunitTest
test_suite_tests
[]
=
{
{
...
...
@@ -304,14 +341,30 @@ static MunitTest test_suite_tests[] = {
MUNIT_TEST_OPTION_NONE
,
NULL
},
// {
// (char*) "/test_tree_is_BST",
// test_tree_is_BST,
// NULL,
// NULL,
// MUNIT_TEST_OPTION_NONE,
// NULL
// },
{
(
char
*
)
"/test_tree_is_BST"
,
test_tree_is_BST
,
NULL
,
NULL
,
MUNIT_TEST_OPTION_NONE
,
NULL
},
{
(
char
*
)
"/test_tree_print"
,
test_tree_print
,
NULL
,
NULL
,
MUNIT_TEST_OPTION_NONE
,
NULL
},
{
(
char
*
)
"/test_tree_delete"
,
test_tree_delete
,
NULL
,
NULL
,
MUNIT_TEST_OPTION_NONE
,
NULL
},
{
NULL
,
NULL
,
NULL
,
NULL
,
MUNIT_TEST_OPTION_NONE
,
NULL
}
};
...
...
This diff is collapsed.
Click to expand it.
tree.c
+
53
−
10
View file @
cfa4b3ba
...
...
@@ -9,6 +9,8 @@ node_t *bst_create()
return
NULL
;
}
// Marche po T_T
bool
bst_is_bst
(
node_t
*
tree
)
{
if
(
tree
==
NULL
)
// Default is true
...
...
@@ -31,22 +33,63 @@ bool bst_is_bst(node_t *tree)
}
return
bst_is_bst
(
tree
->
right
);
}
return
NULL
;
}
void
bst_print
(
node_t
*
tree
,
FILE
*
out
)
{
if
(
tree
==
NULL
){
return
;}
if
(
tree
->
left
!=
NULL
)
{
bst_print
(
tree
->
left
,
out
);
fprintf
(
out
,
"%d "
,
tree
->
val
);
}
if
(
tree
->
left
==
NULL
&&
tree
->
right
!=
NULL
)
{
fprintf
(
out
,
"%d "
,
tree
->
val
);
}
if
(
tree
->
right
!=
NULL
)
{
bst_print
(
tree
->
right
,
out
);
}
if
(
tree
->
left
==
NULL
&&
tree
->
right
==
NULL
)
{
fprintf
(
out
,
"%d "
,
tree
->
val
);
}
}
node_t
*
bst_delete
(
node_t
*
tree
,
int
val
)
{
if
(
tree
==
NULL
)
{
return
NULL
;
}
if
(
val
>
tree
->
val
)
{
}
else
if
(
val
<
tree
->
val
)
{
}
}
node_t
*
bst_find_min_node
(
node_t
*
tree
)
{
node_t
*
tmp
=
tree
;
if
(
tmp
==
NULL
)
if
(
tree
==
NULL
)
{
return
NULL
;
}
if
(
t
mp
->
left
!=
NULL
)
if
(
t
ree
->
left
!=
NULL
)
{
return
bst_find_min_node
(
t
mp
->
left
);
return
bst_find_min_node
(
t
ree
->
left
);
}
else
{
return
t
mp
;
return
t
ree
;
}
}
...
...
@@ -64,18 +107,17 @@ int bst_find_max(node_t *tree)
node_t
*
bst_find_max_node
(
node_t
*
tree
)
{
node_t
*
tmp
=
tree
;
if
(
tmp
==
NULL
)
if
(
tree
==
NULL
)
{
return
NULL
;
}
if
(
t
mp
->
right
!=
NULL
)
if
(
t
ree
->
right
!=
NULL
)
{
return
bst_find_max_node
(
t
mp
->
right
);
return
bst_find_max_node
(
t
ree
->
right
);
}
else
{
return
t
mp
;
return
t
ree
;
}
}
...
...
@@ -122,6 +164,7 @@ void bst_destroy(node_t *tree)
}
}
bool
bst_is_empty
(
node_t
*
tree
)
{
return
(
tree
==
NULL
)
?
true
:
false
;
...
...
This diff is collapsed.
Click to expand it.
tree.h
+
1
−
1
View file @
cfa4b3ba
...
...
@@ -15,7 +15,7 @@ typedef struct _node_t {
node_t
*
bst_create
();
// création d'un arbre vide (retourne NULL)
void
bst_destroy
(
node_t
*
tree
);
// détruit l'arbre et vide la mémoire
void
bst_print
(
node_t
*
tree
);
// affiche l'arbre (voir plus bas)
void
bst_print
(
node_t
*
tree
,
FILE
*
out
);
// affiche l'arbre (voir plus bas)
// insertion de val dans l'arbre et retourne l'arbre (ou NULL si problème)
node_t
*
bst_insert
(
node_t
*
tree
,
int
val
);
...
...
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