Beispiel #1
0
func TestNewBtreeWithPreInOrder(t *testing.T) {
	preOrder := base.NewIntComparableSlice([]int{7, 10, 4, 3, 1, 2, 8, 11})
	inOrder := base.NewIntComparableSlice([]int{4, 10, 3, 1, 7, 11, 8, 2})

	btree, err := NewBtreeWithInPreOrder(inOrder, preOrder)

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

	if len(result) == 0 {
		t.Error("build PRE-Order failed")
	}
	for i, v := range result {
		if preOrder[i] != v.Element {
			t.Error("build PRE-Order failed")
		}
	}

	result = btree.InOrder()
	if len(result) == 0 {
		t.Error("build IN-Order failed")
	}
	for i, v := range result {
		if inOrder[i] != v.Element {
			t.Error("build IN-Order failed")
		}
	}

	preOrder1 := base.NewRuneComparableSlice([]rune{'a', 'b', 'd', 'e', 'f', 'g', 'c'})
	inOrder1 := base.NewRuneComparableSlice([]rune{'d', 'e', 'b', 'g', 'f', 'a', 'c'})

	btree, err = NewBtreeWithInPreOrder(inOrder1, preOrder1)

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

	for i, v := range result {
		if preOrder1[i] != v.Element {
			t.Error("build PRE-Order failed")
		}
	}
}
Beispiel #2
0
func TestNewBtreeWithPostInOrder(t *testing.T) {
	inOrder := base.NewIntComparableSlice([]int{4, 10, 3, 1, 7, 11, 8, 2})
	postOrder := base.NewIntComparableSlice([]int{4, 1, 3, 10, 11, 8, 2, 7})

	btree, err := NewBtreeWithInPostOrder(inOrder, postOrder)

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

	for i, v := range result {
		if postOrder[i] != v.Element {
			t.Error("build POST-Order failed")
		}
	}

	result = btree.InOrder()
	for i, v := range result {
		if inOrder[i] != v.Element {
			t.Error("build IN-Order failed")
		}
	}

	postOrder1 := base.NewRuneComparableSlice([]rune{'e', 'd', 'g', 'f', 'b', 'c', 'a'})
	inOrder1 := base.NewRuneComparableSlice([]rune{'d', 'e', 'b', 'g', 'f', 'a', 'c'})

	btree, err = NewBtreeWithInPostOrder(inOrder1, postOrder1)

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

	for i, v := range result {
		if postOrder1[i] != v.Element {
			t.Error("build POST-Order failed")
		}
	}
}
Beispiel #3
0
func TestBtreePostOrderNonRecursiveV2(t *testing.T) {
	postOrder := base.NewRuneComparableSlice([]rune{'e', 'd', 'g', 'f', 'b', 'c', 'a'})
	inOrder := base.NewRuneComparableSlice([]rune{'d', 'e', 'b', 'g', 'f', 'a', 'c'})

	btree, err := NewBtreeWithInPostOrder(inOrder, postOrder)

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

	result := btree.PostOrderNonRecursiveV2()

	if len(result) == 0 {
		t.Error("build POST-Order failed")
	}

	for i, v := range result {
		if postOrder[i] != v.Element {
			t.Error("build POST-Order failed")
		}
	}
}