BST的查找

本文介绍了作者使用迭代和递归方法在二叉查找树(BST)中搜索特定元素的过程,以及相关的树结构操作如深度计算、中序遍历和验证BST性质。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

BST(Binary Search Tree)二叉查找树也太简单了吧,今天做的这个有点简单哈哈哈哈。

我的思路

迭代

迭代就是我的主要工作在于更新变量上了,如果我想要找的值比根节点小,我就用根节点的左孩子去替代当前的值。用while循环去控制不为空的条件。

node* search_BST(node* root,char str) {
	while (root != nullptr) {
		if (root->info == str) {
			return root;
		}
		else if(root->info <str) {
			root = root->right;
		}
		else if (root->info > str) {
			root = root->left;
		}
	}
	cout << "此BST中没有这个元素!" << endl;
	return NULL;
}

递归

递归就是我直接调用自己去找,把我的子树当参数去传递给这个函数。我还是喜欢迭代一点。

	node* search_BST_recursion(node* root, char str) {
		if (root == nullptr) {
			cout << "此BST中没有这个元素!" << endl;
			return NULL;
		}
		else if (root->info == str) {
			return root;
		}
		else if(root->info < str) {
			search_BST_recursion(root->right, str);
		}
		else {
			search_BST_recursion(root->left, str);
		}
	}
};

我还考虑了一下深拷贝和浅拷贝的问题,一般来说,我们用指针去返回一个对象那基本就是深拷贝了呵呵。

结果

在这里插入图片描述
在这里插入图片描述

我的代码

#include <iostream>
#include<algorithm>
#include<cmath>
#include <queue> 
#include<climits>
using namespace std;

struct node {
	char info;
	node* left;
	node* right;
	node(char data) :info(data), left(nullptr), right(nullptr) {

	};
	node() :info(NULL), left(nullptr), right(nullptr) {

	};
};

class binaryTree {
private:
	node* root;
public:
	binaryTree() {
		root = new node(NULL);
	}

	//得到树的根结点
	node* getRoot() {
		return root;
	}
	//得到树的根结点
	void setRoot(node* newRoot) {
		 root=newRoot;
	}

	//以递归的方式构建一棵树
	void createTree(node*& t,string data,int &i) {
		char str=data[i];
		/*cin >> str;*/
		if (str == '#') {
			t = NULL;
		}
		else {
			t = new node;//为t开辟空间
			t->info = str;
			createTree(t->left,data,++i);
			createTree(t->right,data,++i);
		}
	}

	//树的深度
	int depth(node* root) {
		if (root == nullptr) {
			return 0;
		}
		int left = depth(root->left);
		int right = depth(root->right);
		return max(left, right) + 1;
	}

	//打印一棵树满二叉树,只能打印满二叉树,节点数目最好不要超过10
	void print(node*& root) {
		//存放打印的二叉树
		char str[10][100] = {};
		queue<node*> q;
		int h = depth(root);
		q.push(root);
		int index = 0;
		while (!q.empty()) {
			int size = q.size();
			//存放每一层的节点
			vector<char> list;
			for (int i = 0; i < size; i++) {
				node* temp = q.front();
				q.pop();
				list.push_back(temp->info);
				//cout << temp->info;
				if (temp->left != nullptr) {
					q.push(temp->left);
				}
				if (temp->right != nullptr) {
					q.push(temp->right);
				}
			}
			bool flag = true;
			int j = 0;
			//打印前面部分空白
			while (j <= 2 * h - 1 - index) {
				str[index][j] = ' ';
				j++;

			}
			//保持第一行居中
			if (index == 0) {
				for (int m = 0; m < h - 2; m++) {
					str[index][j++] = ' ';
				}
			}

			for (int k = 0; k < list.size(); k++) {
				//如果是一层最后一个节点
				if (k == list.size() - 1) {
					str[index][j++] = list[k];
				}
				else {
					//相邻左右子节点
					if (k % 2 == 0) {
						str[index][j++] = list[k];
						for (int l = 0; l < 3 + 2 * (h - index / 2 - 1); l++) {
							str[index][j++] = ' ';
						}
					}
					else {
						str[index][j++] = list[k];
						str[index][j++] = ' ';
					}
				}
			}

			index += 2;
			//cout << endl;
		}
		for (int i = 0; i < 10; i++) {
			if (i % 2 == 1) {
				for (int j = 0; j < 100; j++) {
					str[i][j] = ' ';
				}
			}
		}
		for (int i = 0; i < 10; i++) {
			if (i % 2 == 0) {
				for (int j = 0; j < 100; j++) {
					if (str[i][j] - '0' >= 0 && str[i][j] - '0' <= 9 && i < 2 * h - 2) {
						str[i + 1][j - 1] = '/';
						str[i + 1][j + 1] = '\\';
					}

				}
			}
		}
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 100; j++) {
				cout << str[i][j];
			}
			cout << endl;
		}
	}

	void DeepFirstSearch(node* root) {
		if (root == NULL) {
			return;
		}
		else {
			cout << root->info << ' ';
			DeepFirstSearch(root->left);
			DeepFirstSearch(root->right);
		}

	}

	void BreadthFirstSearch(node* root) {
		queue<node> myTree;
		if (root != nullptr) {
			myTree.push(*root);
		}
		while (!myTree.empty()) {
			cout << myTree.front().info << ' ';
			if (myTree.front().left != nullptr) {
				myTree.push(*(myTree.front().left));
			}
			if (myTree.front().right != nullptr) {
				myTree.push(*(myTree.front().right));
			}
			myTree.pop();
		}
	}

	//用于BFS递归的主函数
	void BFS_Recursion(node* root, int level, vector<vector<char>>& res) {
		if (root == nullptr) {
			return;
		}
		if (res.size() < level) {
			res.push_back(vector<char>());
		}
		res[level - 1].push_back(root->info);
		BFS_Recursion(root->left, level + 1, res);
		BFS_Recursion(root->right, level + 1, res);
	}

	void BreadthFirstSearch_recursion(node* root) {
		vector<vector<char>> res;
		BFS_Recursion(root, 1, res);
		for (int i = 0; i < res.size(); i++) {
			for (int j = 0; j < res[i].size(); j++) {
				cout << res[i][j] << " ";
			}
		}
	}

	//验证是否为二叉搜索树
	void isBST(node* root) {
		//先创建一个数组
		vector<char> midOrderArr;
		midOrder(root, midOrderArr);
		//输出看一下我的数组里面存的是不是中序遍历的值
		for (int i = 0; i < midOrderArr.size(); i++) {
			cout << midOrderArr[i] << ' ';
		}
		cout << endl;
		for (int i = 0; i < midOrderArr.size() - 1; i++) {
			if (midOrderArr[i] >= midOrderArr[i + 1]) {
				cout << "该二叉树 不是一颗二叉搜索树!" << endl;
				return;
			}
		}
		cout << "该二叉树 是一颗二叉搜索树!" << endl;
	}

	//二叉树的中序遍历
	void midOrder(node* root, vector<char>& Arr) {
		if (root == nullptr) {
			return;
		}
		midOrder(root->left, Arr);
		Arr.push_back(root->info);
		midOrder(root->right, Arr);
	}

	bool isBST_Recursion(node* root, long long min, long long max) {
		if (root == nullptr) {
			return true;
		}
		if (root->info <= min || root->info >= max) {
			//cout << "该二叉树不是一个二叉搜索树";
			return false;
		}
		return isBST_Recursion(root->left, min, root->info) && isBST_Recursion(root->right, root->info, max);
	}

	node* search_BST(node* root,char str) {
		while (root != nullptr) {
			if (root->info == str) {
				return root;
			}
			else if(root->info <str) {
				root = root->right;
			}
			else if (root->info > str) {
				root = root->left;
			}
		}
		cout << "此BST中没有这个元素!" << endl;
		return NULL;

	}

	node* search_BST_recursion(node* root, char str) {
		if (root == nullptr) {
			cout << "此BST中没有这个元素!" << endl;
			return NULL;
		}
		else if (root->info == str) {
			return root;
		}
		else if(root->info < str) {
			search_BST_recursion(root->right, str);
		}
		else {
			search_BST_recursion(root->left, str);
		}
	}
};

int main() {
	binaryTree T;
	node* root = T.getRoot();
	string data = "421##3##65##7##";
	string data2 = "1248##9##5##36##7##";
	char str = '2';
	int i = 0;
	T.createTree(root,data,i);
	cout << "树的深度:" << T.depth(root) << endl;
	T.print(root);
	cout << "===========查找====================" << endl;
	binaryTree resTree;
	if (T.search_BST(root, str)) {
		resTree.setRoot(T.search_BST(root, str));
		node* resTreeRoot = resTree.getRoot();
		resTree.print(resTreeRoot);
	}
	cout << "===========查找==递归==================" << endl;
	binaryTree resTree2;
	if (T.search_BST_recursion(root, str)) {
		resTree2.setRoot(T.search_BST_recursion(root, str));
		node* resTreeRoot2 = resTree2.getRoot();
		resTree.print(resTreeRoot2);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值