2026-rff_mp/stepushovgs/data-structures/source/tests/test_bst/main.go
GordStep 4a214a2843 Изменение иерархии
- добавеление в отлсеживание go.mod
 - перенос хеш таблицы на общий формат хранения данных
 - удаление лишних
2026-05-10 12:30:21 +03:00

91 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"bufio"
"fmt"
"math/rand"
"os"
ds "source/pkg/data_struct"
bst "source/pkg/structures/bin_search_tree"
)
const (
countNumbers = 64
)
func pressEnterToContinue() {
fmt.Print("Нажмите Enter для продолжения...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
// isInArr проверяет, содержится ли target в срезе arr[:len]
func isInArr(arr []int, length int, target int) bool {
for i := 0; i < length; i++ {
if arr[i] == target {
return true
}
}
return false
}
func main() {
fmt.Println("hello world!")
var head *bst.BinSearchTree = nil
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])
}
fmt.Printf("\n\nКоличество узлов: %d\n", head.Len())
pressEnterToContinue()
fmt.Println("\ninorder traversal:")
head.BstInorderTraversal()
tarName := "name_44"
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()
}