Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prog_exam_05
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
boris.stefanov
prog_exam_05
Commits
90e54c6b
Commit
90e54c6b
authored
3 years ago
by
Boris Stefanovic
Browse files
Options
Downloads
Patches
Plain Diff
save
parent
d209a624
Branches
Branches containing commit
Tags
v1.0.0
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ex3/ex3.c
+105
-0
105 additions, 0 deletions
ex3/ex3.c
with
105 additions
and
0 deletions
ex3/ex3.c
+
105
−
0
View file @
90e54c6b
...
...
@@ -2,5 +2,110 @@
#include
<stdlib.h>
typedef
struct
node
{
int
value
;
struct
node
*
l
;
struct
node
*
r
;
}
node_t
;
struct
node
*
node_create
(
const
int
value
)
{
struct
node
*
n
=
malloc
(
sizeof
(
node_t
));
n
->
value
=
value
;
n
->
l
=
NULL
;
n
->
r
=
NULL
;
return
n
;
}
void
node_insert
(
struct
node
*
n
,
const
int
value
)
{
if
(
value
<
n
->
value
)
{
if
(
n
->
l
==
NULL
)
{
n
->
l
=
node_create
(
value
);
}
else
{
node_insert
(
n
->
l
,
value
);
}
}
else
if
(
value
>
n
->
value
)
{
if
(
n
->
r
==
NULL
)
{
n
->
r
=
node_create
(
value
);
}
else
{
node_insert
(
n
->
r
,
value
);
}
}
}
void
tree_insert
(
struct
node
**
t
,
const
int
value
)
{
if
(
NULL
==
(
*
t
))
{
*
t
=
node_create
(
value
);
}
else
{
node_insert
(
*
t
,
value
);
}
}
void
tree_destroy
(
struct
node
*
n
)
{
if
(
n
!=
NULL
)
{
tree_destroy
(
n
->
l
);
tree_destroy
(
n
->
r
);
free
(
n
);
}
}
typedef
struct
stackitem
{
char
value
;
struct
stackitem
*
next
;
}
stackitem_t
;
void
stack_push
(
stackitem_t
**
s
,
char
value
)
{
stackitem_t
*
top
=
malloc
(
sizeof
(
stackitem_t
));
top
->
value
=
value
;
top
->
next
=
*
s
;
*
s
=
top
;
}
char
stack_pop
(
stackitem_t
**
s
)
{
if
(
*
s
==
NULL
)
return
'\0'
;
stackitem_t
*
top
=
*
s
;
char
c
=
top
->
value
;
*
s
=
(
*
s
)
->
next
;
free
(
top
);
return
c
;
}
void
stack_consume
(
stackitem_t
*
item
)
{
if
(
item
==
NULL
)
return
;
printf
(
"%c"
,
item
->
value
);
if
(
item
->
next
!=
NULL
)
stack_consume
(
item
->
next
);
free
(
item
);
}
void
search_path
(
const
node_t
*
root
,
const
int
value
,
stackitem_t
**
s
)
{
if
(
root
==
NULL
)
return
;
if
(
value
<
root
->
value
)
{
stack_push
(
s
,
'g'
);
search_path
(
root
->
l
,
value
,
s
);
}
else
if
(
value
>
root
->
value
)
{
stack_push
(
s
,
'd'
);
search_path
(
root
->
r
,
value
,
s
);
}
}
int
main
()
{
const
int
a
[]
=
{
10
,
0
,
20
,
-
5
,
5
,
15
,
25
,
22
,
24
};
const
int
len
=
9
;
struct
node
*
root
=
NULL
;
for
(
int
i
=
0
;
i
<
len
;
++
i
)
{
tree_insert
(
&
root
,
a
[
i
]);
}
stackitem_t
*
top
=
NULL
;
search_path
(
root
,
7
,
&
top
);
stack_consume
(
top
);
printf
(
"
\n
"
);
tree_destroy
(
root
);
return
EXIT_SUCCESS
;
}
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