Efficient way of flattening a recursive data structure in golang

huangapple go评论90阅读模式
英文:

Efficient way of flattening a recursive data structure in golang

问题

我有一个递归的数据结构,可以包含几种不同类型的数据:

type Data interface {
    // 一些方法
}
type Pair struct { // 实现了Data接口
    fst Data
    snd Data
}
type Number float64 // 实现了Data接口

现在我想将一系列的Pair扁平化为[]Data。然而,fst字段中的Data不应该被扁平化,只有snd中的数据应该被扁平化。例如:

chain := Pair{Number(1.0), Pair{Number(2.0), Pair{Number(3.0), nil}}}
chain2 := Pair{Pair{Number(1.0), Number(4.0)}, Pair{Number(2.0), Pair{Number(3.0), nil}}}

变成:

data := []Data{Number(1.0), Number(2.0), Number(3.0)}
data2 := []Data{Pair{Number(1.0), Number(4.0)}, Number(2.0), Number(3.0)}

我的朴素方法是:

var data []Data
chain := Pair{Number(1.0), Pair{Number(2.0), Pair{Number(3.0), nil}}}

for chain != nil {
    data = append(data, chain.fst)
    chain = chain.snd
}

是否有一种更高效的方法可以将变量chain中的数据结构扁平化为[]Data数组?

英文:

I have a recursive data structure that can contain a few different type of data:

type Data interface{
 // Some methods
}
type Pair struct { // implements Data
  fst Data
  snd Data
}
type Number float64 // implements Data

Now I want to flatten a chain of Pairs into a []Data. However, the Data in the fst field should not be flattened, only data in snd should be flattened. E.g:

chain := Pair{Number(1.0), Pair{Number(2.0), Pair{Number(3.0), nil}}}
chain2 := Pair{Pair{Number(1.0), Number(4.0)}, Pair{Number(2.0), Pair{Number(3.0), nil}}}

becomes:

data := []Data{Number(1.0), Number(2.0), Number(3.0)}
data2 := []Data{Pair{Number(1.0), Number(4.0)}, Number(2.0), Number(3.0)}

My naive approach would be:

var data []Data
chain := Pair{Number(1.0), Pair{Number(2.0), Pair{Number(3.0), nil}}}

for chain != nil {
    data = append(data, chain.fst)
    chain = chain.snd
}

Is there a more efficient approach that can flatten a data structure like the one in the variable chain into an []Data array?

答案1

得分: 2

你可以使用递归函数。在下行过程中,累加配对数,在底部分配数组,在返回过程中,从后往前填充数组。

如果你需要支持任意树形结构,可以在Data中添加一个size方法,然后进行另一次树遍历来实际填充数组。

英文:

You can use a recursive function. On the way down, add up the number of pairs, at the bottom, allocate the array, and on the way back up, fill the array from back to front.

If you need to support arbitrary trees, you can add a size method to Data, and then do another tree traversal to actually fill the array.

答案2

得分: 0

嗯,你的简单方法对于嵌套在fst中的Pair是不起作用的。如果你有chain := Pair{Pair{Number(1.0), Number(2.0)}, Number{3.0}},它最终会变成[]Data{Pair{Number(1.0), Number(2.0)}, Number{3.0}}。这是一个固有的递归问题,为什么不将其实现为递归呢?

我建议在你的接口中添加一个flatten()方法。Pair可以递归地嵌套自己,而Number只需返回其值。

这是一个完全可工作的示例,其中包含一些最小化的测试:

package main

import "fmt"

type Data interface {
	flatten() []Data
}

type Pair struct {
	fst Data
	snd Data
}

type Number float64

func (p Pair) flatten() []Data {
	res := []Data{}
	if p.fst != nil {
		res = append(res, p.fst.flatten()...)
	}
	if p.snd != nil {
		res = append(res, p.snd.flatten()...)
	}
	return res
}

func (n Number) flatten() []Data {
	return []Data{n}
}

func main() {
	tests := []Data{
		Pair{Number(1.0), Pair{Number(2.0), Pair{Number(3.0), nil}}},
		Pair{Pair{Number(1.0), Number(2.0)}, Number(3.0)},
		Pair{Pair{Pair{Number(1.0), Number(2.0)}, Pair{Number(3.0), Number(4.0)}}, Pair{Pair{Number(5.0), Number(6.0)}, Number(7.0)}},
		Number(1.0),
	}
	for _, t := range tests {
		fmt.Printf("原始数据: %v\n", t)
		fmt.Printf("展开后的数据: %v\n", t.flatten())
	}
}

(这假设顶层输入的Data永远不为nil)。

代码输出:

原始数据: {1 {2 {3 <nil>}}}
展开后的数据: [1 2 3]
原始数据: {{1 2} 3}
展开后的数据: [1 2 3]
原始数据: {{{1 2} {3 4}} {{5 6} 7}}
展开后的数据: [1 2 3 4 5 6 7]
原始数据: 1
展开后的数据: [1]
英文:

Huh, your naive approach doesn't work for Pairs nested inside fst. If you had chain := Pair{Pair{Number(1.0), Number(2.0)}, Number{3.0}}, it would end up as []Data{Pair{Number(1.0), Number(2.0)}, Number{3.0}}. This is an inherently recursive problem, so why not implement it as such?

I suggest adding a flatten() method to your interface. Pairs can just recursively nest themselves, and Numbers just return their value.

Here's a fully working example with some minimal testing:

package main

import &quot;fmt&quot;

type Data interface {
	flatten() []Data
}

type Pair struct {
	fst Data
	snd Data
}

type Number float64

func (p Pair) flatten() []Data {
	res := []Data{}
	if p.fst != nil {
		res = append(res, p.fst.flatten()...)
	}
	if p.snd != nil {
		res = append(res, p.snd.flatten()...)
	}
	return res
}

func (n Number) flatten() []Data {
	return []Data{n}
}

func main() {
	tests := []Data{
		Pair{Number(1.0), Pair{Number(2.0), Pair{Number(3.0), nil}}},
		Pair{Pair{Number(1.0), Number(2.0)}, Number(3.0)},
		Pair{Pair{Pair{Number(1.0), Number(2.0)}, Pair{Number(3.0), Number(4.0)}}, Pair{Pair{Number(5.0), Number(6.0)}, Number(7.0)}},
		Number(1.0),
	}
	for _, t := range tests {
		fmt.Printf(&quot;Original: %v\n&quot;, t)
		fmt.Printf(&quot;Flattened: %v\n&quot;, t.flatten())
	}
}

(This assumes that the top-level input Data is never nil).

The code prints:

Original: {1 {2 {3 &lt;nil&gt;}}}
Flattened: [1 2 3]
Original: {{1 2} 3}
Flattened: [1 2 3]
Original: {{{1 2} {3 4}} {{5 6} 7}}
Flattened: [1 2 3 4 5 6 7]
Original: 1
Flattened: [1]

答案3

得分: 0

根据建议,为这个问题编写一个递归函数是最好的选择。但也可以编写一个非递归版本(在我看来,递归版本可能更清晰):

func flatten(d Data) []Data {
    var res []Data

    stack := []Data{d}

    for {
        if len(stack) == 0 {
            break
        }
        switch x := stack[len(stack)-1].(type) {
        case Pair:
            stack[len(stack)-1] = x.snd
            stack = append(stack, x.fst)
        case Number:
            res = append(res, x)
            stack = stack[:len(stack)-1]
        default:
            if x == nil {
                stack = stack[:len(stack)-1]
            } else {
                panic("INVALID TYPE")
            }
        }
    }

    return res
}
英文:

As suggested, writing a recursive function fits best for this problem. But it's also possible to write a non-recursive version (IMHO recursive version would be more clear):

func flatten(d Data) []Data {
	var res []Data

	stack := []Data{d}

	for {
		if len(stack) == 0 {
			break
		}
		switch x := stack[len(stack)-1].(type) {
		case Pair:
			stack[len(stack)-1] = x.snd
			stack = append(stack, x.fst)
		case Number:
			res = append(res, x)
			stack = stack[:len(stack)-1]
		default:
			if x == nil {
				stack = stack[:len(stack)-1]
			} else {
				panic(&quot;INVALID TYPE&quot;)
			}
		}
	}

	return res
}

huangapple
  • 本文由 发表于 2017年8月18日 19:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/45755873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定