vault backup: 2024-12-01 23:50:59

This commit is contained in:
boris
2024-12-01 23:50:59 +00:00
parent a98ccc88d0
commit 13615cfb79
44 changed files with 1282401 additions and 53 deletions

View File

@@ -0,0 +1,76 @@
# What is a binary search tree, and how does it differ from a regular binary tree?
### **Binary Search Tree (BST):**
A **Binary Search Tree (BST)** is a type of binary tree where nodes are organized based on their key values to maintain a specific order. It adheres to the following properties:
1. **Left Subtree Rule**: For any node, all values in its left subtree are less than the value of the node.
2. **Right Subtree Rule**: For any node, all values in its right subtree are greater than the value of the node.
3. **Uniqueness**: Typically, BSTs do not allow duplicate values (though this can vary depending on implementation).
These properties ensure that a **BST is sorted**, which allows for efficient searching, insertion, and deletion operations.
---
### **Binary Tree (General Definition):**
A **Binary Tree** is a tree data structure where each node has at most two children. It does not impose any ordering constraints on the values of the nodes.
- **Structure**: Each node can have a **left child**, a **right child**, or both (or no children).
- **No Value Order**: The values in a binary tree are not arranged in any specific order.
---
### **Key Differences Between Binary Tree and Binary Search Tree:**
|**Aspect**|**Binary Tree**|**Binary Search Tree**|
|---|---|---|
|**Node Value Order**|No specific order for node values.|Nodes are organized by the BST property (left < root < right).|
|**Searching Efficiency**|Searching is linear in worst case (O(n)).|Searching is efficient (O(log n)) if balanced.|
|**Insertion Rule**|Nodes can be added in any order.|Nodes are inserted based on their value to maintain the BST property.|
|**Duplicates**|Allows duplicates freely.|May or may not allow duplicates, depending on implementation.|
|**Applications**|Used for general tree structures.|Used for sorted collections or when efficient searching is required.|
---
### **Example:**
#### Binary Tree:
A binary tree with no ordering constraints:
```
10
/ \
20 30
/
40
```
- Node values are not in any specific order.
#### Binary Search Tree:
A binary search tree:
```
10
/ \
5 20
/ \ / \
2 7 15 25
```
- For every node:
- Left subtree values < Node value.
- Right subtree values > Node value.
---
### **Advantages of BST Over a General Binary Tree:**
1. **Efficient Searching**: Searching in a BST takes $O(h)$, where $h$ is the height of the tree. In a balanced BST, $h=O(logn)$.
2. **Ordered Traversal**: In-order traversal of a BST produces the nodes in sorted order.
3. **Efficient Range Queries**: BSTs can efficiently find all elements within a specific range.
### **Disadvantages of BST:**
1. If unbalanced (e.g., when nodes are inserted in sorted order), the BST can degrade to a linked list, resulting in $O(n)$ time for operations.
2. Maintaining balance (e.g., in AVL or Red-Black Trees) adds complexity.