forked from UNN/2026-rff_mp
Исправленны ошибки и написаны мини тесты для всех структур
This commit is contained in:
parent
b3688d3ed9
commit
6e259a4770
|
|
@ -16,7 +16,11 @@ type BSTree struct {
|
|||
right *BSTree
|
||||
}
|
||||
|
||||
func NewBinSearchTree(data ds.MyData) *BSTree {
|
||||
func NewBinSearchTree() *BinSearchTree {
|
||||
return &BinSearchTree{}
|
||||
}
|
||||
|
||||
func newBinSearchTree(data ds.MyData) *BSTree {
|
||||
return &BSTree{
|
||||
data: data,
|
||||
left: nil,
|
||||
|
|
|
|||
|
|
@ -178,7 +178,7 @@ func (elem *elementHT) ToString() string {
|
|||
return "nil"
|
||||
}
|
||||
|
||||
return elem.ToString()
|
||||
return elem.data.ToString()
|
||||
}
|
||||
|
||||
func (ht *HashTable) Print() {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,11 @@ type LList struct {
|
|||
next *LList
|
||||
}
|
||||
|
||||
func NewLinkedList(data ds.MyData) *LList {
|
||||
func NewLinkedList() *LinkedList {
|
||||
return &LinkedList{}
|
||||
}
|
||||
|
||||
func newLinkedList(data ds.MyData) *LList {
|
||||
return &LList{
|
||||
data: data,
|
||||
next: nil,
|
||||
|
|
@ -62,7 +66,7 @@ func (ll *LinkedList) Len() int {
|
|||
}
|
||||
|
||||
func (ll *LinkedList) Insert(data ds.MyData) {
|
||||
newNode := NewLinkedList(data)
|
||||
newNode := newLinkedList(data)
|
||||
|
||||
if ll.head == nil {
|
||||
ll.head = newNode
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package benchmark
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
@ -67,14 +67,14 @@ func GenerateTestData() TestData {
|
|||
toDelete := make([]ds.MyData, countDeletes)
|
||||
|
||||
countUniq := len(uniqueItems)
|
||||
for i := 0; i < countUniq; i++ {
|
||||
for i := 0; i < countRandomSearch; i++ {
|
||||
// randInd := rand.Intn(countUsers)
|
||||
randInd := rand.Intn(countUniq)
|
||||
existing[i] = uniqueItems[randInd]
|
||||
// fmt.Println(randInd)
|
||||
}
|
||||
|
||||
for i := 0; i < countUniq; i++ {
|
||||
for i := 0; i < countNotExitstSearch; i++ {
|
||||
// randInd := rand.Intn(countUsers)
|
||||
randInd := rand.Intn(10)
|
||||
name := fmt.Sprintf("User_%d", randInd)
|
||||
|
|
@ -102,7 +102,7 @@ func testOneInsert(structure DataStructure, data []ds.MyData) float64 {
|
|||
return time.Since(start).Seconds()
|
||||
}
|
||||
|
||||
func testRepInsert(structure DataStructure, data []ds.MyData, nameStruct, mode string) {
|
||||
func TestInsert(structure DataStructure, data []ds.MyData, nameStruct, mode string) {
|
||||
BenchRes := make([]csvwriter.BenchmarkResult, 0)
|
||||
|
||||
allTestTime := time.Now()
|
||||
|
|
@ -141,19 +141,19 @@ func Test(nameStruct string, structure DataStructure, data TestData) {
|
|||
|
||||
// allTestTime := time.Now()
|
||||
|
||||
testRepInsert(structure, data.Items, nameStruct, "Случайный")
|
||||
testRepInsert(structure, data.ItemsSorted, nameStruct, "Отсортированный")
|
||||
TestInsert(structure, data.Items, nameStruct, "Случайный")
|
||||
TestInsert(structure, data.ItemsSorted, nameStruct, "Отсортированный")
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
testData := GenerateTestData()
|
||||
|
||||
var head_ll *ll.LinkedList = nil
|
||||
head_ll := &ll.LinkedList{}
|
||||
var head_ht *ht.HashTable = nil
|
||||
var head_bst *bst.BinSearchTree = nil
|
||||
|
||||
Test("Связный список", head_ll, testData)
|
||||
Test("Связный список", head_ht, testData)
|
||||
Test("Связный список", head_bst, testData)
|
||||
Test("Хеш таблица", head_ht, testData)
|
||||
Test("Бинарное дерево поиска", head_bst, testData)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package main
|
|||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
ds "source/pkg/data_struct"
|
||||
bst "source/pkg/structures/bin_search_tree"
|
||||
|
|
@ -31,60 +30,21 @@ func isInArr(arr []int, length int, target int) bool {
|
|||
func main() {
|
||||
fmt.Println("hello world!")
|
||||
|
||||
var head *bst.BinSearchTree = nil
|
||||
head := bst.NewBinSearchTree()
|
||||
|
||||
arr := make([]int, countNumbers)
|
||||
|
||||
var temp int
|
||||
for i := 0; i < countNumbers; i++ {
|
||||
// Генерируем уникальное случайное число
|
||||
for {
|
||||
temp = rand.Intn(100) // 0 до 99
|
||||
if !isInArr(arr, i, temp) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
arr[i] = temp
|
||||
|
||||
nameStr := fmt.Sprintf("name_%02d", temp)
|
||||
phoneStr := fmt.Sprintf("phone_%02d", temp)
|
||||
|
||||
data := ds.NewData(nameStr, phoneStr)
|
||||
|
||||
head = head.Insert(*data)
|
||||
fmt.Printf("%d ", arr[i])
|
||||
for i := 1; i <= 20; i++ {
|
||||
name := fmt.Sprintf("User_%02d", i)
|
||||
phone := fmt.Sprintf("Phone_%02d", i)
|
||||
head.Insert(*ds.NewData(name, phone))
|
||||
}
|
||||
|
||||
fmt.Printf("\n\nКоличество узлов: %d\n", head.Len())
|
||||
|
||||
pressEnterToContinue()
|
||||
|
||||
fmt.Println("\ninorder traversal:")
|
||||
head.BstInorderTraversal()
|
||||
|
||||
tarName := "name_44"
|
||||
head.Delete("User_05")
|
||||
fmt.Println("Удаляем User_05")
|
||||
|
||||
fmt.Printf("\nПоиск '%s' перед удалением: ", tarName)
|
||||
if found, ok := head.Search(tarName); ok {
|
||||
fmt.Printf("Найден: %s\n", found)
|
||||
} else {
|
||||
fmt.Printf("НЕ найден!\n")
|
||||
}
|
||||
|
||||
fmt.Printf("\nУдаляем элемент с значением %s:\n", tarName)
|
||||
|
||||
head = head.Delete(tarName)
|
||||
|
||||
fmt.Printf("\nКоличество узлов после удаления: %d\n", head.Len())
|
||||
|
||||
fmt.Printf("Поиск '%s' после удаления: ", tarName)
|
||||
if found, ok := head.Search(tarName); ok {
|
||||
fmt.Printf("ОШИБКА! Все еще существует: %s\n", found)
|
||||
} else {
|
||||
fmt.Printf("Успешно удален\n")
|
||||
}
|
||||
|
||||
fmt.Println("\ninorder traversal after delete:")
|
||||
head.BstInorderTraversal()
|
||||
|
||||
fmt.Println(head.Search("User_07"))
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,8 +30,23 @@ import (
|
|||
|
||||
func main() {
|
||||
fmt.Println("hello world")
|
||||
hashTable := ht.NewHashTable(8, 0.75)
|
||||
hashTable.Insert(*ds.NewData("User_0", "Phone_0"))
|
||||
head := ht.NewHashTable(8, 0.75)
|
||||
|
||||
for i := 1; i <= 40; i++ {
|
||||
name := fmt.Sprintf("User_%02d", i)
|
||||
phone := fmt.Sprintf("Phone_%02d", i)
|
||||
head.Insert(*ds.NewData(name, phone))
|
||||
}
|
||||
|
||||
head.Print()
|
||||
|
||||
head.Delete("User_05")
|
||||
fmt.Println("Удаляем User_05")
|
||||
|
||||
head.Print()
|
||||
|
||||
fmt.Println(head.Search("User_07"))
|
||||
|
||||
// Чтение всего файла
|
||||
|
||||
// const filePath = "../data/onegin.txt"
|
||||
|
|
|
|||
|
|
@ -3,16 +3,13 @@ package main
|
|||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
dg "source/pkg/gen_data"
|
||||
rs "source/pkg/resulter"
|
||||
ds "source/pkg/data_struct"
|
||||
|
||||
// rs "source/pkg/resulter"
|
||||
ll "source/pkg/structures/linked_list"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func isInArr(arr []int, length int, target int) bool {
|
||||
for i := 0; i < length; i++ {
|
||||
if arr[i] == target {
|
||||
|
|
@ -37,135 +34,19 @@ func pressEnterToContinue() {
|
|||
func main() {
|
||||
fmt.Println("hello world!")
|
||||
|
||||
results := make([]rs.BenchmarkResult, 0, countUsers)
|
||||
averageInsertTime := 0.
|
||||
head := ll.NewLinkedList()
|
||||
|
||||
Razdelitel()
|
||||
fmt.Println("Тестирование вставки:")
|
||||
var head *ll.LinkedList = nil
|
||||
|
||||
for testNum := 0; testNum < countRepeat; testNum++ {
|
||||
head = nil
|
||||
|
||||
testData := dg.RecordsShuffled(countUsers)
|
||||
|
||||
start := time.Now()
|
||||
|
||||
for i := 0; i < countUsers; i++ {
|
||||
head.Insert(testData[i])
|
||||
}
|
||||
|
||||
elapsed := time.Since(start).Seconds()
|
||||
averageInsertTime += elapsed
|
||||
|
||||
results = append(results, rs.BenchmarkResult{
|
||||
Structure: "Связный список", Mode: "Случайный", Operation: "Вставка", Time: elapsed,
|
||||
})
|
||||
}
|
||||
averageInsertTime /= countRepeat
|
||||
results = append(results, rs.BenchmarkResult{
|
||||
Structure: "Связный список", Mode: "Случайный", Operation: "Вставка", Time: averageInsertTime,
|
||||
})
|
||||
for i := 0; i < 6; i++ {
|
||||
fmt.Println(results[i].ToString())
|
||||
for i := 1; i <= 20; i++ {
|
||||
name := fmt.Sprintf("User_%02d", i)
|
||||
phone := fmt.Sprintf("Phone_%02d", i)
|
||||
head.Insert(*ds.NewData(name, phone))
|
||||
}
|
||||
|
||||
Razdelitel()
|
||||
// fmt.Println("Тестирование Поиска:")
|
||||
// // results = make([]rs.BenchmarkResult, 0, countUsers)
|
||||
// averageSearchTime := 0.
|
||||
head.PrintAll()
|
||||
|
||||
// for testNum := 0; testNum < countRepeat; testNum++ {
|
||||
// // var head *ll.LinkedList = nil
|
||||
// // head = dg.RecordsShuffled(countUsers)
|
||||
head.Delete("User_05")
|
||||
|
||||
// testData := make([]ds.MyData, countRandomSearch)
|
||||
head.PrintAll()
|
||||
|
||||
// for i := 0; i < countRandomSearch; i++ {
|
||||
// // randInd := rand.Intn(countUsers)
|
||||
// randInd := rand.Intn(1000) + 9000
|
||||
// testData[i], _ = head.GetByInd(randInd)
|
||||
// // fmt.Println(randInd)
|
||||
// }
|
||||
|
||||
// start := time.Now()
|
||||
|
||||
// for i := 0; i < countRandomSearch; i++ {
|
||||
// head.Search(testData[i].Name)
|
||||
// }
|
||||
|
||||
// elapsed := time.Since(start).Seconds()
|
||||
// averageSearchTime += elapsed
|
||||
|
||||
// results = append(results, rs.BenchmarkResult{
|
||||
// Structure: "Связный список", Mode: "Случайный", Operation: "Поиск", Time: elapsed,
|
||||
// })
|
||||
// }
|
||||
// averageSearchTime /= countRepeat
|
||||
// results = append(results, rs.BenchmarkResult{
|
||||
// Structure: "Связный список", Mode: "Случайный", Operation: "Поиск", Time: averageSearchTime,
|
||||
// })
|
||||
// for i := 0; i < len(results); i++ {
|
||||
// fmt.Println(results[i].ToString())
|
||||
// }
|
||||
|
||||
resultsS := runSearchBenchmark(head)
|
||||
for i := 0; i < len(resultsS); i++ {
|
||||
fmt.Println(resultsS[i].ToString())
|
||||
}
|
||||
// rs.AppendRaw(results)
|
||||
}
|
||||
|
||||
func runSearchBenchmark(head *ll.LinkedList) []rs.BenchmarkResult {
|
||||
fmt.Println("\n=== Тестирование Поиска ===")
|
||||
|
||||
const countRandomSearch = 1000 // уменьшим для поиска
|
||||
|
||||
var results []rs.BenchmarkResult
|
||||
var totalTime float64
|
||||
|
||||
// Предварительно собираем имена для поиска
|
||||
names := make([]string, countUsers)
|
||||
for i := 0; i < countUsers; i++ {
|
||||
data, found := head.GetByInd(i)
|
||||
if found {
|
||||
names[i] = data.Name
|
||||
}
|
||||
}
|
||||
|
||||
for testNum := 0; testNum < countRepeat; testNum++ {
|
||||
// Выбираем случайные имена
|
||||
searchNames := make([]string, countRandomSearch)
|
||||
for i := 0; i < countRandomSearch; i++ {
|
||||
searchNames[i] = names[rand.Intn(countUsers)]
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
|
||||
for _, name := range searchNames {
|
||||
el, ok := head.Search(name)
|
||||
if ok {
|
||||
fmt.Println(el)
|
||||
}
|
||||
}
|
||||
elapsed := time.Since(start).Seconds()
|
||||
|
||||
totalTime += elapsed
|
||||
|
||||
fmt.Printf(" Тест %d: %.6f сек (%.2f мкс/оп)\n",
|
||||
testNum+1, elapsed,
|
||||
elapsed/float64(countRandomSearch)*1_000_000)
|
||||
}
|
||||
|
||||
avgTime := totalTime / float64(countRepeat)
|
||||
fmt.Printf("Среднее: %.6f сек\n", avgTime)
|
||||
|
||||
results = append(results, rs.BenchmarkResult{
|
||||
Structure: "Связный список",
|
||||
Mode: "Случайный",
|
||||
Operation: "Поиск",
|
||||
Time: avgTime,
|
||||
})
|
||||
|
||||
return results
|
||||
fmt.Println(head.Search("User_07"))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user