Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
controle-c-003
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
Package Registry
Model registry
Operate
Environments
Terraform modules
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
florian.burgener
controle-c-003
Commits
6b00b587
Commit
6b00b587
authored
2 years ago
by
florian.burgener
Browse files
Options
Downloads
Patches
Plain Diff
Validation exercice 3
parent
2926de72
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ex3/ex3.c
+21
-70
21 additions, 70 deletions
ex3/ex3.c
with
21 additions
and
70 deletions
ex3/ex3.c
+
21
−
70
View file @
6b00b587
...
...
@@ -9,12 +9,10 @@
*
*/
#include
<math.h>
#include
<stdbool.h>
#include
<stdint.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
// LinkedListNode.h
...
...
@@ -206,14 +204,10 @@ typedef struct Node {
Node
*
node_init
();
void
node_destroy
(
Node
**
node
);
Node
*
binary_search_tree_init
();
void
binary_search_tree_destroy
(
Node
**
root
);
Node
*
binary_search_tree_insert
(
Node
*
root
,
int32_t
key
);
Node
*
binary_search_tree_search
(
Node
*
root
,
int32_t
key
);
Node
*
binary_search_tree_find_smallest_node
(
Node
*
root
);
Node
*
binary_search_tree_delete
(
Node
*
root
,
int32_t
key
);
void
binary_search_tree_print
(
Node
*
root
,
int32_t
depth
);
bool
binary_search_tree_is_leaf
(
Node
*
root
);
// BinarySearchTree.c
...
...
@@ -257,73 +251,23 @@ Node *binary_search_tree_insert(Node *root, int32_t key) {
return
root
;
}
Node
*
binary_search_tree_search
(
Node
*
root
,
int32_t
key
)
{
if
(
root
==
NULL
)
{
return
NULL
;
}
if
(
key
<
root
->
key
)
{
return
binary_search_tree_search
(
root
->
left
,
key
);
}
else
if
(
key
>
root
->
key
)
{
return
binary_search_tree_search
(
root
->
right
,
key
);
}
return
root
;
}
Node
*
binary_search_tree_find_smallest_node
(
Node
*
root
)
{
while
(
root
->
left
!=
NULL
)
{
root
=
root
->
left
;
}
return
root
;
}
Node
*
binary_search_tree_delete
(
Node
*
root
,
int32_t
key
)
{
if
(
root
==
NULL
)
{
return
NULL
;
}
if
(
key
<
root
->
key
)
{
root
->
left
=
binary_search_tree_delete
(
root
->
left
,
key
);
}
else
if
(
key
>
root
->
key
)
{
root
->
right
=
binary_search_tree_delete
(
root
->
right
,
key
);
}
else
{
if
(
root
->
left
==
NULL
&&
root
->
right
==
NULL
)
{
node_destroy
(
&
root
);
return
NULL
;
}
if
(
root
->
left
==
NULL
)
{
Node
*
right
=
root
->
right
;
node_destroy
(
&
root
);
return
right
;
}
if
(
root
->
right
==
NULL
)
{
Node
*
left
=
root
->
left
;
node_destroy
(
&
root
);
return
left
;
}
Node
*
smallest
=
binary_search_tree_find_smallest_node
(
root
->
right
);
root
->
key
=
smallest
->
key
;
root
->
right
=
binary_search_tree_delete
(
root
->
right
,
smallest
->
key
);
}
return
root
;
bool
binary_search_tree_is_leaf
(
Node
*
root
)
{
return
root
->
left
==
NULL
&&
root
->
right
==
NULL
;
}
// ---
bool
node_is_leaf
(
Node
*
node
)
{
return
node
->
left
==
NULL
&&
node
->
right
==
NULL
;
}
/**
* @brief Finds a key and stores the path in a stack.
*
* @param root
* @param key
* @return Stack*
*/
Stack
*
search_path
(
Node
*
root
,
int
key
)
{
Stack
*
s
=
stack_init
();
while
(
root
!=
NULL
&&
!
nod
e_is_leaf
(
root
)
&&
root
->
key
!=
key
)
{
while
(
root
!=
NULL
&&
!
binary_search_tre
e_is_leaf
(
root
)
&&
root
->
key
!=
key
)
{
if
(
key
<
root
->
key
)
{
root
=
root
->
left
;
stack_push
(
s
,
'g'
);
...
...
@@ -337,19 +281,26 @@ Stack *search_path(Node *root, int key) {
}
int
main
()
{
Node
*
root
=
binary_search_tree_init
();
// !!!
const
int
KEY_1
=
24
;
const
int
KEY_2
=
7
;
int
numbers_length
=
9
;
int
numbers
[]
=
{
10
,
0
,
20
,
-
5
,
5
,
15
,
25
,
22
,
24
};
Node
*
root
=
binary_search_tree_init
();
for
(
int
i
=
0
;
i
<
numbers_length
;
i
+=
1
)
{
root
=
binary_search_tree_insert
(
root
,
numbers
[
i
]);
}
Stack
*
s
=
search_path
(
root
,
24
);
Stack
*
s
=
search_path
(
root
,
KEY_1
);
printf
(
"Path for %d
\n
"
,
KEY_1
);
stack_print
(
s
);
stack_destroy
(
&
s
);
s
=
search_path
(
root
,
7
);
s
=
search_path
(
root
,
KEY_2
);
printf
(
"Path for %d
\n
"
,
KEY_2
);
stack_print
(
s
);
stack_destroy
(
&
s
);
...
...
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