datastructures

package
v0.0.0-...-c23374a Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultFreeListSize = 32
)

Variables

This section is empty.

Functions

func AVLInOrder

func AVLInOrder(root *AVLNode, action action)

func AVLMax

func AVLMax(a, b int) int

func AVLPostOrder

func AVLPostOrder(root *AVLNode, action action)

func AVLPreOrder

func AVLPreOrder(root *AVLNode, action action)

Types

type AVLNode

type AVLNode struct {
	Val         int
	Height      int
	Left, Right *AVLNode
}

func Insert

func Insert(n *AVLNode, key int) *AVLNode

func RotateLeft

func RotateLeft(n *AVLNode) *AVLNode

func RotateLeftRight

func RotateLeftRight(n *AVLNode) *AVLNode

func RotateRight

func RotateRight(n *AVLNode) *AVLNode

func RotateRightLeft

func RotateRightLeft(n *AVLNode) *AVLNode

type BTree

type BTree struct {
	// contains filtered or unexported fields
}

BTree is an implementation of a B-Tree.

BTree stores Item instances in an ordered structure, allowing easy insertion, removal, and iteration.

Write operations are not safe for concurrent mutation by multiple goroutines, but Read operations are.

func New

func New(degree int) *BTree

New creates a new B-Tree with the given degree.

New(2), for example, will create a 2-3-4 tree (each node contains 1-3 items and 2-4 children).

func NewWithFreeList

func NewWithFreeList(degree int, f *FreeList) *BTree

NewWithFreeList creates a new B-Tree that uses the given node free list.

func (*BTree) Ascend

func (t *BTree) Ascend(iterator ItemIterator)

Ascend calls the iterator for every value in the tree within the range [first, last], until iterator returns false.

func (*BTree) AscendGreaterOrEqual

func (t *BTree) AscendGreaterOrEqual(pivot BTreeItem, iterator ItemIterator)

AscendGreaterOrEqual calls the iterator for every value in the tree within the range [pivot, last], until iterator returns false.

func (*BTree) AscendLessThan

func (t *BTree) AscendLessThan(pivot BTreeItem, iterator ItemIterator)

AscendLessThan calls the iterator for every value in the tree within the range [first, pivot), until iterator returns false.

func (*BTree) AscendRange

func (t *BTree) AscendRange(greaterOrEqual, lessThan BTreeItem, iterator ItemIterator)

AscendRange calls the iterator for every value in the tree within the range [greaterOrEqual, lessThan), until iterator returns false.

func (*BTree) Clear

func (t *BTree) Clear(addNodesToFreelist bool)

Clear removes all items from the btree. If addNodesToFreelist is true, t's nodes are added to its freelist as part of this call, until the freelist is full. Otherwise, the root node is simply dereferenced and the subtree left to Go's normal GC processes.

This can be much faster than calling Delete on all elements, because that requires finding/removing each element in the tree and updating the tree accordingly. It also is somewhat faster than creating a new tree to replace the old one, because nodes from the old tree are reclaimed into the freelist for use by the new one, instead of being lost to the garbage collector.

This call takes:

O(1): when addNodesToFreelist is false, this is a single operation.
O(1): when the freelist is already full, it breaks out immediately
O(freelist size):  when the freelist is empty and the nodes are all owned
    by this tree, nodes are added to the freelist until full.
O(tree size):  when all nodes are owned by another tree, all nodes are
    iterated over looking for nodes to add to the freelist, and due to
    ownership, none are.

func (*BTree) Clone

func (t *BTree) Clone() (t2 *BTree)

Clone clones the btree, lazily. Clone should not be called concurrently, but the original tree (t) and the new tree (t2) can be used concurrently once the Clone call completes.

The internal tree structure of b is marked read-only and shared between t and t2. Writes to both t and t2 use copy-on-write logic, creating new nodes whenever one of b's original nodes would have been modified. Read operations should have no performance degredation. Write operations for both t and t2 will initially experience minor slow-downs caused by additional allocs and copies due to the aforementioned copy-on-write logic, but should converge to the original performance characteristics of the original tree.

func (*BTree) Delete

func (t *BTree) Delete(item BTreeItem) BTreeItem

Delete removes an item equal to the passed in item from the tree, returning it. If no such item exists, returns nil.

func (*BTree) DeleteMax

func (t *BTree) DeleteMax() BTreeItem

DeleteMax removes the largest item in the tree and returns it. If no such item exists, returns nil.

func (*BTree) DeleteMin

func (t *BTree) DeleteMin() BTreeItem

DeleteMin removes the smallest item in the tree and returns it. If no such item exists, returns nil.

func (*BTree) Descend

func (t *BTree) Descend(iterator ItemIterator)

Descend calls the iterator for every value in the tree within the range [last, first], until iterator returns false.

func (*BTree) DescendGreaterThan

func (t *BTree) DescendGreaterThan(pivot BTreeItem, iterator ItemIterator)

DescendGreaterThan calls the iterator for every value in the tree within the range (pivot, last], until iterator returns false.

func (*BTree) DescendLessOrEqual

func (t *BTree) DescendLessOrEqual(pivot BTreeItem, iterator ItemIterator)

DescendLessOrEqual calls the iterator for every value in the tree within the range [pivot, first], until iterator returns false.

func (*BTree) DescendRange

func (t *BTree) DescendRange(lessOrEqual, greaterThan BTreeItem, iterator ItemIterator)

DescendRange calls the iterator for every value in the tree within the range [lessOrEqual, greaterThan), until iterator returns false.

func (*BTree) Get

func (t *BTree) Get(key BTreeItem) BTreeItem

Get looks for the key item in the tree, returning it. It returns nil if unable to find that item.

func (*BTree) Has

func (t *BTree) Has(key BTreeItem) bool

Has returns true if the given key is in the tree.

func (*BTree) Len

func (t *BTree) Len() int

Len returns the number of items currently in the tree.

func (*BTree) Max

func (t *BTree) Max() BTreeItem

Max returns the largest item in the tree, or nil if the tree is empty.

func (*BTree) Min

func (t *BTree) Min() BTreeItem

Min returns the smallest item in the tree, or nil if the tree is empty.

func (*BTree) ReplaceOrInsert

func (t *BTree) ReplaceOrInsert(item BTreeItem) BTreeItem

ReplaceOrInsert adds the given item to the tree. If an item in the tree already equals the given one, it is removed from the tree and returned. Otherwise, nil is returned.

nil cannot be added to the tree (will panic).

type BTreeItem

type BTreeItem interface {
	Less(than BTreeItem) bool
}

type BinarySearchTree

type BinarySearchTree struct {
	Root *TreeNode
}

func (*BinarySearchTree) Delete

func (t *BinarySearchTree) Delete(node *TreeNode)

func (*BinarySearchTree) InOrderTreeWalk

func (t *BinarySearchTree) InOrderTreeWalk() []int

func (*BinarySearchTree) Insert

func (t *BinarySearchTree) Insert(item int)

func (*BinarySearchTree) Invert

func (t *BinarySearchTree) Invert() *BinarySearchTree

invert

func (*BinarySearchTree) Join

func (*BinarySearchTree) Minimum

func (t *BinarySearchTree) Minimum() *TreeNode

func (*BinarySearchTree) PostOrderTreeWalk

func (t *BinarySearchTree) PostOrderTreeWalk() []int

func (*BinarySearchTree) PreOrderTreeWalk

func (t *BinarySearchTree) PreOrderTreeWalk() []int

func (*BinarySearchTree) Replace

func (t *BinarySearchTree) Replace(src, dest *TreeNode)

func (*BinarySearchTree) Search

func (t *BinarySearchTree) Search(item int) *TreeNode

func (*BinarySearchTree) Size

func (t *BinarySearchTree) Size() int

type FreeList

type FreeList struct {
	// contains filtered or unexported fields
}

FreeList represents a free list of btree nodes. By default each BTree has its own FreeList, but multiple BTrees can share the same FreeList. Two Btrees using the same freelist are safe for concurrent write access.

func NewFreeList

func NewFreeList(size int) *FreeList

NewFreeList creates a new free list. size is the maximum size of the returned free list.

type Graph

type Graph struct {
	Vertices map[string]*Vertex
	// contains filtered or unexported fields
}

func (*Graph) AddEdge

func (g *Graph) AddEdge(a, b string, bidirictional bool)

func (*Graph) AddVertex

func (g *Graph) AddVertex(value string) *Vertex

func (*Graph) String

func (g *Graph) String()

String Prints String Presentation of the Graph

type HeapPriorityQueue

type HeapPriorityQueue []*PriorityQueueItem

func NewHeapPriorityQueue

func NewHeapPriorityQueue() *HeapPriorityQueue

func (HeapPriorityQueue) IsEmpty

func (pq HeapPriorityQueue) IsEmpty() bool

func (HeapPriorityQueue) Len

func (pq HeapPriorityQueue) Len() int

func (HeapPriorityQueue) Less

func (pq HeapPriorityQueue) Less(i, j int) bool

func (HeapPriorityQueue) Next

func (pq HeapPriorityQueue) Next() interface{}

func (*HeapPriorityQueue) Pop

func (pq *HeapPriorityQueue) Pop() interface{}

func (*HeapPriorityQueue) Push

func (pq *HeapPriorityQueue) Push(i interface{})

func (HeapPriorityQueue) Swap

func (pq HeapPriorityQueue) Swap(i, j int)

type Int

type Int int

Int implements the Item interface for integers.

func (Int) Less

func (a Int) Less(b BTreeItem) bool

Less returns true if int(a) < int(b).

type ItemIterator

type ItemIterator func(i BTreeItem) bool

ItemIterator allows callers of Ascend* to iterate in-order over portions of the tree. When this function returns false, iteration will stop and the associated Ascend* function will immediately return.

type LinkedList

type LinkedList struct {
	Previous *LinkedList
	Val      interface{}
	Next     *LinkedList
}

func (*LinkedList) Add

func (l *LinkedList) Add(item interface{})

func (*LinkedList) Get

func (l *LinkedList) Get(i int) *LinkedList

func (*LinkedList) Len

func (l *LinkedList) Len() int

func (*LinkedList) Remove

func (l *LinkedList) Remove(i int)

type Node

type Node struct {
	Val      interface{}
	Parent   *Node
	Children []*Node
}

func (*Node) Delete

func (node *Node) Delete()

func (*Node) Insert

func (node *Node) Insert(item interface{})

type Path

type Path struct {
	ID       string
	Vertices []*WeightedVertex
	Weight   int
}

type Paths

type Paths []*Path

func (Paths) Len

func (p Paths) Len() int

func (Paths) Less

func (p Paths) Less(i, j int) bool

func (Paths) Swap

func (p Paths) Swap(i, j int)

type PriorityQueue

type PriorityQueue struct {
	// contains filtered or unexported fields
}

func NewPriorityQueue

func NewPriorityQueue() *PriorityQueue

func (*PriorityQueue) IsEmpty

func (q *PriorityQueue) IsEmpty() bool

func (*PriorityQueue) Pop

func (q *PriorityQueue) Pop() *Path

func (*PriorityQueue) Push

func (q *PriorityQueue) Push(item *Path)

type PriorityQueueItem

type PriorityQueueItem struct {
	Val    interface{}
	Weight int
	Index  int // The index of the item in the heap.
}

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

func NewQueue

func NewQueue() *Queue

NewQueue returns new instance pointer of Queue struct

func (*Queue) IsEmpty

func (q *Queue) IsEmpty() bool

func (*Queue) Pop

func (q *Queue) Pop() interface{}

Pop removes first item added to the queue

func (*Queue) Push

func (q *Queue) Push(item interface{})

Push adds an Item to the end of the queue

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack

func NewStack

func NewStack() *Stack

New returns new instance pointer of Stack struct

func (*Stack) IsEmpty

func (s *Stack) IsEmpty() bool

func (*Stack) Len

func (s *Stack) Len() int

func (*Stack) Pop

func (s *Stack) Pop() interface{}

Pop removes an Item from the top of the stack

func (*Stack) Push

func (s *Stack) Push(item interface{})

Push adds an Item to the top of the stack

type TreeNode

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

func (*TreeNode) InOrderTreeWalk

func (n *TreeNode) InOrderTreeWalk() []int

func (*TreeNode) Insert

func (n *TreeNode) Insert(item int)

func (*TreeNode) IsContinous

func (n *TreeNode) IsContinous() bool

A tree is Continuous tree if in each root to leaf path, absolute difference between keys of two adjacent is 1. We are given a binary tree, we need to check if tree is continuous or not.

func (*TreeNode) IsFoldable

func (n *TreeNode) IsFoldable() bool

A tree can be folded if left and right subtrees of the tree are structure wise mirror image of each other. An empty tree is considered as foldable.

func (*TreeNode) Minimum

func (n *TreeNode) Minimum() *TreeNode

func (*TreeNode) PostOrderTreeWalk

func (n *TreeNode) PostOrderTreeWalk() []int

func (*TreeNode) PreOrderTreeWalk

func (n *TreeNode) PreOrderTreeWalk() []int

func (*TreeNode) Search

func (n *TreeNode) Search(item int) *TreeNode

func (*TreeNode) Size

func (n *TreeNode) Size() int

func (*TreeNode) Sucessor

func (n *TreeNode) Sucessor() *TreeNode

type Vertex

type Vertex struct {
	Val   string
	Edges map[string]*Vertex
}

func (*Vertex) String

func (n *Vertex) String() string

type WeightedEdge

type WeightedEdge struct {
	Vertex *WeightedVertex
	Weight int
}

type WeightedGraph

type WeightedGraph struct {
	Vertices map[string]*WeightedVertex
	// contains filtered or unexported fields
}

func (*WeightedGraph) AddEdge

func (g *WeightedGraph) AddEdge(a, b string, weight int, bidirictional bool)

func (*WeightedGraph) AddVertex

func (g *WeightedGraph) AddVertex(value string) *WeightedVertex

func (*WeightedGraph) String

func (g *WeightedGraph) String()

String Prints String Presentation of the Graph

type WeightedVertex

type WeightedVertex struct {
	Val   string
	Edges map[string]*WeightedEdge
}

func (*WeightedVertex) String

func (n *WeightedVertex) String() string

Jump to

Keyboard shortcuts

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