124 lines
2.3 KiB
Go
124 lines
2.3 KiB
Go
package btree
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestTree_Search(t *testing.T) {
|
|
tree := NewBtree(3)
|
|
tree.Insert(6)
|
|
|
|
k := 6
|
|
if tree.Search(k) == nil {
|
|
t.Errorf("Not present %d", k)
|
|
}
|
|
|
|
k = 15
|
|
if tree.Search(k) != nil {
|
|
t.Errorf("Present %d", k)
|
|
}
|
|
}
|
|
|
|
func TestTree_Remove(t *testing.T) {
|
|
tree := NewBtree(3)
|
|
toInsert := []int{10, 20, 5, 6, 12, 30, 7, 17}
|
|
|
|
for _, k := range toInsert {
|
|
tree.Insert(k)
|
|
}
|
|
|
|
tree.Remove(toInsert[0])
|
|
for _, k := range toInsert[1:] {
|
|
if tree.Search(k) == nil {
|
|
t.Errorf("Not present %d", k)
|
|
}
|
|
}
|
|
|
|
if tree.Search(toInsert[0]) != nil {
|
|
t.Errorf("Present %d", toInsert[0])
|
|
}
|
|
|
|
}
|
|
|
|
func TestTree_Insert(t *testing.T) {
|
|
tree := NewBtree(3)
|
|
toInsert := []int{10, 20, 5, 6, 12, 30, 7, 17}
|
|
|
|
// Test before insertion
|
|
for _, k := range toInsert {
|
|
if tree.Search(k) != nil {
|
|
t.Errorf("Present %d", k)
|
|
}
|
|
}
|
|
|
|
for _, k := range toInsert {
|
|
tree.Insert(k)
|
|
}
|
|
|
|
// Test after insertion
|
|
for _, k := range toInsert {
|
|
if tree.Search(k) == nil {
|
|
t.Errorf("Not present %d", k)
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
func TestTree_Traverse(t *testing.T) {
|
|
tree := NewBtree(3)
|
|
|
|
toInsert := []int{1, 3, 7, 10, 11, 13, 14, 15, 18, 16, 19, 24, 25, 26, 21, 4, 5, 20, 22, 2, 17, 12, 6}
|
|
for _, k := range toInsert {
|
|
tree.Insert(k)
|
|
}
|
|
|
|
tree.Traverse()
|
|
fmt.Println("\n 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26")
|
|
}
|
|
*/
|
|
|
|
func ExampleTree_Traverse() {
|
|
tree := NewBtree(3)
|
|
|
|
toInsert := []int{1, 3, 7, 10, 11, 13, 14, 15, 18, 16, 19, 24, 25, 26, 21, 4, 5, 20, 22, 2, 17, 12, 6}
|
|
for _, k := range toInsert {
|
|
tree.Insert(k)
|
|
}
|
|
|
|
tree.Traverse()
|
|
fmt.Println("")
|
|
|
|
tree.Remove(6)
|
|
tree.Traverse()
|
|
fmt.Println("")
|
|
|
|
tree.Remove(13)
|
|
tree.Traverse()
|
|
fmt.Println("")
|
|
|
|
tree.Remove(7)
|
|
tree.Traverse()
|
|
fmt.Println("")
|
|
|
|
tree.Remove(4)
|
|
tree.Traverse()
|
|
fmt.Println("")
|
|
|
|
tree.Remove(2)
|
|
tree.Traverse()
|
|
fmt.Println("")
|
|
|
|
tree.Remove(16)
|
|
tree.Traverse()
|
|
|
|
// Output:
|
|
// 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26
|
|
// 1 2 3 4 5 7 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26
|
|
// 1 2 3 4 5 7 10 11 12 14 15 16 17 18 19 20 21 22 24 25 26
|
|
// 1 2 3 4 5 10 11 12 14 15 16 17 18 19 20 21 22 24 25 26
|
|
// 1 2 3 5 10 11 12 14 15 16 17 18 19 20 21 22 24 25 26
|
|
// 1 3 5 10 11 12 14 15 16 17 18 19 20 21 22 24 25 26
|
|
// 1 3 5 10 11 12 14 15 17 18 19 20 21 22 24 25 26
|
|
}
|