Golang:递归数据结构 [英] Golang: Recursive data structures

查看:128
本文介绍了Golang:递归数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我有一个简单的问题...我试图在切片中重现Golang的递归数据结构。 type Trie map [byte] Trie 

现在我有一些使用递归数据的'粗糙'源代码下面的结构与切片和一切工作正常,除了我的类型结构是一个结构,而不是一个结构片。理想情况下,我希望我的类型递归数据结构是Trie元素Trie {byte,[] Trie}的一部分。希望这是有道理的?现在我有一个类型是Trie结构{byte,[] Trie}。

  type Trie struct {
elem byte
others [] Trie
}

也许这会有所帮助。当我创建我的Trie片时,我使用这个函数。

  func CreateTrie()[] Trie {
return make([] Trie,0,13)
}

我想要Trie切片的定义可以像这样创建切片。

  func CreateTrie()Trie {
return make(Trie,0,13)
}

或者我有使用我的第一个(唯一的)解决方案切片?

这是你要找的东西吗?


I have a simple question... I'm trying to reproduce this recursive data structure in Golang with slices.

type Trie map[byte]Trie

Right now I have some 'rough' source code using the recursive data structure below with slices and everything works fine except my typed structure is a structure and not a slice of structures. Ideally I would like my typed recursive data structure to be a slice of Trie's which has elements Trie{byte, []Trie}. Hope that makes sense? Right now I have a type which is a Trie struct{byte, []Trie}.

type Trie struct {
elem byte
others []Trie
} 

Maybe this will help. When I create my Trie of slices right now I use this function.

func CreateTrie() []Trie {
    return make([]Trie, 0, 13)
}

I would like to have the Trie of slices defined in such a way that I could create the slices like this.

func CreateTrie() Trie {
    return make(Trie, 0, 13)
}

Is this possible with slices or do I have use my first(only) solution for slices?

解决方案

Is this what you're looking for?

这篇关于Golang:递归数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆