【数据结构】二叉排序树的插入、查找

 查找算法

 插入算法

代码: 

#include<iostream>
using namespace std;
#define ENDFLAG 2022
typedef int KeyType;
typedef char InfoType;

typedef struct{
	KeyType key;
	InfoType otherinfo;
}ElemType;
typedef struct BSTNode{
	ElemType data;
	struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;

//二叉树插入
void Insert_BSTree(BSTree &T,ElemType e){
	if(!T){   //树为空,插入根结点 
		T=new BSTNode;
		T->data.key=e.key;
		T->lchild=NULL;
		T->rchild=NULL;
	}
	else{
		if(e.key<T->data.key) Insert_BSTree(T->lchild,e);    //要插入的元素比根结点值小,插入在左子树 
		else Insert_BSTree(T->rchild,e);   //要插入的元素比根结点值大,插入在右子树
	}
} 
//二叉树的创建
void Create_BSTree(BSTree &T){
	T=NULL;
	ElemType e;
	cin>>e.key;
	while(e.key!=ENDFLAG){
		Insert_BSTree(T,e);   //循环插入 
		cin>>e.key;
	} 
} 
//中序遍历
void InOrderTraverse(BSTree T){
	if(T){
		InOrderTraverse(T->lchild);
		cout<<T->data.key<<' ';
		InOrderTraverse(T->rchild);
	}
} 

//查找
BSTree SearchBST(BSTree T,KeyType key){
	if((!T)||T->data.key==key) return T;   //树空(未找到) 或 已找到 
	else if(key<T->data.key) SearchBST(T->lchild,key);
	else SearchBST(T->rchild,key);
} 

int main(){
	BSTree T;
	cout<<"请输入数据:";
	Create_BSTree(T);
	cout<<"中序遍历结果为:";
	InOrderTraverse(T);
	cout<<endl<<"请输入要查找关键字:";
	KeyType key;
	cin>>key;
	BSTree result=SearchBST(T,key);
	if(result){
		cout<<"找到";
	}
	else cout<<"未找到";
	
	return 1;
}

运行结果: 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是二叉排序树的基本操作代码: ```c++ #include<iostream> using namespace std; // 二叉搜索结点结构体 struct BSTNode { int data; BSTNode *left; BSTNode *right; }; // 查找操作 BSTNode* search(BSTNode *root, int x) { if (root == nullptr || root->data == x) { return root; } else if (x < root->data) { return search(root->left, x); } else { return search(root->right, x); } } // 插入操作 BSTNode* insert(BSTNode *root, int x) { if (root == nullptr) { root = new BSTNode; root->data = x; root->left = nullptr; root->right = nullptr; } else if (x < root->data) { root->left = insert(root->left, x); } else { root->right = insert(root->right, x); } return root; } // 查找最小结点 BSTNode* findMin(BSTNode *root) { if (root == nullptr) { return nullptr; } else if (root->left == nullptr) { return root; } else { return findMin(root->left); } } // 删除操作 BSTNode* remove(BSTNode *root, int x) { if (root == nullptr) { return nullptr; } if (x < root->data) { root->left = remove(root->left, x); } else if (x > root->data) { root->right = remove(root->right, x); } else if (root->left != nullptr && root->right != nullptr) { root->data = findMin(root->right)->data; root->right = remove(root->right, root->data); } else { BSTNode *temp = root; if (root->left == nullptr) { root = root->right; } else if (root->right == nullptr) { root = root->left; } delete temp; } return root; } // 中序遍历 void inorder(BSTNode *root) { if (root != nullptr) { inorder(root->left); cout << root->data << " "; inorder(root->right); } } int main() { BSTNode *root = nullptr; root = insert(root, 8); root = insert(root, 3); root = insert(root, 10); root = insert(root, 1); root = insert(root, 6); root = insert(root, 14); root = insert(root, 4); root = insert(root, 7); root = insert(root, 13); inorder(root); // 输出结果为:1 3 4 6 7 8 10 13 14 root = remove(root, 6); inorder(root); // 输出结果为:1 3 4 7 8 10 13 14 return 0; } ``` 以上代码中,`BSTNode` 是二叉搜索结点的结构体,包含数据 `data` 和左右子指针 `left` 和 `right`。`search` 函数实现查找操作,`insert` 函数实现插入操作,`findMin` 函数查找最小结点,`remove` 函数实现删除操作,`inorder` 函数实现中序遍历。最后在 `main` 函数中构建二叉搜索,并进行插入、删除、中序遍历等操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值