Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
graph_drawing
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
graph_drawing
Commits
a509de43
Commit
a509de43
authored
Dec 01, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Leaf now extends Node, added Node representation fcuntionality.
parent
decaaecb
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
9 deletions
+19
-9
line.py
line.py
+4
-5
node.py
node.py
+15
-4
No files found.
line.py
View file @
a509de43
from
node
import
Node
from
node
import
Leaf
def
generate_line
(
root
):
def
generate_line
(
root
):
"""
"""
Print an expression tree in a single text line. Where needed, add
Print an expression tree in a single text line. Where needed, add
parentheses.
parentheses.
>>> from node import Leaf
>>> from node import Node, Leaf
>>> l0, l1 = Leaf(1), Leaf(2)
>>> l0, l1 = Leaf(1), Leaf(2)
>>> plus = Node('+', l0, l1)
>>> plus = Node('+', l0, l1)
>>> print generate_line(plus)
>>> print generate_line(plus)
...
@@ -51,7 +50,7 @@ def generate_line(root):
...
@@ -51,7 +50,7 @@ def generate_line(root):
"""
"""
Get the associativity of an operator node.
Get the associativity of an operator node.
"""
"""
if
isinstance
(
node
,
Node
)
and
len
(
node
)
>
1
:
if
not
isinstance
(
node
,
Leaf
)
and
len
(
node
)
>
1
:
op
=
node
.
title
()
op
=
node
.
title
()
for
i
,
group
in
enumerate
(
operators
):
for
i
,
group
in
enumerate
(
operators
):
...
@@ -68,7 +67,7 @@ def generate_line(root):
...
@@ -68,7 +67,7 @@ def generate_line(root):
"""
"""
s
=
node
.
title
()
s
=
node
.
title
()
if
not
isinstance
(
node
,
Node
):
if
isinstance
(
node
,
Leaf
):
return
s
return
s
arity
=
len
(
node
)
arity
=
len
(
node
)
...
...
node.py
View file @
a509de43
...
@@ -21,20 +21,31 @@ class Node(object):
...
@@ -21,20 +21,31 @@ class Node(object):
def
__len__
(
self
):
def
__len__
(
self
):
return
len
(
self
.
nodes
)
return
len
(
self
.
nodes
)
def
__eq__
(
self
,
node
):
return
isinstance
(
node
,
Node
)
\
and
self
.
value
==
node
.
value
and
self
.
nodes
==
node
.
nodes
#def __repr__(self):
# return repr(self.value)
def
__str__
(
self
):
return
'<Node value=%s nodes=%s>'
%
(
str
(
self
.
value
),
str
(
self
.
nodes
))
def
title
(
self
):
def
title
(
self
):
return
str
(
self
.
value
)
return
str
(
self
.
value
)
class
Leaf
(
object
):
class
Leaf
(
Node
):
def
__init__
(
self
,
value
):
def
__init__
(
self
,
value
):
self
.
value
=
value
self
.
value
=
value
self
.
parent
=
None
self
.
parent
=
None
self
.
nodes
=
None
def
__len__
(
self
):
return
len
(
str
(
self
.
value
))
def
__repr__
(
self
):
def
__repr__
(
self
):
return
repr
(
self
.
value
)
return
repr
(
self
.
value
)
def
__str__
(
self
):
def
__str__
(
self
):
return
str
(
self
.
value
)
return
str
(
self
.
value
)
def
title
(
self
):
return
str
(
self
.
value
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment