Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prm2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Martin Mareš
prm2
Commits
2289c465
Commit
2289c465
authored
5 years ago
by
Martin Mareš
Browse files
Options
Downloads
Patches
Plain Diff
Výrazy: Příklad se stromečky
parent
f3921c69
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
06-vyrazy/strom-vyrazu.py
+52
-0
52 additions, 0 deletions
06-vyrazy/strom-vyrazu.py
with
52 additions
and
0 deletions
06-vyrazy/strom-vyrazu.py
0 → 100755
+
52
−
0
View file @
2289c465
#!/usr/bin/python3
class
Node
:
def
__init__
(
self
,
op
,
left
=
None
,
right
=
None
):
self
.
op
=
op
# Buď číslo nebo jméno operátoru
self
.
left
=
left
self
.
right
=
right
def
__str__
(
self
):
if
type
(
self
.
op
)
is
int
:
return
str
(
self
.
op
)
else
:
return
'
(
'
+
str
(
self
.
left
)
+
self
.
op
+
str
(
self
.
right
)
+
'
)
'
def
print_tree
(
self
,
level
=
0
):
if
self
.
right
is
not
None
:
self
.
right
.
print_tree
(
level
+
1
)
print
(
"
"
*
level
,
self
.
op
)
if
self
.
left
is
not
None
:
self
.
left
.
print_tree
(
level
+
1
)
def
eval
(
self
):
op
=
self
.
op
if
type
(
op
)
is
int
:
return
self
.
op
else
:
l
=
self
.
left
.
eval
()
r
=
self
.
right
.
eval
()
if
op
==
'
+
'
:
return
l
+
r
if
op
==
'
-
'
:
return
l
-
r
if
op
==
'
*
'
:
return
l
*
r
if
op
==
'
/
'
:
return
l
//
r
raise
NotImplemented
()
x
=
Node
(
'
+
'
,
Node
(
'
*
'
,
Node
(
3
),
Node
(
4
)),
Node
(
'
-
'
,
Node
(
5
),
Node
(
6
)))
print
(
x
)
# x.print_tree()
# print(x.eval())
# Další úkoly:
# - doplnit unární operátory (třeba 'N' pro negaci)
# - počítání hloubky stromu
# - výpis stromu v postfixovém tvaru
# - převod postfixového tvaru na strom
# - výpis stromu v infixovém tvaru bez zbytečných závorek
# - prevod infixového tvaru na strom (*)
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