How can I build a 13-node binary tree in R Studio

I'm new to R, I'm a graph of a graph, like a ring, a star. There are special functions for them, but I don’t know how I can build a binary tree with 13 nodes? I used the graph.extended.chordal.ring () function, but that did not help. Is there a good tutorial for studio R and how can I build a binary tree?

library(igraph)
G <-  graph.extended.chordal.ring(13, matrix(c(2,4,6), nr=1))
L <- layout.fruchterman.reingold(G)
+4
source share
2 answers

You can use a function graph.tree, for example.

library(igraph)
G <- graph.tree(n=13,children=2)

# let print it using a tree-specific layout 
# (N.B. you must specify the root node)
co <- layout.reingold.tilford(G, params=list(root=1)) 
plot(G, layout=co)

enter image description here


EDIT (according to comments) :

library(igraph)
G <- graph.tree(n=13,children=2)

#add names to vertex (just assign a upper-case letter to each)
V(G)$name <- LETTERS[1:length(V(G))]

# plot (1)
lay <- layout.reingold.tilford(G, params=list(root='A')) 
plot(G, layout=lay, vertex.size=25)

# add a vertex 'O', then a new edge 'G' --> 'O'
G <- G + vertices('O')
G <- G + edge('G', 'O')

# plot again (2)
lay <- layout.reingold.tilford(G, params=list(root='A')) 
plot(G, layout=lay, vertex.size=25)

enter image description here

+9
source

@digEmAll, , btree. , data.table( data.frames).

library(data.table)
library(ggplot2)
library(btree)

# Make a perfect btree with depth 3 -> 15 total nodes
btree <- make_perfect_btree(depth=3)
btree
    NodeId ParentNodeId LeftChildNodeId RightChildNodeId NodePath Depth
 1:      1           NA               2                3              0
 2:      2            1               4                5        L     1
 3:      3            1               6                7        R     1
 4:      4            2               8                9       LL     2
 5:      5            2              10               11       LR     2
 6:      6            3              12               13       RL     2
 7:      7            3              14               15       RR     2
 8:      8            4              NA               NA      LLL     3
 9:      9            4              NA               NA      LLR     3
10:     10            5              NA               NA      LRL     3
11:     11            5              NA               NA      LRR     3
12:     12            6              NA               NA      RLL     3
13:     13            6              NA               NA      RLR     3
14:     14            7              NA               NA      RRL     3
15:     15            7              NA               NA      RRR     3

# Remove nodes 14 & 15 to get a tree with 13 nodes. (We're simply removing rows from a data.frame)
btree <- btree[!NodeId %in% c(14, 15)]

# Plot the tree
plot_btree(btree, labelCol = "NodeId")

enter image description here

0

All Articles