get_children function works

This commit is contained in:
bjt-user 2024-05-05 16:38:35 +02:00
parent d4c590f2ec
commit 5125b7bc0a
2 changed files with 36 additions and 0 deletions

View File

@ -130,4 +130,7 @@ func main() {
fmt.Printf("Header %d occurs %d times.\n", (i + 1), header_level_count[i])
}
}
my_children := get_children(0, headings)
fmt.Printf("%v\n", my_children)
}

33
tree.go Normal file
View File

@ -0,0 +1,33 @@
package main
func get_children(header_index int, headings []heading) []heading {
var children []heading
parent_level := get_heading_level(headings[header_index].text)
next_level := get_heading_level(headings[header_index+1].text)
child_level := 0
if next_level <= parent_level {
return nil
} else {
child_level = next_level
}
cur_level := 0
for i := (header_index + 1); i < len(headings); i++ {
cur_level = get_heading_level(headings[i].text)
if cur_level == child_level {
children = append(children, headings[i])
}
if cur_level < child_level {
break
}
}
return children
}