-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryTree_MDPyramid_.R
95 lines (80 loc) · 2.35 KB
/
binaryTree_MDPyramid_.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# https://rstudio.cloud/project/152401
rm(list = ls())
gc()
pacman::p_load(tidyverse, data.table, stringr, magrittr,glue,data.tree,treemap)
file <- "https://raw.githubusercontent.com/vinciuna/MDPyramid/master/binaryTree_test.txt"
dt <- readLines(con = file)
max <- dt %>% map_int(.,~str_count(.,"\\s+")) %>% max()
dt %<>% map_chr(., ~str_c(., str_c(rep(" 0",max-str_count(.,"\\s")),collapse = ""),sep=""))
dt <- str_split(dt,"\\s")
dt <- pmap_dfc(dt,rbind) %>% mutate_all(.,as.integer)
dt
#----------------------------------------------------------------------------
#data cleansing
dt_clean <- function(x,p) {
if (p==1){
x[!(seq(x)%%2 == x%%2)] <- 0} else
{x[(seq(x)%%2 == x%%2)] <- 0}
x[x==0] <- -Inf
x
}
#summing vectors
vsum <- function(jj){
#if (v1)
v1 <- xs[ii]; v2 <- c(x2[jj],x2[jj+1])
sums=v1+v2
max=max(sums)
adds=list(v1, v2)
#browser()
if (max!=-Inf){
list(max=max,
sums=sums,
adds=adds)
} else return(-Inf)
}
#----------------------------------------------------------------------------
#data setup
par <- dt[[1,1]]%%2
dt %<>% mutate_all(~dt_clean(.,par))
dt
n <- nrow(dt)
x1 <- dt[1,] %>% as.numeric(.)
xs <-x1
path <- as.list(1:n)
lc1 <- which(x1!=-Inf)
path[[1]] <- rep(x1[lc1],2)
#loop over nodes
for (i in 2:n) {
x2 <- dt[i,] %>% as.numeric(.)
lc2 <- which(x2!=-Inf)
lc2 <- lc2[lc2 %in% c(lc1,max(lc1)+1)]
ii <- 1
for (j in lc1) {
rez <- vsum(j)
kmax <- which(rez$sums==rez$max)
if( (max(xs[ii])<rez$max)&(length(lc1)>1) ) {
xs[ii] <- rez$max
lc0[ii] <- j+kmax-1
path[[i]][[ii]] <- rez$adds[[2]][kmax]
} else {
xs <- rez$sums
lc0 <- lc2
path[[i]] <- rez$adds[[2]]
}
ii <- ii+1
}
x1 <- x2
lc1 <- lc0
}
glue("path1: MAX sum={map(path,1) %>% unlist() %>% sum()} \t adds={map(path,1) %>% unlist() %>% paste0(.,collapse = '+')}
path2: MAX sum={map(path,2) %>% unlist() %>% sum()} \t adds={map(path,2) %>% unlist() %>% paste0(.,collapse = '+')}")
pathTree <- data.frame(path=c(map(path,1) %>% unlist()))
pathTree$pathString <- paste("root",
pathTree$path,
sep = "/")
pathTree <- as.Node(path)
print(pathTree)
print(pathTree)
plot(pathTree)
x <- getURL(, ssl.verifypeer = FALSE)
eval(parse(text = x))