Organizing all the code blocks.

This commit is contained in:
Yudong Jin
2022-12-03 01:31:29 +08:00
parent fec56afd5f
commit 9bd5980a81
21 changed files with 2520 additions and 310 deletions

View File

@@ -91,6 +91,30 @@ comments: true
```
=== "JavaScript"
```js title="binary_search_tree.js"
```
=== "TypeScript"
```typescript title="binary_search_tree.ts"
```
=== "C"
```c title="binary_search_tree.c"
```
=== "C#"
```csharp title="binary_search_tree.cs"
```
### 插入结点
给定一个待插入元素 `num` ,为了保持二叉搜索树 “左子树 < 根结点 < 右子树” 的性质,插入操作分为两步:
@@ -166,6 +190,30 @@ comments: true
```
=== "JavaScript"
```js title="binary_search_tree.js"
```
=== "TypeScript"
```typescript title="binary_search_tree.ts"
```
=== "C"
```c title="binary_search_tree.c"
```
=== "C#"
```csharp title="binary_search_tree.cs"
```
为了插入结点,需要借助 **辅助结点 `prev`** 保存上一轮循环的结点,这样在遍历到 $\text{null}$ 时,我们也可以获取到其父结点,从而完成结点插入操作。
与查找结点相同,插入结点使用 $O(\log n)$ 时间。
@@ -320,6 +368,30 @@ comments: true
```
=== "JavaScript"
```js title="binary_search_tree.js"
```
=== "TypeScript"
```typescript title="binary_search_tree.ts"
```
=== "C"
```c title="binary_search_tree.c"
```
=== "C#"
```csharp title="binary_search_tree.cs"
```
## 二叉搜索树的优势
假设给定 $n$ 个数字,最常用的存储方式是「数组」,那么对于这串乱序的数字,常见操作的效率为: