Browse Source

Misc improvements.

 * "forwards/backwards" -> "forward/backward".
 * Add an example.
 * Simplify the comparison routine, because integers.
Yawning Angel 1 year ago
parent
commit
10c267d936
3 changed files with 59 additions and 15 deletions
  1. 2 2
      avl.go
  2. 3 13
      avl_test.go
  3. 54 0
      example_test.go

+ 2 - 2
avl.go

@@ -37,10 +37,10 @@ type CompareFunc func(a, b interface{}) int
 type Direction int8
 
 const (
-	// Backward is backwards in-order.
+	// Backward is backward in-order.
 	Backward Direction = -1
 
-	// Forward is forwards in-order.
+	// Forward is forward in-order.
 	Forward Direction = 1
 )
 

+ 3 - 13
avl_test.go

@@ -18,19 +18,9 @@ import (
 func TestAVLTree(t *testing.T) {
 	require := require.New(t)
 
-	cmpInt := func(a, b interface{}) int {
-		aInt, bInt := a.(int), b.(int)
-		switch {
-		case aInt < bInt:
-			return -1
-		case aInt > bInt:
-			return 1
-		default:
-			return 0
-		}
-	}
-
-	tree := New(cmpInt)
+	tree := New(func(a, b interface{}) int {
+		return a.(int) - b.(int)
+	})
 	require.Equal(0, tree.Len(), "Len(): empty")
 	require.Nil(tree.First(), "First(): empty")
 	require.Nil(tree.Last(), "Last(): empty")

+ 54 - 0
example_test.go

@@ -0,0 +1,54 @@
+// example_test.go - AVL tree example.
+//
+// To the extent possible under law, Yawning Angel has waived all copyright
+// and related or neighboring rights to avl, using the Creative
+// Commons "CC0" public domain dedication. See LICENSE or
+// <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
+
+package avl
+
+import "fmt"
+
+func CompareIntegers(a, b interface{}) int {
+	// Returns < 0, 0, > 1 if a < b, a == b, a > b respectively.
+	return a.(int) - b.(int)
+}
+
+func Example() {
+	// Create a new tree that will store integers.
+	tree := New(CompareIntegers)
+
+	// Insert a handful of integers in random order.
+	s := []int{5, 2, 6, 3, 1, 4}
+	for _, i := range s {
+		tree.Insert(i)
+	}
+
+	// Traverse the tree forward in-order.
+	forwardInOrder := make([]int, 0, len(s))
+	tree.ForEach(Forward, func(node *Node) bool {
+		forwardInOrder = append(forwardInOrder, node.Value.(int))
+		return true
+	})
+
+	fmt.Println(forwardInOrder)
+
+	// Traverse the tree backward using an interator.
+	backwardInOrder := make([]int, 0, len(s))
+	iter := tree.Iterator(Backward)
+	for node := iter.First(); node != nil; node = iter.Next() {
+		backwardInOrder = append(backwardInOrder, node.Value.(int))
+
+		// It is safe to remove the current node while iterating.
+		tree.Remove(node)
+	}
+
+	fmt.Println(backwardInOrder)
+
+	// The tree is empty after the Remove() calls.
+	fmt.Println(tree.Len())
+
+	// Output: [1 2 3 4 5 6]
+	// [6 5 4 3 2 1]
+	// 0
+}