示例#1
0
文件: btree_test.go 项目: aiden0z/kit
func TestBtree_FindNonRecursive(t *testing.T) {
	inOrder := base.NewIntComparableSlice([]int{1, 2, 3, 5, 6, 8, 9, 10, 11})
	postOrder := base.NewIntComparableSlice([]int{1, 2, 5, 3, 8, 11, 10, 9, 6})

	btree, err := NewBtreeWithInPostOrder(inOrder, postOrder)

	if err != nil {
		t.Errorf("build btree failed %s", err)
	}
	key := base.Int(5)

	node := btree.FindNonRecursive(key)

	if node == nil {
		t.Errorf("Btree FindNonRecursive work error, can not found equivalent node, %s", key)
	}

	if key.CompareTo(node.Element) != 0 {

		t.Errorf("Btree FindNonRecursive  work error, not find the correct equivalent node")
	}

	// find no exist node
	key = base.Int(100)
	node = btree.FindNonRecursive(key)
	if node != nil {
		t.Errorf("Btree FindNonRecursive work error, find a non exist targe")
	}
}
示例#2
0
文件: btree_test.go 项目: aiden0z/kit
func TestBtree_InsertNonRecursive(t *testing.T) {
	inOrder := base.NewIntComparableSlice([]int{1, 2, 3, 5, 6, 8, 9, 10, 11})
	preOrder := base.NewIntComparableSlice([]int{6, 3, 2, 1, 5, 9, 8, 10, 11})

	btree, err := NewBtreeWithInPreOrder(inOrder, preOrder)

	if err != nil {
		t.Errorf("build btree failed %s", err)
	}

	key := base.Int(7)

	aBtree := btree.InsertNonRecursive(key)

	node := aBtree.Find(key)

	if key.CompareTo(node.Element) != 0 {
		t.Error("Btree InsertNonRecursive wrok error")
	}
}
示例#3
0
文件: btree_test.go 项目: aiden0z/kit
func TestBtree_FindMinNonRecursive(t *testing.T) {
	inOrder := base.NewIntComparableSlice([]int{1, 2, 3, 5, 6, 8, 9, 10, 11})
	preOrder := base.NewIntComparableSlice([]int{6, 3, 2, 1, 5, 9, 8, 10, 11})

	btree, err := NewBtreeWithInPreOrder(inOrder, preOrder)

	if err != nil {
		t.Errorf("build btree failed %s", err)
	}

	min := base.Int(1)

	minNode := btree.FindMinNonRecursive()

	if minNode == nil {
		t.Error("Btree FindMinNonRecursive work error, can not find the min node.")
	}

	if min.CompareTo(minNode.Element) != 0 {
		t.Error("Btree FindMinNonRecursive work error, not find the correct min node.")
	}

}