From 1b21f97e28bbfb3f1ed9eab6fd1ce300bdf55e44 Mon Sep 17 00:00:00 2001 From: GordStep Date: Sat, 9 May 2026 22:34:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B8=D0=B5=D1=80=D0=B0=D1=80=D1=85=D0=B8=D0=B8?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - test/ -- папка с файлами для тестов каждой структуры - pkg/ -- папка с реализациями и вспомогательными модулями --- .../sorce/hash_table/hash_table.c | 1 - .../{sorce => source}/bin_search_tree_C/bst.c | 0 .../{sorce => source}/bin_search_tree_C/bst.h | 0 .../bin_search_tree_C/queue.c | 0 .../bin_search_tree_C/queue.h | 0 .../linked_list_c/linked_list.c | 0 .../linked_list_c/linked_list.h | 0 .../{sorce => source/old_c}/main.c | 0 .../{sorce => source/old_c}/main_tree.c | 0 .../{sorce => source/old_c}/swap.c | 0 .../pkg/bin_search_tree/bin_search_tree.go} | 43 ++-- .../source/pkg/data_struct/data_structure.go | 19 ++ .../source/pkg/data_struct/qsort.go | 37 +++ .../source/pkg/gen_data/data_generator.go | 41 +++ .../source/pkg/hash_table/hash_string.go | 15 ++ .../source/pkg/hash_table/hash_table.go | 242 ++++++++++++++++++ .../pkg/hash_table/qsort.go} | 6 +- .../pkg}/linked_list/linked_list.go | 35 ++- .../tests/test_bst}/main.go | 17 +- .../source/tests/test_ht/main.go | 71 +++++ .../tests/test_ll}/main.go | 8 +- 21 files changed, 479 insertions(+), 56 deletions(-) delete mode 100644 stepushovgs/data-structures/sorce/hash_table/hash_table.c rename stepushovgs/data-structures/{sorce => source}/bin_search_tree_C/bst.c (100%) rename stepushovgs/data-structures/{sorce => source}/bin_search_tree_C/bst.h (100%) rename stepushovgs/data-structures/{sorce => source}/bin_search_tree_C/queue.c (100%) rename stepushovgs/data-structures/{sorce => source}/bin_search_tree_C/queue.h (100%) rename stepushovgs/data-structures/{sorce => source}/linked_list_c/linked_list.c (100%) rename stepushovgs/data-structures/{sorce => source}/linked_list_c/linked_list.h (100%) rename stepushovgs/data-structures/{sorce => source/old_c}/main.c (100%) rename stepushovgs/data-structures/{sorce => source/old_c}/main_tree.c (100%) rename stepushovgs/data-structures/{sorce => source/old_c}/swap.c (100%) rename stepushovgs/data-structures/{sorce/bin_search_tree/bst.go => source/pkg/bin_search_tree/bin_search_tree.go} (80%) create mode 100644 stepushovgs/data-structures/source/pkg/data_struct/data_structure.go create mode 100644 stepushovgs/data-structures/source/pkg/data_struct/qsort.go create mode 100644 stepushovgs/data-structures/source/pkg/gen_data/data_generator.go create mode 100644 stepushovgs/data-structures/source/pkg/hash_table/hash_string.go create mode 100644 stepushovgs/data-structures/source/pkg/hash_table/hash_table.go rename stepushovgs/data-structures/{sorce/linked_list/q_sort_ll.go => source/pkg/hash_table/qsort.go} (74%) rename stepushovgs/data-structures/{sorce => source/pkg}/linked_list/linked_list.go (77%) rename stepushovgs/data-structures/{sorce/bin_search_tree => source/tests/test_bst}/main.go (88%) create mode 100644 stepushovgs/data-structures/source/tests/test_ht/main.go rename stepushovgs/data-structures/{sorce/linked_list => source/tests/test_ll}/main.go (90%) diff --git a/stepushovgs/data-structures/sorce/hash_table/hash_table.c b/stepushovgs/data-structures/sorce/hash_table/hash_table.c deleted file mode 100644 index 8b13789..0000000 --- a/stepushovgs/data-structures/sorce/hash_table/hash_table.c +++ /dev/null @@ -1 +0,0 @@ - diff --git a/stepushovgs/data-structures/sorce/bin_search_tree_C/bst.c b/stepushovgs/data-structures/source/bin_search_tree_C/bst.c similarity index 100% rename from stepushovgs/data-structures/sorce/bin_search_tree_C/bst.c rename to stepushovgs/data-structures/source/bin_search_tree_C/bst.c diff --git a/stepushovgs/data-structures/sorce/bin_search_tree_C/bst.h b/stepushovgs/data-structures/source/bin_search_tree_C/bst.h similarity index 100% rename from stepushovgs/data-structures/sorce/bin_search_tree_C/bst.h rename to stepushovgs/data-structures/source/bin_search_tree_C/bst.h diff --git a/stepushovgs/data-structures/sorce/bin_search_tree_C/queue.c b/stepushovgs/data-structures/source/bin_search_tree_C/queue.c similarity index 100% rename from stepushovgs/data-structures/sorce/bin_search_tree_C/queue.c rename to stepushovgs/data-structures/source/bin_search_tree_C/queue.c diff --git a/stepushovgs/data-structures/sorce/bin_search_tree_C/queue.h b/stepushovgs/data-structures/source/bin_search_tree_C/queue.h similarity index 100% rename from stepushovgs/data-structures/sorce/bin_search_tree_C/queue.h rename to stepushovgs/data-structures/source/bin_search_tree_C/queue.h diff --git a/stepushovgs/data-structures/sorce/linked_list_c/linked_list.c b/stepushovgs/data-structures/source/linked_list_c/linked_list.c similarity index 100% rename from stepushovgs/data-structures/sorce/linked_list_c/linked_list.c rename to stepushovgs/data-structures/source/linked_list_c/linked_list.c diff --git a/stepushovgs/data-structures/sorce/linked_list_c/linked_list.h b/stepushovgs/data-structures/source/linked_list_c/linked_list.h similarity index 100% rename from stepushovgs/data-structures/sorce/linked_list_c/linked_list.h rename to stepushovgs/data-structures/source/linked_list_c/linked_list.h diff --git a/stepushovgs/data-structures/sorce/main.c b/stepushovgs/data-structures/source/old_c/main.c similarity index 100% rename from stepushovgs/data-structures/sorce/main.c rename to stepushovgs/data-structures/source/old_c/main.c diff --git a/stepushovgs/data-structures/sorce/main_tree.c b/stepushovgs/data-structures/source/old_c/main_tree.c similarity index 100% rename from stepushovgs/data-structures/sorce/main_tree.c rename to stepushovgs/data-structures/source/old_c/main_tree.c diff --git a/stepushovgs/data-structures/sorce/swap.c b/stepushovgs/data-structures/source/old_c/swap.c similarity index 100% rename from stepushovgs/data-structures/sorce/swap.c rename to stepushovgs/data-structures/source/old_c/swap.c diff --git a/stepushovgs/data-structures/sorce/bin_search_tree/bst.go b/stepushovgs/data-structures/source/pkg/bin_search_tree/bin_search_tree.go similarity index 80% rename from stepushovgs/data-structures/sorce/bin_search_tree/bst.go rename to stepushovgs/data-structures/source/pkg/bin_search_tree/bin_search_tree.go index a909c0d..b3a653a 100644 --- a/stepushovgs/data-structures/sorce/bin_search_tree/bst.go +++ b/stepushovgs/data-structures/source/pkg/bin_search_tree/bin_search_tree.go @@ -1,21 +1,20 @@ -package main +package bin_search_tree import ( "fmt" + ds "source/pkg/data_struct" ) type BinSearchTree struct { - name string - phone string + data ds.MyData left *BinSearchTree right *BinSearchTree } -func NewBinSearchTree(name, phone string) *BinSearchTree { +func NewBinSearchTree(data ds.MyData) *BinSearchTree { return &BinSearchTree{ - name: name, - phone: phone, + data: data, left: nil, right: nil, } @@ -42,14 +41,14 @@ func (bst *BinSearchTree) Maximum() *BinSearchTree { } func (node *BinSearchTree) PrintNode() { - fmt.Printf("Имя: %s, Телефон: %s", node.name, node.phone) + fmt.Print(node.data.ToString()) } func (node *BinSearchTree) ToString() string { if node == nil { return "nil" } - return fmt.Sprintf("Имя: %s, Телефон: %s", node.name, node.phone) + return node.data.ToString() } func (bst *BinSearchTree) BstInorderTraversal() { @@ -93,10 +92,10 @@ func (node *BinSearchTree) search(targetName string) (*BinSearchTree, bool) { if node == nil { return nil, false } - if node.name == targetName { + if node.data.Name == targetName { return node, true } - if targetName < node.name { + if targetName < node.data.Name { return node.left.search(targetName) } return node.right.search(targetName) @@ -117,15 +116,15 @@ func (node *BinSearchTree) search(targetName string) (*BinSearchTree, bool) { // return targetNode.ToString(), ok // } -func (node *BinSearchTree) Insert(name, phone string) *BinSearchTree { +func (node *BinSearchTree) Insert(data ds.MyData) *BinSearchTree { if node == nil { - return NewBinSearchTree(name, phone) - } else if name < node.name { - node.left = node.left.Insert(name, phone) - } else if name > node.name { - node.right = node.right.Insert(name, phone) + return NewBinSearchTree(data) + } else if data.Name < node.data.Name { + node.left = node.left.Insert(data) + } else if data.Name > node.data.Name { + node.right = node.right.Insert(data) } else { - node.phone = phone // Заменяем существующее значение + node.data.Phone = data.Phone // Заменяем существующее значение } return node } @@ -159,18 +158,18 @@ func (root *BinSearchTree) Delete(targetName string) *BinSearchTree { return nil } - if targetName < root.name { + if targetName < root.data.Name { root.left = root.left.Delete(targetName) - } else if targetName > root.name { + } else if targetName > root.data.Name { root.right = root.right.Delete(targetName) } else if root.left != nil && root.right != nil { temp := root.right.Minimum() - root.name = root.right.Minimum().name - root.phone = root.right.Minimum().phone + root.data.Name = root.right.Minimum().data.Name + root.data.Phone = root.right.Minimum().data.Phone // strcpy(root->name, bst_minimum(root->right)->name); // strcpy(root->phone, bst_minimum(root->right)->phone); - root.right = root.right.Delete(temp.name) + root.right = root.right.Delete(temp.data.Name) } else { if root.left != nil { return root.left diff --git a/stepushovgs/data-structures/source/pkg/data_struct/data_structure.go b/stepushovgs/data-structures/source/pkg/data_struct/data_structure.go new file mode 100644 index 0000000..cbceb85 --- /dev/null +++ b/stepushovgs/data-structures/source/pkg/data_struct/data_structure.go @@ -0,0 +1,19 @@ +package data_struct + +import "fmt" + +type MyData struct { + Name string + Phone string +} + +func NewData(name, phone string) *MyData { + return &MyData{ + Name: name, + Phone: phone, + } +} + +func (d *MyData) ToString() string { + return fmt.Sprintf("Имя: %s, Телефон: %s", d.Name, d.Phone) +} diff --git a/stepushovgs/data-structures/source/pkg/data_struct/qsort.go b/stepushovgs/data-structures/source/pkg/data_struct/qsort.go new file mode 100644 index 0000000..00e0c01 --- /dev/null +++ b/stepushovgs/data-structures/source/pkg/data_struct/qsort.go @@ -0,0 +1,37 @@ +package data_struct + +func QSort(arr []MyData, l, r int) []MyData { + if l < r { + s := Partition_Hoa(arr, l, r) + arr = QSort(arr, l, s) + arr = QSort(arr, s+1, r) + } + return arr +} + +func Partition_Hoa(arr []MyData, l, r int) int { + p := arr[(l+r)/2].Name + i := l - 1 + j := r + 1 + + for { + for { + i++ + if arr[i].Name >= p { + break + } + } + for { + j-- + if arr[j].Name <= p { + break + } + } + + if i >= j { + return j + } + + arr[i], arr[j] = arr[j], arr[i] + } +} diff --git a/stepushovgs/data-structures/source/pkg/gen_data/data_generator.go b/stepushovgs/data-structures/source/pkg/gen_data/data_generator.go new file mode 100644 index 0000000..31de838 --- /dev/null +++ b/stepushovgs/data-structures/source/pkg/gen_data/data_generator.go @@ -0,0 +1,41 @@ +package gendata + +import ( + "fmt" + "math/rand" + ds "source/pkg/data_struct" +) + +const ( + MAX_USER_IND = 10000 + PHONE_LEN = 11 +) + +func genRandomPhone() string { + phone := "" + for i := 0; i < PHONE_LEN; i++ { + phone += fmt.Sprintf("%d", rand.Intn(10)) + } + + return phone +} + +func RecordsShuffled(count int) []ds.MyData { + data := make([]ds.MyData, count) + number := 0 + for i := 0; i < count; i++ { + number = rand.Intn(MAX_USER_IND) + data[i].Name = fmt.Sprintf("User_%05d", number) + data[i].Phone = genRandomPhone() + } + + return data +} + +func RecordsSorted(count int) []ds.MyData { + data := RecordsShuffled(count) + + data = ds.QSort(data, 0, len(data)-1) + + return data +} diff --git a/stepushovgs/data-structures/source/pkg/hash_table/hash_string.go b/stepushovgs/data-structures/source/pkg/hash_table/hash_string.go new file mode 100644 index 0000000..0efa5bb --- /dev/null +++ b/stepushovgs/data-structures/source/pkg/hash_table/hash_string.go @@ -0,0 +1,15 @@ +package hash_table + +func GetHashString(str string) int { + hash := 0 + + for _, ch := range str { + hash = (hash << 5) - hash + int(ch) + } + + if hash < 0 { + hash = -hash + } + + return hash +} diff --git a/stepushovgs/data-structures/source/pkg/hash_table/hash_table.go b/stepushovgs/data-structures/source/pkg/hash_table/hash_table.go new file mode 100644 index 0000000..7696bd6 --- /dev/null +++ b/stepushovgs/data-structures/source/pkg/hash_table/hash_table.go @@ -0,0 +1,242 @@ +package hash_table + +import ( + "bufio" + "fmt" + "os" +) + +// HashTable - хеш-таблица с цепочками +type HashTable struct { + buckets []*bucket + size int + capacity int + loadFactor float64 +} + +type bucket struct { + head *elementHT +} + +type elementHT struct { + name string + phone string + next *elementHT +} + +// NewHashTable - создает новую хеш-таблицу +func NewHashTable(capacity int, loadFactor float64) *HashTable { + + buckets := make([]*bucket, capacity) + + for i := 0; i < capacity; i++ { + buckets[i] = &bucket{} + } + + return &HashTable{ + buckets: buckets, + size: 0, + capacity: capacity, + loadFactor: loadFactor, + } +} + +// func (h HashTable) getIndex(name string) int { +// return GetHashString(name) % h.size +// } + +// func (h HashTable) Add(name string) { + +// } + +func (ht *HashTable) GetIndex(name string) int { + hash := GetHashString(name) + return hash % ht.capacity +} + +// func (ht *HashTable) getIndex(hash int) int { +// return hash % ht.capacity +// } + +func (h *HashTable) Put(name string, phone string) { + + if h.size >= int(float64(h.capacity)*h.loadFactor) { + h.resize() + } + + ind := h.GetIndex(name) + + buck := h.buckets[ind] + + current := buck.head + + for current != nil { + if current.name == name { + current.phone = phone + return + } + + current = current.next + } + + newHead := &elementHT{ + name: name, + phone: phone, + next: buck.head, + } + + buck.head = newHead + h.size++ +} + +func (h *HashTable) Get(name string) (phone string, status bool) { + ind := h.GetIndex(name) + + buck := h.buckets[ind] + + current := buck.head + + for current != nil { + if current.name == name { + return current.phone, true + } + + current = current.next + } + + return "", false +} + +func pressEnterToContinue() { + fmt.Print("Нажмите Enter для продолжения...") + bufio.NewReader(os.Stdin).ReadBytes('\n') +} + +// resize - увеличивает размер таблицы +func (ht *HashTable) resize() { + + // fmt.Printf("Resize table!\n elements: %d(%.3f%%)\n old capacity: %d\n new capacity: %d\n", ht.size, float64(ht.size)/float64(ht.capacity), ht.capacity, 2*ht.capacity) + + // ht.Print() + + // pressEnterToContinue() + + newCapacity := ht.capacity * 2 + newHT := NewHashTable(newCapacity, ht.loadFactor) + + for _, b := range ht.buckets { + current := b.head + for current != nil { + newHT.Put(current.name, current.phone) + current = current.next + } + } + + ht.buckets = newHT.buckets + ht.capacity = newCapacity +} + +func (ht *HashTable) Remove(name string) bool { + ind := ht.GetIndex(name) + + buck := ht.buckets[ind] + + if buck.head == nil { + return false + } + + if buck.head.name == name { + buck.head = buck.head.next + ht.size-- + return true + } + + prev := buck.head + current := buck.head.next + + for current != nil { + if current.name == name { + prev.next = current.next + ht.size-- + return true + } + prev = current + current = current.next + } + + return false +} + +func (ht *HashTable) Contains(name string) bool { + _, ok := ht.Get(name) + return ok +} + +func (elem *elementHT) ToString() string { + if elem == nil { + return "nil" + } + + return fmt.Sprintf("Имя: %s, Телефон: %s", elem.name, elem.phone) +} + +func (ht *HashTable) Print() { + for ind := 0; ind < ht.capacity; ind++ { + buck := ht.buckets[ind] + current := buck.head + + bucketsStr := "" + + for current != nil { + bucketsStr += " --> " + current.ToString() + current = current.next + } + fmt.Printf("[%d]: %s\n", ind, bucketsStr) + } +} + +func (ht *HashTable) listAll() []elementHT { + data := make([]elementHT, ht.size) + + index := 0 + + for ind := 0; ind < ht.capacity; ind++ { + buck := ht.buckets[ind] + current := buck.head + + for current != nil { + data[index] = elementHT{ + name: current.name, + phone: current.phone, + } + index++ + // fmt.Println(current.name, current.phone) + current = current.next + } + } + + return data +} + +func (ht *HashTable) ListAll() []elementHT { + // fmt.Printf("Size: %d, Capacity: %d\n", ht.size, ht.capacity) + data := ht.listAll() + + data = QSortElementsHT(data, 0, len(data)-1) + + // for i, el := range data { + // fmt.Printf("[%d]: \"%s\", %d\n", i, el.name, el.phone) + // } + return data +} + +// func (ht *HashTable) PrintMostPopularnames(phone int) { +// // fmt.Printf("Size: %d, Capacity: %d\n", ht.size, ht.capacity) +// data := ht.listAll() + +// data = QSortElementsHT(data, 0, len(data)-1) + +// for i := 0; i < phone; i++ { +// fmt.Printf("[%d]: %3d : %s\n", i, ht.GetIndex(data[len(data)-i-1].name), data[len(data)-i-1].ToString()) +// } +// } diff --git a/stepushovgs/data-structures/sorce/linked_list/q_sort_ll.go b/stepushovgs/data-structures/source/pkg/hash_table/qsort.go similarity index 74% rename from stepushovgs/data-structures/sorce/linked_list/q_sort_ll.go rename to stepushovgs/data-structures/source/pkg/hash_table/qsort.go index 2fd6c0c..fb01c32 100644 --- a/stepushovgs/data-structures/sorce/linked_list/q_sort_ll.go +++ b/stepushovgs/data-structures/source/pkg/hash_table/qsort.go @@ -1,6 +1,6 @@ -package main +package hash_table -func QSortElementsHT(arr []LinkedList, l, r int) []LinkedList { +func QSortElementsHT(arr []elementHT, l, r int) []elementHT { if l < r { s := Partition_Hoa(arr, l, r) arr = QSortElementsHT(arr, l, s) @@ -9,7 +9,7 @@ func QSortElementsHT(arr []LinkedList, l, r int) []LinkedList { return arr } -func Partition_Hoa(arr []LinkedList, l, r int) int { +func Partition_Hoa(arr []elementHT, l, r int) int { p := arr[(l+r)/2].name i := l - 1 j := r + 1 diff --git a/stepushovgs/data-structures/sorce/linked_list/linked_list.go b/stepushovgs/data-structures/source/pkg/linked_list/linked_list.go similarity index 77% rename from stepushovgs/data-structures/sorce/linked_list/linked_list.go rename to stepushovgs/data-structures/source/pkg/linked_list/linked_list.go index db20023..9d33735 100644 --- a/stepushovgs/data-structures/sorce/linked_list/linked_list.go +++ b/stepushovgs/data-structures/source/pkg/linked_list/linked_list.go @@ -1,7 +1,8 @@ -package main +package linked_list import ( "fmt" + ds "source/pkg/data_struct" ) /* @@ -21,22 +22,20 @@ def ll_list_all(head) — собирает все записи в список */ type LinkedList struct { - name string - phone string + data ds.MyData next *LinkedList } -func NewLinkedList(name, phone string) *LinkedList { +func NewLinkedList(data ds.MyData) *LinkedList { return &LinkedList{ - name: name, - phone: phone, - next: nil, + data: data, + next: nil, } } func (ll *LinkedList) ToString() string { - return fmt.Sprintf("Имя: %s, Телефон: %s", ll.name, ll.phone) + return ll.data.ToString() } func (ll *LinkedList) Len() int { @@ -55,8 +54,8 @@ func (ll *LinkedList) Len() int { return len } -func (ll *LinkedList) Insert(name, phone string) *LinkedList { - newNode := NewLinkedList(name, phone) +func (ll *LinkedList) Insert(data ds.MyData) *LinkedList { + newNode := NewLinkedList(data) if ll == nil { return newNode @@ -74,8 +73,8 @@ func (ll *LinkedList) Search(targetName string) (string, bool) { current := ll for current != nil { - if current.name == targetName { - return current.phone, true + if current.data.Name == targetName { + return current.data.Phone, true } current = current.next @@ -99,7 +98,7 @@ func (ll *LinkedList) Delete(targetName string) (*LinkedList, bool) { if ll == nil { return nil, false } - if ll.name == targetName { + if ll.data.Name == targetName { return ll.next, true } @@ -107,7 +106,7 @@ func (ll *LinkedList) Delete(targetName string) (*LinkedList, bool) { current := ll.next for current != nil { - if current.name == targetName { + if current.data.Name == targetName { prev.next = current.next } prev = current @@ -117,17 +116,17 @@ func (ll *LinkedList) Delete(targetName string) (*LinkedList, bool) { return ll, false } -func (ll *LinkedList) listAll() []LinkedList { +func (ll *LinkedList) listAll() []ds.MyData { current := ll - listLL := make([]LinkedList, ll.Len()) + listLL := make([]ds.MyData, ll.Len()) ind := 0 for current != nil { - listLL[ind] = *current + listLL[ind] = current.data ind++ current = current.next } - listLL = QSortElementsHT(listLL, 0, len(listLL)-1) + listLL = ds.QSort(listLL, 0, len(listLL)-1) return listLL } diff --git a/stepushovgs/data-structures/sorce/bin_search_tree/main.go b/stepushovgs/data-structures/source/tests/test_bst/main.go similarity index 88% rename from stepushovgs/data-structures/sorce/bin_search_tree/main.go rename to stepushovgs/data-structures/source/tests/test_bst/main.go index 5ef8e0d..197b5dd 100644 --- a/stepushovgs/data-structures/sorce/bin_search_tree/main.go +++ b/stepushovgs/data-structures/source/tests/test_bst/main.go @@ -5,6 +5,8 @@ import ( "fmt" "math/rand" "os" + bst "source/pkg/bin_search_tree" + ds "source/pkg/data_struct" ) const ( @@ -29,7 +31,7 @@ func isInArr(arr []int, length int, target int) bool { func main() { fmt.Println("hello world!") - var head *BinSearchTree = nil + var head *bst.BinSearchTree = nil arr := make([]int, countNumbers) @@ -48,7 +50,9 @@ func main() { nameStr := fmt.Sprintf("name_%02d", temp) phoneStr := fmt.Sprintf("phone_%02d", temp) - head = head.Insert(nameStr, phoneStr) + data := ds.NewData(nameStr, phoneStr) + + head = head.Insert(*data) fmt.Printf("%d ", arr[i]) } @@ -72,7 +76,7 @@ func main() { head = head.Delete(tarName) - fmt.Printf("\nКоличество узлов после удаления: %d\n", countNodes(head)) + fmt.Printf("\nКоличество узлов после удаления: %d\n", head.Len()) fmt.Printf("Поиск '%s' после удаления: ", tarName) if found, ok := head.Search(tarName); ok { @@ -84,10 +88,3 @@ func main() { fmt.Println("\ninorder traversal after delete:") head.BstInorderTraversal() } - -func countNodes(root *BinSearchTree) int { - if root == nil { - return 0 - } - return 1 + countNodes(root.left) + countNodes(root.right) -} diff --git a/stepushovgs/data-structures/source/tests/test_ht/main.go b/stepushovgs/data-structures/source/tests/test_ht/main.go new file mode 100644 index 0000000..4891571 --- /dev/null +++ b/stepushovgs/data-structures/source/tests/test_ht/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "fmt" + + // hash_table "hash-table-task/hash-table" + + ht "source/pkg/hash_table" +) + +/* + +1. Сконструировать и реализовать свою хеш таблицу + +- изначальный размер 8, коэф-т загрузки 0.75 + +- Преобразование подаваемого данного в индекс с помощью хеш функции(в ручну) пример: полиномиальный хеш + +- Коллизии обрабатываются методом цепочек, каждая корзина таблицы - список в котором хранятся пары значений key-value + +- При превышении коэф-та загрузки происходит перехеширование таблицы, размер увеличивается вдвое, все пары заново вставляются в таблицу. + +2. Читаем текстовый файл, разбивает на слова, приводим к нижнему регистру, подсчитываем повторения каждого слова: key - слово, value - кол-во повторений + +- На вывод 10 самых встречающихся слов, для каждого слова выводим: ind(hash), key, value +- Текст - первая глава, первые три стиха Евгений Онегин + +*/ + +func main() { + // Чтение всего файла + + // const filePath = "../data/onegin.txt" + // // const filePath = "../data/onegin_full.txt" + + // data, err := os.ReadFile(filePath) + // text := string(data) + // if err != nil { + // fmt.Println("Ошибка чтения файла:", err) + // return + // } + // fmt.Println(text) + + // text = strings.ToLower(text) + + // // Разбиение на слова (разделители: пробелы и переводы строк) + // re := regexp.MustCompile(`[\p{L}\p{N}-]+`) + // words := re.FindAllString(text, -1) + + // fmt.Printf("Найдено слов: %d\n", len(words)) + // for i, word := range words { + // fmt.Printf("Слово %d: %s\n", i+1, word) + // } + + hashTable := ht.NewHashTable(8, 0.95) + + for i, word := range words { + fmt.Printf("%d : %s\n", i, word) + hashTable.Put(word, 1) + } + + fmt.Println("\nХеш таблица текста: ") + hashTable.Print() + + // fmt.Println("Отсортированные ячейки таблицы: ") + // hashTable.PrintSort() + + fmt.Println("\nСамые часто встречающиеся слова: ") + + // hashTable.PrintMostPopularWords(10) +} diff --git a/stepushovgs/data-structures/sorce/linked_list/main.go b/stepushovgs/data-structures/source/tests/test_ll/main.go similarity index 90% rename from stepushovgs/data-structures/sorce/linked_list/main.go rename to stepushovgs/data-structures/source/tests/test_ll/main.go index a77923a..20c7824 100644 --- a/stepushovgs/data-structures/sorce/linked_list/main.go +++ b/stepushovgs/data-structures/source/tests/test_ll/main.go @@ -5,6 +5,9 @@ import ( "fmt" "math/rand" "os" + ds "source/pkg/data_struct" + ll "source/pkg/linked_list" + // dg "source/pkg/gendata" ) const countNumbers = 64 @@ -26,7 +29,7 @@ func pressEnterToContinue() { func main() { fmt.Println("hello world!") - var head *LinkedList = nil + var head *ll.LinkedList = nil arr := make([]int, countNumbers) @@ -44,8 +47,9 @@ func main() { nameStr := fmt.Sprintf("name_%02d", temp) phoneStr := fmt.Sprintf("phone_%02d", temp) + data := ds.NewData(nameStr, phoneStr) - head = head.Insert(nameStr, phoneStr) + head = head.Insert(*data) fmt.Printf("%d ", arr[i]) // pressEnterToContinue() }