Platform
Resources
Pricing
Sign in
Get started
giovannimaffeo
Workspace
Fork
Published
By
giovannimaffeo
Edited
May 15, 2022
1 fork
1 star
Insert cell
Insert cell
class
RootedTree
{
// "...descendants" means that all the arguments we put after the first argument will be
// gathered (reunidos) in a list called descendants
// A ideia aqui é que a RootedTree seja formada por um nó raiz e os descendentes sejam
// outras RootedTrees com o mesmo formato.
constructor
(
data
,
...
descendants
)
{
// Data to be stored by the root
this
.
data
=
data
;
this
.
descendants
=
descendants
;
}
// A função que utiliza yield é uma geradora, isto é, ela retorna valores "aos poucos", assim,
// podemos ter o seguinte programa abaixo em python:
//
// def f():
// yield 1 #gere 1
// print("oi") #print "oi"
// yield 2 #gere 2
//
// for x in f():
// print(x)
//
// retorno: 1, "oi", 2
//
// Essa função gera o valor 1 como retorno, fica em "suspensão", printa o valor "oi" e, por fim,
// gera 2 como retorno.
//
// No js, as funções geradoras devem ser implementadas com o "*" antes.
// Visitamos a raíz (este nó --> yeld this) e, para todos os filhos, visitamos os filhos em pré ordem T1,..,TK
// O "*yield significa fazer o yield de cada um deles
*
preorder
(
)
{
yield
this
;
for
(
let
d
of
this
.
descendants
)
yield
*
d
.
preorder
(
)
}
*
postorder
(
)
{
for
(
let
d
of
this
.
descendants
)
yield
*
d
.
postorder
(
)
yield
this
;
}
}
Insert cell
Insert cell
RT
=
function
(
...
args
)
{
return
new
RootedTree
(
...
args
)
}
;
Insert cell
Insert cell
rt1
=
RT
(
"A"
,
RT
(
"B"
)
,
RT
(
"C"
)
,
RT
(
"D"
)
)
Insert cell
draw
(
rt1
)
Insert cell
Insert cell
rt2
=
RT
(
"A"
,
RT
(
"B"
,
RT
(
"C"
)
,
RT
(
"D"
)
)
,
RT
(
"E"
)
,
RT
(
"F"
,
RT
(
"G"
)
,
RT
(
"H"
,
RT
(
"I"
)
,
RT
(
"J"
)
,
RT
(
"K"
)
)
)
)
Insert cell
draw
(
rt2
)
Insert cell
Insert cell
[
...
rt2
.
preorder
(
)
]
.
map
(
node
=>
node
.
data
)
Insert cell
[
...
rt2
.
postorder
(
)
]
.
map
(
node
=>
node
.
data
)
Insert cell
Insert cell
Insert cell
Insert cell
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
RootedTree
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
RT
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
rt1
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
rt2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
graphViz
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
draw
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML