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
9bdc66c9
Commit
9bdc66c9
authored
4 years ago
by
poulpe
Browse files
Options
Downloads
Patches
Plain Diff
[Update] Add test + fix somes function
parent
4172d801
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
+60
-0
60 additions, 0 deletions
tests/test.c
tree.c
+27
-16
27 additions, 16 deletions
tree.c
tree.h
+2
-2
2 additions, 2 deletions
tree.h
with
89 additions
and
18 deletions
tests/test.c
+
60
−
0
View file @
9bdc66c9
...
@@ -29,11 +29,47 @@ static MunitResult test_tree_insert()
...
@@ -29,11 +29,47 @@ static MunitResult test_tree_insert()
static
MunitResult
test_tree_destroy
()
static
MunitResult
test_tree_destroy
()
{
{
node_t
*
nd
=
bst_create
();
node_t
*
nd
=
bst_create
();
munit_assert_ptr
(
nd
,
==
,
NULL
);
nd
=
bst_insert
(
nd
,
12
);
nd
=
bst_insert
(
nd
,
22
);
nd
=
bst_insert
(
nd
,
32
);
nd
=
bst_insert
(
nd
,
42
);
nd
=
bst_insert
(
nd
,
52
);
nd
=
bst_insert
(
nd
,
62
);
nd
=
bst_insert
(
nd
,
72
);
nd
=
bst_insert
(
nd
,
82
);
munit_assert_ptr
(
nd
,
!=
,
NULL
);
munit_assert_ptr
(
nd
,
!=
,
NULL
);
bst_destroy
(
nd
);
bst_destroy
(
nd
);
return
MUNIT_OK
;
return
MUNIT_OK
;
}
}
static
MunitResult
test_tree_length
()
{
node_t
*
nd
=
bst_create
();
munit_assert_ptr
(
nd
,
==
,
NULL
);
nd
=
bst_insert
(
nd
,
12
);
nd
=
bst_insert
(
nd
,
22
);
nd
=
bst_insert
(
nd
,
32
);
nd
=
bst_insert
(
nd
,
42
);
nd
=
bst_insert
(
nd
,
52
);
nd
=
bst_insert
(
nd
,
62
);
nd
=
bst_insert
(
nd
,
72
);
nd
=
bst_insert
(
nd
,
82
);
munit_assert_ptr
(
nd
,
!=
,
NULL
);
bst_destroy
(
nd
);
return
MUNIT_OK
;
}
static
MunitResult
test_tree_is_empty
()
{
node_t
*
nd
=
bst_create
();
munit_assert_true
(
bst_is_empty
(
nd
));
nd
=
bst_insert
(
nd
,
12
);
munit_assert_false
(
bst_is_empty
(
nd
));
bst_destroy
(
nd
);
return
MUNIT_OK
;
}
static
MunitTest
test_suite_tests
[]
=
{
static
MunitTest
test_suite_tests
[]
=
{
{
{
...
@@ -52,6 +88,30 @@ static MunitTest test_suite_tests[] = {
...
@@ -52,6 +88,30 @@ static MunitTest test_suite_tests[] = {
MUNIT_TEST_OPTION_NONE
,
MUNIT_TEST_OPTION_NONE
,
NULL
NULL
},
},
{
(
char
*
)
"/test_tree_destroy"
,
test_tree_destroy
,
NULL
,
NULL
,
MUNIT_TEST_OPTION_NONE
,
NULL
},
{
(
char
*
)
"/test_tree_length"
,
test_tree_length
,
NULL
,
NULL
,
MUNIT_TEST_OPTION_NONE
,
NULL
},
{
(
char
*
)
"/test_tree_empty"
,
test_tree_is_empty
,
NULL
,
NULL
,
MUNIT_TEST_OPTION_NONE
,
NULL
},
{
NULL
,
NULL
,
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
+
27
−
16
View file @
9bdc66c9
...
@@ -21,6 +21,7 @@ void bst_destroy(node_t *tree)
...
@@ -21,6 +21,7 @@ void bst_destroy(node_t *tree)
bst_destroy
(
tree
->
right
);
bst_destroy
(
tree
->
right
);
tree
->
right
=
NULL
;
tree
->
right
=
NULL
;
}
}
if
(
tree
->
right
==
NULL
&&
tree
->
left
==
NULL
)
if
(
tree
->
right
==
NULL
&&
tree
->
left
==
NULL
)
{
{
free
(
tree
);
free
(
tree
);
...
@@ -28,6 +29,11 @@ void bst_destroy(node_t *tree)
...
@@ -28,6 +29,11 @@ void bst_destroy(node_t *tree)
}
}
}
}
bool
bst_is_empty
(
node_t
*
tree
)
{
return
(
tree
==
NULL
)
?
true
:
false
;
}
node_t
*
bst_insert
(
node_t
*
tree
,
int
val
)
node_t
*
bst_insert
(
node_t
*
tree
,
int
val
)
{
{
if
(
tree
==
NULL
)
if
(
tree
==
NULL
)
...
@@ -41,25 +47,30 @@ node_t *bst_insert(node_t *tree, int val)
...
@@ -41,25 +47,30 @@ node_t *bst_insert(node_t *tree, int val)
if
(
val
>
tree
->
val
)
// Right
if
(
val
>
tree
->
val
)
// Right
{
{
// if(tree->right != NULL)
tree
->
right
=
bst_insert
(
tree
->
right
,
val
);
tree
=
bst_insert
(
tree
->
right
,
val
);
// else if(tree->right == NULL)
// {
// tree->right = malloc(1 * sizeof(node_t));
// tree->right->val = val;
// tree->left = NULL;
// }
}
}
else
if
(
val
<
tree
->
val
)
// Left
else
if
(
val
<
tree
->
val
)
// Left
{
{
// if(tree->left != NULL)
tree
->
left
=
bst_insert
(
tree
->
left
,
val
);
tree
=
bst_insert
(
tree
->
left
,
val
);
// else if(tree->left == NULL)
// {
// tree->left = malloc(1 * sizeof(node_t));
// tree->left->val = val;
// tree->right = NULL;
// }
}
}
return
tree
;
return
tree
;
}
uint32_t
bst_lenght
(
node_t
*
tree
)
{
uint32_t
length
=
0
;
if
(
tree
->
left
==
NULL
&&
tree
->
right
==
NULL
&&
tree
!=
NULL
)
{
return
1
;
}
else
{
if
(
tree
->
left
!=
NULL
)
{
length
+=
bst_lenght
(
tree
->
left
);
}
if
(
tree
->
right
!=
NULL
){
length
+=
bst_lenght
(
tree
->
right
);
}
}
return
length
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tree.h
+
2
−
2
View file @
9bdc66c9
...
@@ -38,7 +38,7 @@ node_t *bst_find_max_node(node_t *tree);
...
@@ -38,7 +38,7 @@ node_t *bst_find_max_node(node_t *tree);
// retourne la valeur la plus grande stockée dans l'arbre (ou MAX_INT)
// retourne la valeur la plus grande stockée dans l'arbre (ou MAX_INT)
int
bst_find_max
(
node_t
*
tree
);
int
bst_find_max
(
node_t
*
tree
);
int
bst_lenght
(
node_t
*
tree
);
// Retourne la longeur de l'arbre
u
int
32_t
bst_lenght
(
node_t
*
tree
);
// Retourne la longeur de l'arbre
int
bst_is_empty
(
node_t
*
tree
);
// Retourne true si l'arbre est vide
bool
bst_is_empty
(
node_t
*
tree
);
// Retourne true si l'arbre est vide
#endif
#endif
\ 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