c语言_二叉排序树增删

一、tree.h

#pragma once
#include<stdio.h>
#include<stdlib.h>

typedef struct binary_sort_tree {
	int* data;
	void* left;
	void* right;
	char ptr;
}bst;

#define BST_SIZE sizeof(bst)
bst* tree_root;//这里如果赋初始值,就是定义了,声明可以多次,但是定义只能一次。两个.c文件重复引入头文件,就会有两次定义,会出错
//int bst_num;
void bst_insertNode(bst* node, int sdata);
void bst_free_space(bst* node);
void bst_print_tree(bst* node);
void init_bst_tree();
int bst_deleteNode(bst* node, int sdata);
int bst_delete(bst* node);

二、binary_sort_tree.c

#define _CRT_SECURE_NO_WARNINGS
#include "tree.h"

int bst_num = 0;
//初始化二叉排序树
void init_bst_tree() {
	int temp = 0;
	printf("请输入节点的个数:\n");
	scanf("%d", &bst_num);
	printf("请依次输入节点的值:\n");
	tree_root = (bst*)malloc(BST_SIZE);
	tree_root->data = NULL;
	tree_root->left = NULL;
	tree_root->right = NULL;
	tree_root->ptr = 'p';
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言实现二排序的主要步骤如下: 1. 定义二排序的节点结构体,包括左右子指针和数据域。 2. 实现节点的创建函数,用于新建一个节点并初始化其值。 3. 实现插入函数,将一个元素插入到二排序中。 4. 实现查找函数,查找指定元素是否在二排序中。 5. 实现删除函数,删除指定元素在二排序中的节点。 6. 实现遍历函数,可以按照前序遍历、中序遍历、后序遍历的方式输出二排序中所有节点的值。 下面是一个简单的C语言实现二排序的示例代码: ``` #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 创建一个新节点 TreeNode* createNode(int val) { TreeNode* node = (TreeNode*) malloc(sizeof(TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node; } // 向二排序中插入一个节点 void insert(TreeNode** root, int val) { if (*root == NULL) { *root = createNode(val); return; } if (val < (*root)->val) { insert(&((*root)->left), val); } else { insert(&((*root)->right), val); } } // 查找指定元素是否在二排序中 TreeNode* search(TreeNode* root, int val) { if (root == NULL || root->val == val) { return root; } if (val < root->val) { return search(root->left, val); } else { return search(root->right, val); } } // 删除指定元素在二排序中的节点 TreeNode* delete(TreeNode* root, int val) { if (root == NULL) { return root; } if (val < root->val) { root->left = delete(root->left, val); } else if (val > root->val) { root->right = delete(root->right, val); } else { if (root->left == NULL) { TreeNode* temp = root->right; free(root); return temp; } else if (root->right == NULL) { TreeNode* temp = root->left; free(root); return temp; } TreeNode* temp = root->right; while (temp && temp->left != NULL) { temp = temp->left; } root->val = temp->val; root->right = delete(root->right, temp->val); } return root; } // 前序遍历二排序 void preOrder(TreeNode* root) { if (root == NULL) { return; } printf("%d ", root->val); preOrder(root->left); preOrder(root->right); } // 中序遍历二排序 void inOrder(TreeNode* root) { if (root == NULL) { return; } inOrder(root->left); printf("%d ", root->val); inOrder(root->right); } // 后序遍历二排序 void postOrder(TreeNode* root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); printf("%d ", root->val); } int main() { TreeNode* root = NULL; // 插入元素 insert(&root, 5); insert(&root, 3); insert(&root, 7); insert(&root, 1); insert(&root, 4); // 查找元素 TreeNode* node = search(root, 7); if (node != NULL) { printf("Found element: %d\n", node->val); } else { printf("Element not found\n"); } // 删除元素 root = delete(root, 3); // 遍历二排序 printf("Pre-order traversal: "); preOrder(root); printf("\n"); printf("In-order traversal: "); inOrder(root); printf("\n"); printf("Post-order traversal: "); postOrder(root); printf("\n"); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值