Skip to content

Commit 639162e

Browse files
committed
added examples and document for btree
1 parent aeebb7e commit 639162e

File tree

5 files changed

+463
-8
lines changed

5 files changed

+463
-8
lines changed

README.md

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ gocontainer implements some containers which exist in Java, but are missing in g
1313
- [List](#list)
1414
- [PriorityQueue](#priorityqueue)
1515
- [LinkedMap](#linkedMap)
16+
- [BTree](#bTree)
1617
- [Others](#others)
1718
- **[Utilities](#Utilities)**
1819
- [Comparator](#Comparator)
@@ -491,7 +492,7 @@ func main() {
491492

492493
A utils.Comparator instance can be provided for a priorityQueue by method WithComparator, please get more detailed info in **[Comparator](#comparator)**.
493494
```go
494-
WithComparator(c gsort.Comparator) Interface
495+
WithComparator(c utils.Comparator) Interface
495496
```
496497

497498
A priorityQueue can be configured to use min-heap or max-heap using method WithMinHeap. If the parameter is true, then it's a min-heap, which is the default option as well; otherwise, it's a max-heap.
@@ -618,6 +619,134 @@ for hasPrev {
618619
}
619620
```
620621

622+
## **BTree**
623+
BTree is a B-Tree implementation. It was originally copied from github.com/google/btree, but it is refactored to adapt to the interface convention in this repository. Some improvements are also applied on top of the original design & implementation, so that it's more user-friendly.
624+
It implements the following interface. Click **[here](examples/btree_example.go)** to find examples on how to use a BTree.
625+
```go
626+
// Interface is a type of btree, and bTree implements this interface
627+
type Interface interface {
628+
collection.Interface
629+
630+
// WithComparator sets an utils.Comparator instance for the btree.
631+
// It's used to impose a total ordering on the elements in the btree.
632+
WithComparator(c utils.Comparator) Interface
633+
634+
// Clone clones the btree, lazily. The internal tree structure is marked read-only and
635+
// shared between the old and new btree. Writes to both the old and the new btree use copy-on-write logic.
636+
Clone() Interface
637+
// ReplaceOrInsert adds the given item to the tree. If an item in the tree
638+
// already equals the given one, it is removed from the tree and returned.
639+
// Otherwise, nil is returned.
640+
ReplaceOrInsert(item interface{}) interface{}
641+
// Delete removes an item equal to the passed in item from the tree, returning
642+
// it. If no such item exists, returns nil.
643+
Delete(item interface{}) interface{}
644+
// DeleteMin removes the smallest item in the tree and returns it.
645+
// If no such item exists, returns nil.
646+
DeleteMin() interface{}
647+
// DeleteMax removes the largest item in the tree and returns it.
648+
// If no such item exists, returns nil.
649+
DeleteMax() interface{}
650+
651+
// AscendRange calls the iterator for every value in the tree within the range
652+
// [greaterOrEqual, lessThan), until iterator returns false.
653+
AscendRange(greaterOrEqual, lessThan interface{}, iterator ItemIterator)
654+
// AscendLessThan calls the iterator for every value in the tree within the range
655+
// [first, pivot), until iterator returns false.
656+
AscendLessThan(pivot interface{}, iterator ItemIterator)
657+
// AscendGreaterOrEqual calls the iterator for every value in the tree within
658+
// the range [pivot, last], until iterator returns false.
659+
AscendGreaterOrEqual(pivot interface{}, iterator ItemIterator)
660+
// Ascend calls the iterator for every value in the tree within the range
661+
// [first, last], until iterator returns false.
662+
Ascend(iterator ItemIterator)
663+
664+
// DescendRange calls the iterator for every value in the tree within the range
665+
// [lessOrEqual, greaterThan), until iterator returns false.
666+
DescendRange(lessOrEqual, greaterThan interface{}, iterator ItemIterator)
667+
// DescendLessOrEqual calls the iterator for every value in the tree within the range
668+
// [pivot, first], until iterator returns false.
669+
DescendLessOrEqual(pivot interface{}, iterator ItemIterator)
670+
// DescendGreaterThan calls the iterator for every value in the tree within
671+
// the range [last, pivot), until iterator returns false.
672+
DescendGreaterThan(pivot interface{}, iterator ItemIterator)
673+
// Descend calls the iterator for every value in the tree within the range
674+
// [last, first], until iterator returns false.
675+
Descend(iterator ItemIterator)
676+
677+
// Get looks for the key item in the tree, returning it. It returns nil if
678+
// unable to find that item.
679+
Get(key interface{}) interface{}
680+
// Min returns the smallest item in the tree, or nil if the tree is empty.
681+
Min() interface{}
682+
// Max returns the largest item in the tree, or nil if the tree is empty.
683+
Max() interface{}
684+
// Has returns true if the given key is in the tree.
685+
Has(key interface{}) bool
686+
}
687+
```
688+
689+
Please import the following package in order to use btree,
690+
```go
691+
import (
692+
"github.com/ahrtr/gocontainer/btree"
693+
)
694+
```
695+
696+
Call btree.New() to create a BTree,
697+
```go
698+
New() Interface
699+
```
700+
701+
The following is a simple example for btree,
702+
```go
703+
package main
704+
705+
import (
706+
"fmt"
707+
708+
"github.com/ahrtr/gocontainer/btree"
709+
)
710+
711+
func main() {
712+
items := []int {5, 9, 2, 4, 11, 6}
713+
tr := btree.New(2)
714+
715+
fmt.Printf("tr.Size(): %d\n", tr.Size()) // should be 0 in the beginning
716+
717+
// Insert values
718+
fmt.Printf("Inserting %d items: %v\n", len(items), items)
719+
for _, item := range items {
720+
tr.ReplaceOrInsert(item)
721+
}
722+
723+
// Search values
724+
fmt.Printf(" tr.Size(): %d\n", tr.Size()) // should be len(items): 6 now
725+
fmt.Printf(" tr.Min(): %v\n", tr.Min()) // should be 2
726+
fmt.Printf(" tr.Max(): %v\n", tr.Max()) // should be 11
727+
fmt.Printf(" tr.Has(6): %t\n", tr.Has(6)) // true
728+
fmt.Printf(" tr.Get(6): %v\n", tr.Get(6)) // 6
729+
fmt.Printf(" tr.Has(7): %t\n", tr.Has(7)) // false
730+
fmt.Printf(" tr.Get(7): %v\n", tr.Get(7)) // nil
731+
732+
// Delete values
733+
fmt.Println("Deleting items:")
734+
fmt.Printf(" tr.DeleteMin(): %v\n", tr.DeleteMin()) // 2 is deleted and returned
735+
fmt.Printf(" tr.Min(): %v\n", tr.Min()) // should be 4 now
736+
fmt.Printf(" tr.DeleteMax(): %v\n", tr.DeleteMax()) // 11 is deleted and returned
737+
fmt.Printf(" tr.Max(): %v\n", tr.Max()) // should be 9 now
738+
fmt.Printf(" tr.Delete(6): %v\n", tr.Delete(6)) // 6 is deleted and returned
739+
fmt.Printf(" tr.Delete(7): %v\n", tr.Delete(7)) // 7 doesn't exist, so nil is returned
740+
741+
fmt.Printf("tr.Size(): %d\n", tr.Size()) // should be 3 now because 3 items have already been removed
742+
}
743+
```
744+
745+
A utils.Comparator instance can be provided for a btree by method WithComparator, please get more detailed info in **[Comparator](#comparator)**.
746+
```go
747+
WithComparator(c utils.Comparator) Interface
748+
```
749+
621750
## Others
622751
More containers will be added soon. Please also kindly let me know if you need any other kinds of containers. Feel free to raise issues.
623752

README_cn.md

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ gocontainer实现了一些Java中存在,而Golang中没有的容器。这个
1313
- [List](#list)
1414
- [PriorityQueue](#priorityqueue)
1515
- [LinkedMap](#linkedMap)
16+
- [BTree](#bTree)
1617
- [其它容器](#其它容器)
1718
- **[工具箱](#工具箱)**
1819
- [Comparator](#Comparator)
@@ -93,7 +94,7 @@ import (
9394
)
9495
```
9596

96-
调用stack.New()可以创建要一个stack
97+
调用stack.New()可以创建一个stack
9798
```go
9899
New() Interface
99100
```
@@ -492,7 +493,7 @@ func main() {
492493

493494
可以通过方法WithComparator为一个priorityQueue设置一个utils.Comparator实例,具体请参考 **[Comparator](#comparator)**.
494495
```go
495-
WithComparator(c gsort.Comparator) Interface
496+
WithComparator(c utils.Comparator) Interface
496497
```
497498

498499
可以通过方法WithMinHeap将一个priorityQueue设置成小顶堆或大顶堆。如果传给这个方法的参数为true,那就是小顶堆,这也是默认选项。如果为false,则是大顶堆。
@@ -619,6 +620,134 @@ for hasPrev {
619620
}
620621
```
621622

623+
## **BTree**
624+
BTree是一个B-Tree的实现。最初的实现是从github.com/google/btree拷贝来的,但是做了一定的重构以适应该项目一贯的接口约定。另外,也对原来的一些设计与实现做了一些改进,因此使用起来更加用户友好!
625+
具体来说,它实现了下面的接口。点击**[这里](examples/btree_example.go)**查看关于btree的示例。
626+
```go
627+
// Interface is a type of btree, and bTree implements this interface
628+
type Interface interface {
629+
collection.Interface
630+
631+
// WithComparator sets an utils.Comparator instance for the btree.
632+
// It's used to impose a total ordering on the elements in the btree.
633+
WithComparator(c utils.Comparator) Interface
634+
635+
// Clone clones the btree, lazily. The internal tree structure is marked read-only and
636+
// shared between the old and new btree. Writes to both the old and the new btree use copy-on-write logic.
637+
Clone() Interface
638+
// ReplaceOrInsert adds the given item to the tree. If an item in the tree
639+
// already equals the given one, it is removed from the tree and returned.
640+
// Otherwise, nil is returned.
641+
ReplaceOrInsert(item interface{}) interface{}
642+
// Delete removes an item equal to the passed in item from the tree, returning
643+
// it. If no such item exists, returns nil.
644+
Delete(item interface{}) interface{}
645+
// DeleteMin removes the smallest item in the tree and returns it.
646+
// If no such item exists, returns nil.
647+
DeleteMin() interface{}
648+
// DeleteMax removes the largest item in the tree and returns it.
649+
// If no such item exists, returns nil.
650+
DeleteMax() interface{}
651+
652+
// AscendRange calls the iterator for every value in the tree within the range
653+
// [greaterOrEqual, lessThan), until iterator returns false.
654+
AscendRange(greaterOrEqual, lessThan interface{}, iterator ItemIterator)
655+
// AscendLessThan calls the iterator for every value in the tree within the range
656+
// [first, pivot), until iterator returns false.
657+
AscendLessThan(pivot interface{}, iterator ItemIterator)
658+
// AscendGreaterOrEqual calls the iterator for every value in the tree within
659+
// the range [pivot, last], until iterator returns false.
660+
AscendGreaterOrEqual(pivot interface{}, iterator ItemIterator)
661+
// Ascend calls the iterator for every value in the tree within the range
662+
// [first, last], until iterator returns false.
663+
Ascend(iterator ItemIterator)
664+
665+
// DescendRange calls the iterator for every value in the tree within the range
666+
// [lessOrEqual, greaterThan), until iterator returns false.
667+
DescendRange(lessOrEqual, greaterThan interface{}, iterator ItemIterator)
668+
// DescendLessOrEqual calls the iterator for every value in the tree within the range
669+
// [pivot, first], until iterator returns false.
670+
DescendLessOrEqual(pivot interface{}, iterator ItemIterator)
671+
// DescendGreaterThan calls the iterator for every value in the tree within
672+
// the range [last, pivot), until iterator returns false.
673+
DescendGreaterThan(pivot interface{}, iterator ItemIterator)
674+
// Descend calls the iterator for every value in the tree within the range
675+
// [last, first], until iterator returns false.
676+
Descend(iterator ItemIterator)
677+
678+
// Get looks for the key item in the tree, returning it. It returns nil if
679+
// unable to find that item.
680+
Get(key interface{}) interface{}
681+
// Min returns the smallest item in the tree, or nil if the tree is empty.
682+
Min() interface{}
683+
// Max returns the largest item in the tree, or nil if the tree is empty.
684+
Max() interface{}
685+
// Has returns true if the given key is in the tree.
686+
Has(key interface{}) bool
687+
}
688+
```
689+
690+
为了使用btree,必须import下面这个package,
691+
```go
692+
import (
693+
"github.com/ahrtr/gocontainer/btree"
694+
)
695+
```
696+
697+
调用btree.New()可以创建一个btree,
698+
```go
699+
New() Interface
700+
```
701+
702+
下面是一个简单的使用btree的例子,
703+
```go
704+
package main
705+
706+
import (
707+
"fmt"
708+
709+
"github.com/ahrtr/gocontainer/btree"
710+
)
711+
712+
func main() {
713+
items := []int {5, 9, 2, 4, 11, 6}
714+
tr := btree.New(2)
715+
716+
fmt.Printf("tr.Size(): %d\n", tr.Size()) // should be 0 in the beginning
717+
718+
// Insert values
719+
fmt.Printf("Inserting %d items: %v\n", len(items), items)
720+
for _, item := range items {
721+
tr.ReplaceOrInsert(item)
722+
}
723+
724+
// Search values
725+
fmt.Printf(" tr.Size(): %d\n", tr.Size()) // should be len(items): 6 now
726+
fmt.Printf(" tr.Min(): %v\n", tr.Min()) // should be 2
727+
fmt.Printf(" tr.Max(): %v\n", tr.Max()) // should be 11
728+
fmt.Printf(" tr.Has(6): %t\n", tr.Has(6)) // true
729+
fmt.Printf(" tr.Get(6): %v\n", tr.Get(6)) // 6
730+
fmt.Printf(" tr.Has(7): %t\n", tr.Has(7)) // false
731+
fmt.Printf(" tr.Get(7): %v\n", tr.Get(7)) // nil
732+
733+
// Delete values
734+
fmt.Println("Deleting items:")
735+
fmt.Printf(" tr.DeleteMin(): %v\n", tr.DeleteMin()) // 2 is deleted and returned
736+
fmt.Printf(" tr.Min(): %v\n", tr.Min()) // should be 4 now
737+
fmt.Printf(" tr.DeleteMax(): %v\n", tr.DeleteMax()) // 11 is deleted and returned
738+
fmt.Printf(" tr.Max(): %v\n", tr.Max()) // should be 9 now
739+
fmt.Printf(" tr.Delete(6): %v\n", tr.Delete(6)) // 6 is deleted and returned
740+
fmt.Printf(" tr.Delete(7): %v\n", tr.Delete(7)) // 7 doesn't exist, so nil is returned
741+
742+
fmt.Printf("tr.Size(): %d\n", tr.Size()) // should be 3 now because 3 items have already been removed
743+
}
744+
```
745+
746+
可以通过方法WithComparator为一个btree设置一个utils.Comparator实例,具体请参考**[Comparator](#comparator)**.
747+
```go
748+
WithComparator(c utils.Comparator) Interface
749+
```
750+
622751
## 其它容器
623752
更多的容器将来可能会加入进来。如果您需要任何其它类型的容器,或者有任何建议,欢迎通过issues反馈给我。
624753

btree/btree.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ type Interface interface {
7070
// It's used to impose a total ordering on the elements in the btree.
7171
WithComparator(c utils.Comparator) Interface
7272

73-
// Clone clones the btree, lazily.
73+
// Clone clones the btree, lazily. The internal tree structure is marked read-only and
74+
// shared between the old and new btree. Writes to both the old and the new btree use copy-on-write logic.
7475
Clone() Interface
7576
// ReplaceOrInsert adds the given item to the tree. If an item in the tree
7677
// already equals the given one, it is removed from the tree and returned.
@@ -132,7 +133,7 @@ var (
132133
nilChildren = make(children, 16)
133134
)
134135

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

0 commit comments

Comments
 (0)