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