Sobes.tech
Middle

How long does a search tree operate?

sobes.tech AI

Answer from AI

The search tree's runtime depends on the type of tree and operation. For balanced binary search trees (e.g., AVL, red-black tree), search, insertion, and deletion operate in O(log n), where n is the number of elements. In the worst case, for an unbalanced binary tree, search can degrade to O(n). For example, in Go, you can implement binary search on a sorted slice in O(log n):

func binarySearch(arr []int, target int) int {
    left, right := 0, len(arr)-1
    for left <= right {
        mid := left + (right-left)/2
        if arr[mid] == target {
            return mid
        } else if arr[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1 // not found
}
How long does a search tree operate? — Golang - sobes.tech