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
af965ba7
Commit
af965ba7
authored
4 years ago
by
poulpe
Browse files
Options
Downloads
Patches
Plain Diff
[Update] Add find_min/max_node + test
parent
d2a5a44b
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
tests/test.c
+54
-0
54 additions, 0 deletions
tests/test.c
tree.c
+25
-12
25 additions, 12 deletions
tree.c
with
79 additions
and
12 deletions
tests/test.c
+
54
−
0
View file @
af965ba7
...
...
@@ -154,6 +154,44 @@ static MunitResult test_tree_search()
return
MUNIT_OK
;
}
static
MunitResult
test_tree_find_max_node
()
{
node_t
*
nd
=
bst_create
();
munit_assert_true
(
bst_is_empty
(
nd
));
nd
=
bst_insert
(
nd
,
12
);
nd
=
bst_insert
(
nd
,
22
);
nd
=
bst_insert
(
nd
,
32
);
nd
=
bst_insert
(
nd
,
42
);
nd
=
bst_insert
(
nd
,
2
);
nd
=
bst_insert
(
nd
,
3
);
nd
=
bst_insert
(
nd
,
4
);
nd
=
bst_insert
(
nd
,
5
);
nd
=
bst_insert
(
nd
,
10
);
nd
=
bst_insert
(
nd
,
50
);
munit_assert_int32
(
bst_find_max_node
(
nd
)
->
val
,
==
,
50
);
bst_destroy
(
nd
);
return
MUNIT_OK
;
}
static
MunitResult
test_tree_find_min_node
()
{
node_t
*
nd
=
bst_create
();
munit_assert_true
(
bst_is_empty
(
nd
));
nd
=
bst_insert
(
nd
,
12
);
nd
=
bst_insert
(
nd
,
22
);
nd
=
bst_insert
(
nd
,
32
);
nd
=
bst_insert
(
nd
,
42
);
nd
=
bst_insert
(
nd
,
2
);
nd
=
bst_insert
(
nd
,
3
);
nd
=
bst_insert
(
nd
,
4
);
nd
=
bst_insert
(
nd
,
5
);
nd
=
bst_insert
(
nd
,
10
);
nd
=
bst_insert
(
nd
,
50
);
munit_assert_int32
(
bst_find_min_node
(
nd
)
->
val
,
==
,
2
);
bst_destroy
(
nd
);
return
MUNIT_OK
;
}
static
MunitResult
test_tree_is_BST
()
{
node_t
*
nd
=
bst_create
();
...
...
@@ -234,6 +272,14 @@ static MunitTest test_suite_tests[] = {
MUNIT_TEST_OPTION_NONE
,
NULL
},
{
(
char
*
)
"/test_tree_find_min_node"
,
test_tree_find_min_node
,
NULL
,
NULL
,
MUNIT_TEST_OPTION_NONE
,
NULL
},
{
(
char
*
)
"/test_tree_find_max"
,
test_tree_find_max
,
...
...
@@ -242,6 +288,14 @@ static MunitTest test_suite_tests[] = {
MUNIT_TEST_OPTION_NONE
,
NULL
},
{
(
char
*
)
"/test_tree_find_max_node"
,
test_tree_find_max_node
,
NULL
,
NULL
,
MUNIT_TEST_OPTION_NONE
,
NULL
},
{
(
char
*
)
"/test_tree_search"
,
test_tree_search
,
...
...
This diff is collapsed.
Click to expand it.
tree.c
+
25
−
12
View file @
af965ba7
...
...
@@ -33,36 +33,49 @@ bool bst_is_bst(node_t *tree)
}
}
int
bst_find_min
(
node_t
*
tree
)
node_t
*
bst_find_min_node
(
node_t
*
tree
)
{
if
(
tree
==
NULL
)
node_t
*
tmp
=
tree
;
if
(
tmp
==
NULL
)
{
return
INT_MIN
;
return
NULL
;
}
if
(
t
ree
->
left
!=
NULL
)
if
(
t
mp
->
left
!=
NULL
)
{
return
bst_find_min
(
tree
->
left
);
return
bst_find_min
_node
(
tmp
->
left
);
}
else
{
return
t
ree
->
val
;
return
t
mp
;
}
}
int
bst_find_min
(
node_t
*
tree
)
{
node_t
*
tmp
=
bst_find_min_node
(
tree
);
return
(
tmp
!=
NULL
)
?
tmp
->
val
:
INT_MIN
;
}
int
bst_find_max
(
node_t
*
tree
)
{
if
(
tree
==
NULL
)
node_t
*
tmp
=
bst_find_max_node
(
tree
);
return
(
tmp
!=
NULL
)
?
tmp
->
val
:
INT_MAX
;
}
node_t
*
bst_find_max_node
(
node_t
*
tree
)
{
return
INT_MIN
;
node_t
*
tmp
=
tree
;
if
(
tmp
==
NULL
)
{
return
NULL
;
}
if
(
t
ree
->
right
!=
NULL
)
if
(
t
mp
->
right
!=
NULL
)
{
return
bst_find_max
(
tree
->
right
);
return
bst_find_max
_node
(
tmp
->
right
);
}
else
{
return
t
ree
->
val
;
return
t
mp
;
}
}
...
...
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