当前位置:网站首页>2022-01-25: serialize and deserialize n-ary tree. Serialization means that a
2022-01-25: serialize and deserialize n-ary tree. Serialization means that a
2022-06-23 02:59:00 【Fuda scaffold constructor's daily question】
2022-01-25: Serialization and deserialization N Fork tree .
Serialization is the process of converting a data structure into a sequence of bits , So you can store it in a file or in a memory buffer , So that the structure can be restored later in the same or different computer environment .
Design a serialization and deserialization N Fork tree algorithm .
One N A fork tree means that each node has no more than N A rooted tree with child nodes .
serialize / There are no restrictions on the algorithm implementation of deserialization algorithm .
You just need to guarantee N The fork tree can be serialized into a string and the string can be deserialized into the original tree structure .
Be careful :
N The scope of 1, 1000
Don't use class members / Global variables / Static variables to store state .
Your serialization and deserialization algorithms should be stateless .
Power button 428.
answer 2022-01-25:
Natural intelligence . recursive .
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
a := NewNode2(1)
b := NewNode2(2)
c := NewNode2(3)
d := NewNode2(4)
e := NewNode2(5)
f := NewNode2(6)
g := NewNode2(7)
a.children = append(a.children, b)
a.children = append(a.children, c)
a.children = append(a.children, d)
b.children = append(b.children, e)
b.children = append(b.children, f)
e.children = append(e.children, g)
codec := &Codec{}
content := codec.serialize(a)
fmt.Println(content)
head := codec.deserialize(content)
fmt.Println(codec.serialize(head) == content)
}
// Don't submit this class
type Node struct {
val int
children []*Node
}
func NewNode1() *Node {
ret := &Node{}
ret.children = make([]*Node, 0)
return ret
}
func NewNode2(_val int) *Node {
ret := &Node{}
ret.val = _val
ret.children = make([]*Node, 0)
return ret
}
func NewNode3(_val int, _children []*Node) *Node {
ret := &Node{}
ret.val = _val
ret.children = _children
return ret
}
// Submit the following class
type Codec struct {
}
func (this *Codec) serialize(root *Node) string {
if root == nil { // Empty tree ! Go straight back to #
return "#"
}
builder := ""
this.serial(&builder, root)
return builder
}
// Now comes head
func (this *Codec) serial(builder *string, head *Node) {
*builder += fmt.Sprint(head.val) + ","
if len(head.children) > 0 {
*builder += "[,"
for _, next := range head.children {
this.serial(builder, next)
}
*builder += "],"
}
}
func (this *Codec) deserialize(data string) *Node {
if data == "#" {
return nil
}
splits := strings.Split(data, ",")
queue := make([]string, 0)
for _, str := range splits {
queue = append(queue, str)
}
return this.deserial(&queue)
}
func (this *Codec) deserial(queue *[]string) *Node {
v, _ := strconv.Atoi((*queue)[0])
cur := NewNode2(v)
*queue = (*queue)[1:]
cur.children = make([]*Node, 0)
if len(*queue) > 0 && (*queue)[0] == "[" {
*queue = (*queue)[1:]
for (*queue)[0] != "]" {
child := this.deserial(queue)
cur.children = append(cur.children, child)
}
*queue = (*queue)[1:]
}
return cur
}The results are as follows :
边栏推荐
- CVE-2021-21973 Vmware Vcenter SSRF POC
- The metauniverse is just a cloak for future technological evolution
- How to generate DataMatrix code in batch through TXT file
- Weekly Postgres world news 2022w04
- Chaoscraft: join your girlfriend in Hackathon show -- Interview with the skate team
- Reading redis source code (VI) multi threading of redis 6.0
- A bit about the state machine (FSM SMR DFSM)
- Troubleshooting and optimization of easynvr version 5.0 Video Square snapshot not displayed
- DAAS architecture and Implementation (I)
- Delta oscillation in EEG
猜你喜欢

How to store, manage and view family photos in an orderly manner?

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027

8. greed

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030

5. concept of ruler method

Spark broadcast variables and accumulators (cases attached)

6. template for integer and real number dichotomy

C language series - Section 4 - arrays
What is sitelock? What is the function?

Vulnhub DC-5
随机推荐
The difference between script in head and body
Salesforce fileUpload (III) how to display uploaded images
Online signature with canvas
A penetration of an internal self built shooting range
Initial xxE
2022-01-27: heater. Winter has come. Your task is to design a
The priority supplier field in the purchase information record of SAP mm primary level
Simple implementation of promise basic method
C language series - Section 4 - arrays
Hypervisor Necromancy; Recover kernel protector (1)
What is a smart farm?
DAAS architecture and Implementation (I)
February 3, 2022: a group of people (two or more) want to meet at the same place
Quic implementation in rust --- Quinn
The performance of the new Tokio scheduler is improved by 10 times
Applet control version update best practices
How to store, manage and view family photos in an orderly manner?
How does the easyplayer streaming video player set up tiling?
SQLSERVER database restore stored procedure script
Use Sakura FRP intranet penetration service to build your own website / game server