二叉排序树的查找、插入算法。(算法设计题)
时间: 2023-05-02 18:00:53 浏览: 75
题目:二叉排序树的查找、插入算法。(算法设计题)
二叉排序树是一种特殊的二叉树,它满足左子树所有节点的键值小于根节点的键值,右子树所有节点的键值大于根节点的键值。二叉排序树可以高效地支持查找、插入、删除等操作。
查找算法:从根节点开始,若当前节点的键值等于目标值,则返回该节点;若当前节点的键值大于目标值,则在左子树中继续查找;若当前节点的键值小于目标值,则在右子树中继续查找。若遍历到叶子节点还未找到,则说明目标值不存在于树中。
插入算法:从根节点开始,若当前节点为空,则将目标值插入该节点;若当前节点的键值等于目标值,则插入失败(节点已存在);若当前节点的键值大于目标值,则在左子树中继续插入;若当前节点的键值小于目标值,则在右子树中继续插入。
相关问题
实现二叉排序树查找,步骤是1、定义二叉排序树的查找算法函数。 2、二叉排序树的插入算法函数。 3、建立二叉排序树的插入算法。 4、二叉排序树删除一个结点的操作实现。 5、 打印二叉排序树。
好的,我来为您解答如何实现二叉排序树的查找。
1. 定义二叉排序树的查找算法函数
二叉排序树的查找算法函数可以使用递归或迭代方式实现,以下是一个递归实现的示例代码:
```python
def search_bst(root, val):
if not root or root.val == val:
return root
elif root.val > val:
return search_bst(root.left, val)
else:
return search_bst(root.right, val)
```
2. 二叉排序树的插入算法函数
二叉排序树的插入算法函数也可以使用递归或迭代方式实现,以下是一个递归实现的示例代码:
```python
def insert_bst(root, val):
if not root:
return TreeNode(val)
elif root.val > val:
root.left = insert_bst(root.left, val)
else:
root.right = insert_bst(root.right, val)
return root
```
3. 建立二叉排序树的插入算法
建立二叉排序树的插入算法可以使用一个循环来不断插入新的节点,以下是一个示例代码:
```python
def build_bst(nums):
if not nums:
return None
root = TreeNode(nums[0])
for i in range(1, len(nums)):
insert_bst(root, nums[i])
return root
```
4. 二叉排序树删除一个结点的操作实现
二叉排序树删除一个结点的操作需要考虑多种情况,包括删除的节点有无子节点、删除的节点只有一个子节点、删除的节点有两个子节点等,以下是一个示例代码:
```python
def delete_bst(root, val):
if not root:
return None
if root.val == val:
if not root.left:
return root.right
if not root.right:
return root.left
if root.left and root.right:
temp = root.right
while temp.left:
temp = temp.left
root.val = temp.val
root.right = delete_bst(root.right, temp.val)
elif root.val > val:
root.left = delete_bst(root.left, val)
else:
root.right = delete_bst(root.right, val)
return root
```
5. 打印二叉排序树
二叉排序树的打印可以使用中序遍历来实现,以下是一个示例代码:
```python
def inorder_traversal(root):
if not root:
return []
res = []
stack = []
while root or stack:
while root:
stack.append(root)
root = root.left
root = stack.pop()
res.append(root.val)
root = root.right
return res
```
可以使用以上函数来打印二叉排序树的所有节点值。
使用C语言写一个关于二叉树的算法,包含定义二叉排序树的查找算法函数、二叉排序树的插入算法函数、建立二叉排序树的插入算法、二叉排序树删除一个结点的操作实现、打印二叉排序树
好的,这是一个二叉排序树的完整实现,包括定义二叉排序树的查找算法函数、二叉排序树的插入算法函数、建立二叉排序树的插入算法、二叉排序树删除一个结点的操作实现、打印二叉排序树:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义二叉排序树结点的结构体
struct node {
int data;
struct node *left_child;
struct node *right_child;
};
// 查找函数
struct node* search(struct node *root, int data) {
// 如果查找的结点为空,返回 NULL
if (root == NULL) {
return NULL;
}
// 如果查找的值等于根结点的值,返回根结点
if (data == root->data) {
return root;
}
// 如果查找的值小于根结点的值,在左子树中查找
if (data < root->data) {
return search(root->left_child, data);
}
// 如果查找的值大于根结点的值,在右子树中查找
else {
return search(root->right_child, data);
}
}
// 插入函数
struct node* insert(struct node *root, int data) {
// 如果根结点为空,创建一个新结点并返回
if (root == NULL) {
struct node *new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = data;
new_node->left_child = NULL;
new_node->right_child = NULL;
return new_node;
}
// 如果插入的值比根结点的值小,在左子树中插入
if (data < root->data) {
root->left_child = insert(root->left_child, data);
}
// 如果插入的值比根结点的值大,在右子树中插入
else if (data > root->data) {
root->right_child = insert(root->right_child, data);
}
// 如果插入的值等于根结点的值,返回根结点
else {
return root;
}
return root;
}
// 建立二叉排序树的插入算法
struct node* build_binary_search_tree(int data[], int n) {
struct node *root = NULL;
int i;
for (i = 0; i < n; i++) {
root = insert(root, data[i]);
}
return root;
}
// 删除函数
struct node* delete(struct node *root, int data) {
struct node *temp;
if (root == NULL) {
printf("Element not found\n");
}
else if (data < root->data) {
root->left_child = delete(root->left_child, data);
}
else if (data > root->data) {
root->right_child = delete(root->right_child, data);
}
else {
if (root->left_child && root->right_child) {
temp = find_minimum(root->right_child);
root->data = temp->data;
root->right_child = delete(root->right_child, root->data);
}
else {
temp = root;
if (root->left_child == NULL) {
root = root->right_child;
}
else if (root->right_child == NULL) {
root = root->left_child;
}
free(temp);
}
}
return root;
}
// 找到子树中的最小值结点
struct node* find_minimum(struct node *root) {
if (root == NULL) {
return NULL;
}
else if (root->left_child == NULL) {
return root;
}
else {
return find_minimum(root->left_child);
}
}
// 打印二叉排序树
void print_binary_search_tree(struct node *root) {
if (root != NULL) {
print_binary_search_tree(root->left_child);
printf("%d ", root->data);
print_binary_search_tree(root->right_child);
}
}
// 测试插入、查找、删除、打印函数
int main() {
int data[] = { 5, 3, 7, 1, 4, 6, 8 };
struct node *root = build_binary_search_tree(data, 7);
printf("Binary search tree: ");
print_binary_search_tree(root);
printf("\n");
printf("Search for 6: %d\n", search(root, 6)->data);
root = delete(root, 5);
printf("Binary search tree after deleting 5: ");
print_binary_search_tree(root);
printf("\n");
return 0;
}
```
以上代码中,我们定义了一个二叉排序树的结点结构体,包含了结点的值以及左右子结点的指针。然后定义了查找函数、插入函数、建立二叉排序树的插入算法、删除函数、找到子树中的最小值结点函数和打印二叉排序树函数。最后我们测试了插入、查找、删除、打印函数,可以看到二叉排序树的操作按照预期执行,且结果正确。