2017-02-25 20:29:26 +00:00
|
|
|
// Copyright (c) 2017, Benjamin Scher Purcell. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-02-26 19:12:51 +00:00
|
|
|
// Package avltree implements an AVL balanced binary tree.
|
2017-02-25 20:29:26 +00:00
|
|
|
//
|
|
|
|
// Structure is not thread safe.
|
2017-03-05 20:26:42 +00:00
|
|
|
//
|
|
|
|
// References: https://en.wikipedia.org/wiki/AVL_tree
|
2017-02-26 19:12:51 +00:00
|
|
|
package avltree
|
2017-02-25 20:29:26 +00:00
|
|
|
|
|
|
|
import (
|
2017-02-26 19:12:51 +00:00
|
|
|
"fmt"
|
2017-03-05 19:48:04 +00:00
|
|
|
"github.com/emirpasic/gods/trees"
|
2017-03-05 19:50:19 +00:00
|
|
|
"github.com/emirpasic/gods/utils"
|
2017-02-25 20:29:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func assertTreeImplementation() {
|
|
|
|
var _ trees.Tree = new(Tree)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tree holds elements of the AVL tree.
|
|
|
|
type Tree struct {
|
2017-03-05 20:03:01 +00:00
|
|
|
Root *Node // Root node
|
|
|
|
Comparator utils.Comparator // Key comparator
|
|
|
|
size int // Total number of keys in the tree
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 19:12:51 +00:00
|
|
|
// Node is a single element within the tree
|
2017-02-25 20:29:26 +00:00
|
|
|
type Node struct {
|
2017-03-05 20:03:01 +00:00
|
|
|
Key interface{}
|
|
|
|
Value interface{}
|
|
|
|
Parent *Node // Parent node
|
|
|
|
Children [2]*Node // Children nodes
|
|
|
|
b int8
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 00:52:00 +00:00
|
|
|
// NewWith instantiates an AVL tree with the custom comparator.
|
2017-02-25 20:29:26 +00:00
|
|
|
func NewWith(comparator utils.Comparator) *Tree {
|
2017-03-05 19:57:53 +00:00
|
|
|
return &Tree{Comparator: comparator}
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 00:52:00 +00:00
|
|
|
// NewWithIntComparator instantiates an AVL tree with the IntComparator, i.e. keys are of type int.
|
2017-02-25 20:29:26 +00:00
|
|
|
func NewWithIntComparator() *Tree {
|
2017-03-05 19:57:53 +00:00
|
|
|
return &Tree{Comparator: utils.IntComparator}
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 00:52:00 +00:00
|
|
|
// NewWithStringComparator instantiates an AVL tree with the StringComparator, i.e. keys are of type string.
|
2017-02-25 20:29:26 +00:00
|
|
|
func NewWithStringComparator() *Tree {
|
2017-03-05 19:57:53 +00:00
|
|
|
return &Tree{Comparator: utils.StringComparator}
|
2017-02-27 01:46:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 20:33:07 +00:00
|
|
|
// Put inserts node into the tree.
|
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
|
|
|
func (t *Tree) Put(key interface{}, value interface{}) {
|
|
|
|
t.put(key, value, nil, &t.Root)
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 19:12:51 +00:00
|
|
|
// Get searches the node in the tree by key and returns its value or nil if key is not found in tree.
|
|
|
|
// Second return parameter is true if key was found, otherwise false.
|
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
2017-02-25 20:29:26 +00:00
|
|
|
func (t *Tree) Get(key interface{}) (value interface{}, found bool) {
|
2022-04-12 01:33:59 +00:00
|
|
|
n := t.GetNode(key)
|
|
|
|
if n != nil {
|
|
|
|
return n.Value, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNode searches the node in the tree by key and returns its node or nil if key is not found in tree.
|
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
|
|
|
func (t *Tree) GetNode(key interface{}) *Node {
|
2017-02-25 20:29:26 +00:00
|
|
|
n := t.Root
|
|
|
|
for n != nil {
|
2017-03-05 19:57:53 +00:00
|
|
|
cmp := t.Comparator(key, n.Key)
|
2017-02-25 20:29:26 +00:00
|
|
|
switch {
|
|
|
|
case cmp == 0:
|
2022-04-12 01:33:59 +00:00
|
|
|
return n
|
2017-02-26 19:12:51 +00:00
|
|
|
case cmp < 0:
|
2017-03-05 20:03:01 +00:00
|
|
|
n = n.Children[0]
|
2017-02-25 20:29:26 +00:00
|
|
|
case cmp > 0:
|
2017-03-05 20:03:01 +00:00
|
|
|
n = n.Children[1]
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-12 01:33:59 +00:00
|
|
|
return n
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 20:33:07 +00:00
|
|
|
// Remove remove the node from the tree by key.
|
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
|
|
|
func (t *Tree) Remove(key interface{}) {
|
|
|
|
t.remove(key, &t.Root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty returns true if tree does not contain any nodes.
|
|
|
|
func (t *Tree) Empty() bool {
|
|
|
|
return t.size == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the number of elements stored in the tree.
|
|
|
|
func (t *Tree) Size() int {
|
|
|
|
return t.size
|
|
|
|
}
|
|
|
|
|
2022-04-12 01:33:59 +00:00
|
|
|
// Size returns the number of elements stored in the subtree.
|
|
|
|
// Computed dynamically on each call, i.e. the subtree is traversed to count the number of the nodes.
|
|
|
|
func (n *Node) Size() int {
|
|
|
|
if n == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
size := 1
|
|
|
|
if n.Children[0] != nil {
|
|
|
|
size += n.Children[0].Size()
|
|
|
|
}
|
|
|
|
if n.Children[1] != nil {
|
|
|
|
size += n.Children[1].Size()
|
|
|
|
}
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:33:07 +00:00
|
|
|
// Keys returns all keys in-order
|
|
|
|
func (t *Tree) Keys() []interface{} {
|
|
|
|
keys := make([]interface{}, t.size)
|
|
|
|
it := t.Iterator()
|
|
|
|
for i := 0; it.Next(); i++ {
|
|
|
|
keys[i] = it.Key()
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Values returns all values in-order based on the key.
|
|
|
|
func (t *Tree) Values() []interface{} {
|
|
|
|
values := make([]interface{}, t.size)
|
|
|
|
it := t.Iterator()
|
|
|
|
for i := 0; it.Next(); i++ {
|
|
|
|
values[i] = it.Value()
|
|
|
|
}
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
|
|
|
// Left returns the minimum element of the AVL tree
|
|
|
|
// or nil if the tree is empty.
|
|
|
|
func (t *Tree) Left() *Node {
|
|
|
|
return t.bottom(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Right returns the maximum element of the AVL tree
|
|
|
|
// or nil if the tree is empty.
|
|
|
|
func (t *Tree) Right() *Node {
|
|
|
|
return t.bottom(1)
|
|
|
|
}
|
|
|
|
|
2017-02-25 20:29:26 +00:00
|
|
|
// Floor Finds floor node of the input key, return the floor node or nil if no ceiling is found.
|
|
|
|
// Second return parameter is true if floor was found, otherwise false.
|
|
|
|
//
|
|
|
|
// Floor node is defined as the largest node that is smaller than or equal to the given node.
|
|
|
|
// A floor node may not be found, either because the tree is empty, or because
|
|
|
|
// all nodes in the tree is larger than the given node.
|
|
|
|
//
|
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
|
|
|
func (t *Tree) Floor(key interface{}) (floor *Node, found bool) {
|
|
|
|
found = false
|
|
|
|
n := t.Root
|
|
|
|
for n != nil {
|
2017-03-05 19:57:53 +00:00
|
|
|
c := t.Comparator(key, n.Key)
|
2017-02-25 20:29:26 +00:00
|
|
|
switch {
|
|
|
|
case c == 0:
|
|
|
|
return n, true
|
|
|
|
case c < 0:
|
2017-03-05 20:03:01 +00:00
|
|
|
n = n.Children[0]
|
2017-02-25 20:29:26 +00:00
|
|
|
case c > 0:
|
|
|
|
floor, found = n, true
|
2017-03-05 20:03:01 +00:00
|
|
|
n = n.Children[1]
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ceiling finds ceiling node of the input key, return the ceiling node or nil if no ceiling is found.
|
|
|
|
// Second return parameter is true if ceiling was found, otherwise false.
|
|
|
|
//
|
|
|
|
// Ceiling node is defined as the smallest node that is larger than or equal to the given node.
|
|
|
|
// A ceiling node may not be found, either because the tree is empty, or because
|
|
|
|
// all nodes in the tree is smaller than the given node.
|
|
|
|
//
|
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
|
|
|
func (t *Tree) Ceiling(key interface{}) (floor *Node, found bool) {
|
|
|
|
found = false
|
|
|
|
n := t.Root
|
|
|
|
for n != nil {
|
2017-03-05 19:57:53 +00:00
|
|
|
c := t.Comparator(key, n.Key)
|
2017-02-25 20:29:26 +00:00
|
|
|
switch {
|
|
|
|
case c == 0:
|
|
|
|
return n, true
|
|
|
|
case c < 0:
|
|
|
|
floor, found = n, true
|
2017-03-05 20:03:01 +00:00
|
|
|
n = n.Children[0]
|
2017-02-25 20:29:26 +00:00
|
|
|
case c > 0:
|
2017-03-05 20:03:01 +00:00
|
|
|
n = n.Children[1]
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:33:07 +00:00
|
|
|
// Clear removes all nodes from the tree.
|
|
|
|
func (t *Tree) Clear() {
|
|
|
|
t.Root = nil
|
|
|
|
t.size = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string representation of container
|
|
|
|
func (t *Tree) String() string {
|
|
|
|
str := "AVLTree\n"
|
|
|
|
if !t.Empty() {
|
|
|
|
output(t.Root, "", true, &str)
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) String() string {
|
|
|
|
return fmt.Sprintf("%v", n.Key)
|
2017-03-05 20:26:42 +00:00
|
|
|
}
|
2017-02-25 20:29:26 +00:00
|
|
|
|
2017-03-05 20:26:42 +00:00
|
|
|
func (t *Tree) put(key interface{}, value interface{}, p *Node, qp **Node) bool {
|
|
|
|
q := *qp
|
|
|
|
if q == nil {
|
|
|
|
t.size++
|
|
|
|
*qp = &Node{Key: key, Value: value, Parent: p}
|
|
|
|
return true
|
|
|
|
}
|
2017-02-25 20:29:26 +00:00
|
|
|
|
2017-03-05 20:26:42 +00:00
|
|
|
c := t.Comparator(key, q.Key)
|
|
|
|
if c == 0 {
|
|
|
|
q.Key = key
|
|
|
|
q.Value = value
|
2017-02-25 20:29:26 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:26:42 +00:00
|
|
|
if c < 0 {
|
|
|
|
c = -1
|
|
|
|
} else {
|
|
|
|
c = 1
|
|
|
|
}
|
|
|
|
a := (c + 1) / 2
|
|
|
|
var fix bool
|
|
|
|
fix = t.put(key, value, q, &q.Children[a])
|
|
|
|
if fix {
|
|
|
|
return putFix(int8(c), qp)
|
|
|
|
}
|
|
|
|
return false
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 20:26:42 +00:00
|
|
|
func (t *Tree) remove(key interface{}, qp **Node) bool {
|
|
|
|
q := *qp
|
|
|
|
if q == nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-02-25 20:29:26 +00:00
|
|
|
|
2017-03-05 20:26:42 +00:00
|
|
|
c := t.Comparator(key, q.Key)
|
|
|
|
if c == 0 {
|
|
|
|
t.size--
|
|
|
|
if q.Children[1] == nil {
|
|
|
|
if q.Children[0] != nil {
|
|
|
|
q.Children[0].Parent = q.Parent
|
|
|
|
}
|
|
|
|
*qp = q.Children[0]
|
|
|
|
return true
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
2017-03-05 20:26:42 +00:00
|
|
|
fix := removeMin(&q.Children[1], &q.Key, &q.Value)
|
2017-02-25 20:29:26 +00:00
|
|
|
if fix {
|
2017-03-05 20:26:42 +00:00
|
|
|
return removeFix(-1, qp)
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:26:42 +00:00
|
|
|
if c < 0 {
|
|
|
|
c = -1
|
|
|
|
} else {
|
|
|
|
c = 1
|
|
|
|
}
|
|
|
|
a := (c + 1) / 2
|
|
|
|
fix := t.remove(key, &q.Children[a])
|
|
|
|
if fix {
|
|
|
|
return removeFix(int8(-c), qp)
|
|
|
|
}
|
|
|
|
return false
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 19:12:51 +00:00
|
|
|
func removeMin(qp **Node, minKey *interface{}, minVal *interface{}) bool {
|
2017-02-25 20:29:26 +00:00
|
|
|
q := *qp
|
2017-03-05 20:03:01 +00:00
|
|
|
if q.Children[0] == nil {
|
2017-02-25 20:29:26 +00:00
|
|
|
*minKey = q.Key
|
|
|
|
*minVal = q.Value
|
2017-03-05 20:03:01 +00:00
|
|
|
if q.Children[1] != nil {
|
|
|
|
q.Children[1].Parent = q.Parent
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
2017-03-05 20:03:01 +00:00
|
|
|
*qp = q.Children[1]
|
2017-02-25 20:29:26 +00:00
|
|
|
return true
|
|
|
|
}
|
2017-03-05 20:03:01 +00:00
|
|
|
fix := removeMin(&q.Children[0], minKey, minVal)
|
2017-02-25 20:29:26 +00:00
|
|
|
if fix {
|
|
|
|
return removeFix(1, qp)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func putFix(c int8, t **Node) bool {
|
|
|
|
s := *t
|
|
|
|
if s.b == 0 {
|
|
|
|
s.b = c
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.b == -c {
|
|
|
|
s.b = 0
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:03:01 +00:00
|
|
|
if s.Children[(c+1)/2].b == c {
|
2017-02-25 20:29:26 +00:00
|
|
|
s = singlerot(c, s)
|
|
|
|
} else {
|
|
|
|
s = doublerot(c, s)
|
|
|
|
}
|
|
|
|
*t = s
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFix(c int8, t **Node) bool {
|
|
|
|
s := *t
|
|
|
|
if s.b == 0 {
|
|
|
|
s.b = c
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.b == -c {
|
|
|
|
s.b = 0
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
a := (c + 1) / 2
|
2017-03-05 20:03:01 +00:00
|
|
|
if s.Children[a].b == 0 {
|
2017-02-25 20:29:26 +00:00
|
|
|
s = rotate(c, s)
|
|
|
|
s.b = -c
|
|
|
|
*t = s
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:03:01 +00:00
|
|
|
if s.Children[a].b == c {
|
2017-02-25 20:29:26 +00:00
|
|
|
s = singlerot(c, s)
|
|
|
|
} else {
|
|
|
|
s = doublerot(c, s)
|
|
|
|
}
|
|
|
|
*t = s
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func singlerot(c int8, s *Node) *Node {
|
|
|
|
s.b = 0
|
|
|
|
s = rotate(c, s)
|
|
|
|
s.b = 0
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func doublerot(c int8, s *Node) *Node {
|
|
|
|
a := (c + 1) / 2
|
2017-03-05 20:03:01 +00:00
|
|
|
r := s.Children[a]
|
|
|
|
s.Children[a] = rotate(-c, s.Children[a])
|
2017-02-25 20:29:26 +00:00
|
|
|
p := rotate(c, s)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
s.b = 0
|
|
|
|
r.b = 0
|
|
|
|
case p.b == c:
|
|
|
|
s.b = -c
|
|
|
|
r.b = 0
|
|
|
|
case p.b == -c:
|
|
|
|
s.b = 0
|
|
|
|
r.b = c
|
|
|
|
}
|
|
|
|
|
|
|
|
p.b = 0
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func rotate(c int8, s *Node) *Node {
|
|
|
|
a := (c + 1) / 2
|
2017-03-05 20:03:01 +00:00
|
|
|
r := s.Children[a]
|
|
|
|
s.Children[a] = r.Children[a^1]
|
|
|
|
if s.Children[a] != nil {
|
|
|
|
s.Children[a].Parent = s
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
2017-03-05 20:03:01 +00:00
|
|
|
r.Children[a^1] = s
|
|
|
|
r.Parent = s.Parent
|
|
|
|
s.Parent = r
|
2017-02-25 20:29:26 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tree) bottom(d int) *Node {
|
|
|
|
n := t.Root
|
|
|
|
if n == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:03:01 +00:00
|
|
|
for c := n.Children[d]; c != nil; c = n.Children[d] {
|
2017-02-25 20:29:26 +00:00
|
|
|
n = c
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prev returns the previous element in an inorder
|
|
|
|
// walk of the AVL tree.
|
|
|
|
func (n *Node) Prev() *Node {
|
|
|
|
return n.walk1(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next element in an inorder
|
|
|
|
// walk of the AVL tree.
|
|
|
|
func (n *Node) Next() *Node {
|
|
|
|
return n.walk1(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) walk1(a int) *Node {
|
|
|
|
if n == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:03:01 +00:00
|
|
|
if n.Children[a] != nil {
|
|
|
|
n = n.Children[a]
|
|
|
|
for n.Children[a^1] != nil {
|
|
|
|
n = n.Children[a^1]
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:03:01 +00:00
|
|
|
p := n.Parent
|
|
|
|
for p != nil && p.Children[a] == n {
|
2017-02-25 20:29:26 +00:00
|
|
|
n = p
|
2017-03-05 20:03:01 +00:00
|
|
|
p = p.Parent
|
2017-02-25 20:29:26 +00:00
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
2017-02-26 19:12:51 +00:00
|
|
|
|
|
|
|
func output(node *Node, prefix string, isTail bool, str *string) {
|
2017-03-05 21:25:29 +00:00
|
|
|
if node.Children[1] != nil {
|
2017-02-26 19:12:51 +00:00
|
|
|
newPrefix := prefix
|
|
|
|
if isTail {
|
|
|
|
newPrefix += "│ "
|
|
|
|
} else {
|
|
|
|
newPrefix += " "
|
|
|
|
}
|
2017-03-05 21:25:29 +00:00
|
|
|
output(node.Children[1], newPrefix, false, str)
|
2017-02-26 19:12:51 +00:00
|
|
|
}
|
|
|
|
*str += prefix
|
|
|
|
if isTail {
|
|
|
|
*str += "└── "
|
|
|
|
} else {
|
|
|
|
*str += "┌── "
|
|
|
|
}
|
|
|
|
*str += node.String() + "\n"
|
2017-03-05 21:25:29 +00:00
|
|
|
if node.Children[0] != nil {
|
2017-02-26 19:12:51 +00:00
|
|
|
newPrefix := prefix
|
|
|
|
if isTail {
|
|
|
|
newPrefix += " "
|
|
|
|
} else {
|
|
|
|
newPrefix += "│ "
|
|
|
|
}
|
2017-03-05 21:25:29 +00:00
|
|
|
output(node.Children[0], newPrefix, true, str)
|
2017-02-26 19:12:51 +00:00
|
|
|
}
|
|
|
|
}
|