tree finally works on all 3 test files

This commit is contained in:
bjt-user 2024-05-06 01:09:44 +02:00
parent 1ef07c59c6
commit 67b64d113d
2 changed files with 29 additions and 6 deletions

View File

@ -131,6 +131,5 @@ func main() {
}
}
my_children := get_child_indices(0, headings)
fmt.Printf("%v\n", my_children)
tree(-1, "", headings)
}

32
tree.go
View File

@ -1,5 +1,9 @@
package main
import (
"fmt"
)
func get_children(header_index int, headings []heading) []heading {
var children []heading
@ -47,17 +51,21 @@ func get_root_children(headings []heading) []int {
return root_children
}
// TODO: -1 for root -> get indices for headers lying directly under root
func get_child_indices(header_index int, headings []heading) []int {
var children []int
parent_level := get_heading_level(headings[header_index].text)
if header_index == -1 {
children = get_root_children(headings)
return children
}
parent_level := headings[header_index].level
if header_index >= (len(headings) - 1) {
return nil
}
next_level := get_heading_level(headings[header_index+1].text)
next_level := headings[header_index+1].level
child_level := 0
@ -69,7 +77,7 @@ func get_child_indices(header_index int, headings []heading) []int {
cur_level := 0
for i := (header_index + 1); i < len(headings); i++ {
cur_level = get_heading_level(headings[i].text)
cur_level = headings[i].level
if cur_level == child_level {
children = append(children, i)
@ -82,3 +90,19 @@ func get_child_indices(header_index int, headings []heading) []int {
return children
}
func tree(index int, prefix string, headings []heading) {
children := get_child_indices(index, headings)
//fmt.Printf("children: %v\n", children)
for index, child_index := range children {
//fmt.Printf("child_index: %d\n", child_index)
if index == (len(children) - 1) {
fmt.Println(prefix+"`--", headings[child_index].text)
tree(child_index, prefix+" ", headings)
} else {
fmt.Println(prefix+"|--", headings[child_index].text)
tree(child_index, prefix+"| ", headings)
}
}
}