From 6e259a4770666ad6f3abda5ad8c5c660d0e59f03 Mon Sep 17 00:00:00 2001 From: GordStep Date: Wed, 13 May 2026 22:03:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D1=8B=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=B8=20=D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BC=D0=B8=D0=BD=D0=B8=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=B2=D1=81=D0=B5=D1=85=20=D1=81=D1=82?= =?UTF-8?q?=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bin_search_tree/bin_search_tree.go | 6 +- .../pkg/structures/hash_table/hash_table.go | 2 +- .../pkg/structures/linked_list/linked_list.go | 8 +- .../source/tests/benchmark/main.go | 18 +-- .../source/tests/test_bst/main.go | 60 ++------ .../source/tests/test_ht/main.go | 19 ++- .../source/tests/test_ll/main.go | 143 ++---------------- 7 files changed, 60 insertions(+), 196 deletions(-) diff --git a/stepushovgs/data-structures/source/pkg/structures/bin_search_tree/bin_search_tree.go b/stepushovgs/data-structures/source/pkg/structures/bin_search_tree/bin_search_tree.go index f647ddb..2154865 100644 --- a/stepushovgs/data-structures/source/pkg/structures/bin_search_tree/bin_search_tree.go +++ b/stepushovgs/data-structures/source/pkg/structures/bin_search_tree/bin_search_tree.go @@ -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, diff --git a/stepushovgs/data-structures/source/pkg/structures/hash_table/hash_table.go b/stepushovgs/data-structures/source/pkg/structures/hash_table/hash_table.go index fa566b2..32fed46 100644 --- a/stepushovgs/data-structures/source/pkg/structures/hash_table/hash_table.go +++ b/stepushovgs/data-structures/source/pkg/structures/hash_table/hash_table.go @@ -178,7 +178,7 @@ func (elem *elementHT) ToString() string { return "nil" } - return elem.ToString() + return elem.data.ToString() } func (ht *HashTable) Print() { diff --git a/stepushovgs/data-structures/source/pkg/structures/linked_list/linked_list.go b/stepushovgs/data-structures/source/pkg/structures/linked_list/linked_list.go index 82317b8..b383414 100644 --- a/stepushovgs/data-structures/source/pkg/structures/linked_list/linked_list.go +++ b/stepushovgs/data-structures/source/pkg/structures/linked_list/linked_list.go @@ -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 diff --git a/stepushovgs/data-structures/source/tests/benchmark/main.go b/stepushovgs/data-structures/source/tests/benchmark/main.go index 0140589..1bc241e 100644 --- a/stepushovgs/data-structures/source/tests/benchmark/main.go +++ b/stepushovgs/data-structures/source/tests/benchmark/main.go @@ -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) } diff --git a/stepushovgs/data-structures/source/tests/test_bst/main.go b/stepushovgs/data-structures/source/tests/test_bst/main.go index a16d473..f5e5ccb 100644 --- a/stepushovgs/data-structures/source/tests/test_bst/main.go +++ b/stepushovgs/data-structures/source/tests/test_bst/main.go @@ -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")) + } diff --git a/stepushovgs/data-structures/source/tests/test_ht/main.go b/stepushovgs/data-structures/source/tests/test_ht/main.go index 6443a3b..2920570 100644 --- a/stepushovgs/data-structures/source/tests/test_ht/main.go +++ b/stepushovgs/data-structures/source/tests/test_ht/main.go @@ -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" diff --git a/stepushovgs/data-structures/source/tests/test_ll/main.go b/stepushovgs/data-structures/source/tests/test_ll/main.go index b13d382..3420389 100644 --- a/stepushovgs/data-structures/source/tests/test_ll/main.go +++ b/stepushovgs/data-structures/source/tests/test_ll/main.go @@ -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")) }