Bst的基本操作

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <queue>
#include <stack>
using namespace std;
typedef struct BstNode
{
	int data;
	BstNode *lchild, *rchild;
}BstNode,*BstTree;

void Bst_insert(BstTree &T,int z)//建树
{
	if(T==NULL)
	{
		T=(BstTree)malloc(sizeof(BstNode));
		T->lchild=NULL;
		T->rchild=NULL;
		T->data=z;
	}
	else
	{
		if(T->data!=z)
			if(T->data>z)
				Bst_insert(T->lchild,z);
			else
				Bst_insert(T->rchild,z);
	}
}
void Bst_preorder(BstTree T)  //中序递归遍历
{  
    if(T!=NULL)  
    {  
		Bst_preorder(T->lchild);
		printf("%d\n",T->data);
		Bst_preorder(T->rchild);  
    }  
}  
bool Bst_exist(BstTree T,int z)//寻找某数值是否存在
{
	if(T==NULL)
		return false;
	if(T->data==z)
		return true;
	if(T->data>z)
		Bst_exist(T->lchild,z);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言BST树的基本操作代码: ```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; } //向二叉搜索树中插入一个节点 TreeNode* insertNode(TreeNode* root, int val) { if (root == NULL) { return createNode(val); } if (val < root->val) { root->left = insertNode(root->left, val); } else if (val > root->val) { root->right = insertNode(root->right, val); } return root; } //在二叉搜索树中查找一个节点 TreeNode* searchNode(TreeNode* root, int val) { if (root == NULL || root->val == val) { return root; } if (val < root->val) { return searchNode(root->left, val); } else { return searchNode(root->right, val); } } //删除二叉搜索树中的一个节点 TreeNode* deleteNode(TreeNode* root, int val) { if (root == NULL) { return root; } if (val < root->val) { root->left = deleteNode(root->left, val); } else if (val > root->val) { root->right = deleteNode(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->left != NULL) { temp = temp->left; } root->val = temp->val; root->right = deleteNode(root->right, temp->val); } return root; } //中序遍历二叉搜索树 void inorderTraversal(TreeNode* root) { if (root != NULL) { inorderTraversal(root->left); printf("%d ", root->val); inorderTraversal(root->right); } } int main() { TreeNode* root = NULL; root = insertNode(root, 5); insertNode(root, 3); insertNode(root, 7); insertNode(root, 1); insertNode(root, 9); printf("中序遍历二叉搜索树:"); inorderTraversal(root); printf("\n"); root = deleteNode(root, 5); printf("删除节点5后的中序遍历二叉搜索树:"); inorderTraversal(root); printf("\n"); return 0; } ``` 以上代码实现了二叉搜索树的基本操作,包括创建节点、插入节点、查找节点、删除节点和中序遍历等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值