treenode

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2022 License: MIT Imports: 4 Imported by: 1

README ¶

TreeNode 🌲🌲🌲🌲

Binary Tree Node struct with LeetCode compatible Serialize / Deserialize format

Build Status

thanks to https://github.com/egregors/TreeNode

Install

go get gitee.com/masx200/TreeNode

Usage

This package provides TreeNode data struct and a few function for serialization and deserialization respectively.

package main

import (
	"log"

	tn "gitee.com/masx200/TreeNode"
)

func main() {
	data := "[1,2,3,null,null,4,5]"
	root, err := tn.NewTreeNode(data)
	if err != nil {
		log.Fatal(err)
	}

	root.Right.Left.Val = 42
	newData := root.String()
	...
}

A NewTreeNode(data string) (*TreeNode, error) constructor expect LeetCode style formatted string. Otherwise, to serialize a tree just call String() for TreeNode object.

Benchmarks

goos: darwin
goarch: amd64
pkg: github.com/egregors/TreeNode
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkEmptyTree-12           13766786                77.96 ns/op
Benchmark1_5Tree-12              2645138               448.3 ns/op

Contributing

Bug reports, bug fixes and new features are always welcome. Please open issues and submit pull requests for any new code.

Documentation ¶

Index ¶

Constants ¶

View Source
const (
	EmptyNodeMark = "null"
	Separator     = ","
)

EmptyNodeMark is used to mark empty node in serialized string

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type NodeQueue ¶

type NodeQueue []*TreeNode

NodeQueue is a simple queue of TreeNode pointers

func (*NodeQueue) IsEmpty ¶

func (q *NodeQueue) IsEmpty() bool

IsEmpty returns true if the queue is empty

func (*NodeQueue) Pop ¶

func (q *NodeQueue) Pop() *TreeNode

Pop removes the first node from the queue and returns it.

func (*NodeQueue) Push ¶

func (q *NodeQueue) Push(nodes ...*TreeNode)

Push adds a nodes to the end of the queue. You can push a few nodes at a time. They will be added like a "tail": ] q == [a, b, c] => q.Push(d, e, f) -> q == [a, b, c, d, e, f]

type TreeNode ¶

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

TreeNode binary tree node representation with Int value

func NewTreeNode ¶

func NewTreeNode(data string) (*TreeNode, error)

NewTreeNode deserializes raw data and creates a new TreeNode

func (TreeNode) String ¶

func (t TreeNode) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL