中文
二叉搜索树(Binary Search Tree,BST) 是一种特殊的二叉树,它满足以下性质:
- 对于任意一个节点,其左子树上所有节点的值都小于该节点的值。
- 对于任意一个节点,其右子树上所有节点的值都大于该节点的值。
- 左右子树本身也是二叉搜索树。
时间复杂度
- 查找:在理想情况下(树是平衡的),查找的时间复杂度为 O ( log n ) O(\log n) O(logn),其中 n n n 是树中节点的数量。最坏情况下(树退化为链表),查找的时间复杂度为 O ( n ) O(n) O(n)。
- 插入:插入操作的时间复杂度与查找操作相同,为 O ( log n ) O(\log n) O(logn) 或 O ( n ) O(n) O(n)。
- 删除:删除操作的时间复杂度也与查找操作相同,为 O ( log n ) O(\log n) O(logn) 或 O ( n ) O(n) O(n)。
English Version
A Binary Search Tree (BST) is a special kind of binary tree that satisfies the following properties:
- For any given node, all nodes in its left subtree have values less than the node’s value.
- For any given node, all nodes in its right subtree have values greater than the node’s value.
- Both the left and right subtrees are also binary search trees.
Time Complexity:
- Search: In the best case (when the tree is balanced), the time complexity for searching is O ( log n ) O(\log n) O(logn), where n n n is the number of nodes in the tree. In the worst case (when the tree degenerates into a linked list), the time complexity is O ( n ) O(n) O(n).
- Insertion: The time complexity for insertion is the same as for searching, O ( log n ) O(\log n) O(logn) or O ( n ) O(n) O(n).
- Deletion: The time complexity for deletion is also the same as for searching, O ( log n ) O(\log n) O(logn) or O ( n ) O(n) O(n).