Documentation
¶
Index ¶
- type BTree
- type BTreeNode
- type BinaryIndexedTree
- type BinarySearchTree
- func (b *BinarySearchTree) Clear()
- func (b *BinarySearchTree) ConvertFromDoubleLinkedList(head *BinaryTreeNode, length int)
- func (b *BinarySearchTree) ConvertToDoubleLinkedList() (head *BinaryTreeNode, tail *BinaryTreeNode)
- func (b *BinarySearchTree) Delete(data interface{}) error
- func (b *BinarySearchTree) GetSize() int
- func (b *BinarySearchTree) Put(key, value interface{})
- func (b *BinarySearchTree) Search(key interface{}) (value interface{})
- func (b *BinarySearchTree) ToSortedSlice() []interface{}
- type BinaryTree
- type BinaryTreeNode
- type Heap
- type HeapType
- type IndexedPriorityQueue
- func (i *IndexedPriorityQueue) ChangeValue(index int, value interface{}) error
- func (i *IndexedPriorityQueue) Contains(index int) bool
- func (i *IndexedPriorityQueue) DeleteValue(index int) error
- func (i *IndexedPriorityQueue) GetValue(index int) (interface{}, error)
- func (i *IndexedPriorityQueue) Insert(index int, value interface{}) error
- func (i *IndexedPriorityQueue) IsEmpty() bool
- func (i *IndexedPriorityQueue) Peek() (index int, value interface{}, err error)
- func (i *IndexedPriorityQueue) Pop() (index int, value interface{}, err error)
- func (i *IndexedPriorityQueue) Size() int
- type SegmentTree
- type SegmentTreeType
- type TrieNode
- type TrieTree
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BTree ¶
type BTree struct { Root *BTreeNode // root has min 2 children if it's not leaf. // contains filtered or unexported fields }
BTree defines a B-Tree
func NewBTree ¶
func NewBTree(order int, comparator shared.Comparator) *BTree
NewBTree creates a new B-Tree
type BTreeNode ¶
type BTreeNode struct { Keys []interface{} // The separation keys, for non-leaf node, count = count(children) - 1 Values []interface{} Children []*BTreeNode // The childrens, maximum at order Parent *BTreeNode }
BTreeNode defines a B-Tree Node
type BinaryIndexedTree ¶
type BinaryIndexedTree struct {
// contains filtered or unexported fields
}
BinaryIndexedTree defines a binary indexes tree
func NewBinaryIndexedTree ¶
func NewBinaryIndexedTree(input []int) *BinaryIndexedTree
NewBinaryIndexedTree creates a binary indexes tree from a given input array.
func (*BinaryIndexedTree) GetSum ¶
func (b *BinaryIndexedTree) GetSum(idx int) int
GetSum returns the sum of values from index 0 to idx.
func (*BinaryIndexedTree) RangeSum ¶
func (b *BinaryIndexedTree) RangeSum(i, j int) int
RangeSum gets the range sum from idx i to j. where i and j is from [0, N).
func (*BinaryIndexedTree) Update ¶
func (b *BinaryIndexedTree) Update(idx, delta int)
Update updates the val at idx by a delta, idx is from 0 to N-1
type BinarySearchTree ¶
type BinarySearchTree struct { BinaryTree // contains filtered or unexported fields }
BinarySearchTree defines a binary search tree
func (*BinarySearchTree) Clear ¶
func (b *BinarySearchTree) Clear()
Clear clears the binary search tree.
func (*BinarySearchTree) ConvertFromDoubleLinkedList ¶
func (b *BinarySearchTree) ConvertFromDoubleLinkedList(head *BinaryTreeNode, length int)
ConvertFromDoubleLinkedList converts double linked list back to
func (*BinarySearchTree) ConvertToDoubleLinkedList ¶
func (b *BinarySearchTree) ConvertToDoubleLinkedList() (head *BinaryTreeNode, tail *BinaryTreeNode)
ConvertToDoubleLinkedList converts the BST to A Double Linked List.
func (*BinarySearchTree) Delete ¶
func (b *BinarySearchTree) Delete(data interface{}) error
Delete deletes a data node from binary search tree.
func (*BinarySearchTree) GetSize ¶
func (b *BinarySearchTree) GetSize() int
GetSize gets the size of the tree.
func (*BinarySearchTree) Put ¶
func (b *BinarySearchTree) Put(key, value interface{})
Put puts a data node into the binary search tree, if the key exists already, update its value.
func (*BinarySearchTree) Search ¶
func (b *BinarySearchTree) Search(key interface{}) (value interface{})
Search searchs value by key.
func (*BinarySearchTree) ToSortedSlice ¶
func (b *BinarySearchTree) ToSortedSlice() []interface{}
ToSortedSlice traverse the tree and store the data into a sorted slice
type BinaryTree ¶
type BinaryTree struct { Root *BinaryTreeNode Comparator shared.Comparator }
BinaryTree defines a general binary tree
func (*BinaryTree) BreadthFirstTraverse ¶
func (b *BinaryTree) BreadthFirstTraverse() []interface{}
BreadthFirstTraverse traverse the tree breadth-first way, a.k.a level by level. The idea is to use a QUEUE to store candidate left and right children along the way.
func (*BinaryTree) DepthFirstTraverse ¶
func (b *BinaryTree) DepthFirstTraverse() []interface{}
DepthFirstTraverse traverse the tree depth-first way. The idea is to use a STACK to store candidate right and left children along the way.
type BinaryTreeNode ¶
type BinaryTreeNode struct { Key interface{} Value interface{} Left *BinaryTreeNode Right *BinaryTreeNode }
BinaryTreeNode defines a tree node of a binary search tree
type Heap ¶
type Heap struct { HeapType HeapType Comparator shared.Comparator // contains filtered or unexported fields }
Heap defines a heap
func (*Heap) GetValues ¶
func (h *Heap) GetValues() []interface{}
GetValues gets the values of the heap
type IndexedPriorityQueue ¶
type IndexedPriorityQueue struct {
// contains filtered or unexported fields
}
IndexedPriorityQueue defines an indexed priority queue based on a heap.
func NewIndexedPriorityQueue ¶
func NewIndexedPriorityQueue(capacity int, heapType HeapType, comparator shared.Comparator) *IndexedPriorityQueue
NewIndexedPriorityQueue creates a new indexed priority queue with capacity, heapType and value comparator specified
func (*IndexedPriorityQueue) ChangeValue ¶
func (i *IndexedPriorityQueue) ChangeValue(index int, value interface{}) error
ChangeValue changes the value at a given index, if the index is out of range or no value was added, error will be returned.
func (*IndexedPriorityQueue) Contains ¶
func (i *IndexedPriorityQueue) Contains(index int) bool
Contains check whether the queue contains a value with the given index.
func (*IndexedPriorityQueue) DeleteValue ¶
func (i *IndexedPriorityQueue) DeleteValue(index int) error
DeleteValue deletes the value at a given index, if the index is out of range or no value was added, error will be returned.
func (*IndexedPriorityQueue) GetValue ¶
func (i *IndexedPriorityQueue) GetValue(index int) (interface{}, error)
GetValue gets value at a given index, if index is out of range or no value was added, error will be returned.
func (*IndexedPriorityQueue) Insert ¶
func (i *IndexedPriorityQueue) Insert(index int, value interface{}) error
Insert inters a value into the queue with a given index, if the index is not valid or is taken, error will be return.
func (*IndexedPriorityQueue) IsEmpty ¶
func (i *IndexedPriorityQueue) IsEmpty() bool
IsEmpty returns whether the queue is empty.
func (*IndexedPriorityQueue) Peek ¶
func (i *IndexedPriorityQueue) Peek() (index int, value interface{}, err error)
Peek peeks the priority queue, it returns the heap top value with it's index. if queue is empty, error will be returned.
func (*IndexedPriorityQueue) Pop ¶
func (i *IndexedPriorityQueue) Pop() (index int, value interface{}, err error)
Pop returns the heap top value of the queue, and remove it from the queue in the meantime, error will be returned if queue is empty.
func (*IndexedPriorityQueue) Size ¶
func (i *IndexedPriorityQueue) Size() int
Size returns the number of values stored in the queue.
type SegmentTree ¶
type SegmentTree struct {
// contains filtered or unexported fields
}
SegmentTree represents a segment tree.
func NewSegmentTree ¶
func NewSegmentTree(input []int, segmentTreeType SegmentTreeType) *SegmentTree
NewSegmentTree creates a segment tree for the input array.
func (*SegmentTree) Query ¶
func (s *SegmentTree) Query(i, j int) int
Query queries the result from range i to j. Then we can get it's real position in nodes at left and right. Where left = n + i, and right = n + j. As long as i <= j, we can exam the value at i and j first and then move up to their parents level.
func (*SegmentTree) Update ¶
func (s *SegmentTree) Update(i, update int)
Update updates the value of a given index i. The idea is that, for a given index i, the leaf node of it is at n + i We will then propogate the update from bottom to up.
type SegmentTreeType ¶
type SegmentTreeType int
SegmentTreeType defines the segment tree types.
const ( SegmentTreeMin SegmentTreeType = iota + 1 SegmentTreeMax SegmentTreeSum )
Define 3 types of segment tree. These are for 3 types of query respectively, query range minimum, range max and range sum.
type TrieNode ¶
type TrieNode struct {
// contains filtered or unexported fields
}
TrieNode defines a trie node.
type TrieTree ¶
type TrieTree struct {
// contains filtered or unexported fields
}
TrieTree defines a trie tree.
func (*TrieTree) Search ¶
Search searches a given str against a trie tree and see whether it exists.
func (*TrieTree) StartsWith ¶
StartsWith returns all strings that starts with the prefix.