#include <iostream> #include <string> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; void insert(TreeNode*& root, int val) { if (root == nullptr) { root = new TreeNode(val); return; } if (val < root->val) { insert(root->left, val); } else if (val > root->val) { insert(root->right, val); } } string search(TreeNode* root, int val) { string path = "*"; while (root != nullptr) { if (val < root->val) { path += "l"; root = root->left; } else if (val > root->val) { path += "r"; root = root->right; } else { return path; } } return "Nothing."; } void remove(TreeNode*& root, int val) { if (root == nullptr) { return; } if (val < root->val) { remove(root->left, val); } else if (val > root->val) { remove(root->right, val); } else { if (root->left == nullptr) { TreeNode* temp = root; root = root->right; delete temp; } else if (root->right == nullptr) { TreeNode* temp = root; root = root->left; delete temp; } else { TreeNode* minNode = root->right; while (minNode->left != nullptr) { minNode = minNode->left; } root->val = minNode->val; remove(root->right, minNode->val); } } } int main() { int Q; cin >> Q; TreeNode* root = nullptr; for (int i = 0; i < Q; i++) { char op; int x; cin >> op >> x; if (op == '+') { insert(root, x); } else if (op == '-') { remove(root, x); } else if (op == '*') { string result = search(root, x); cout << result << endl; } } return 0; }
时间: 2024-02-14 15:09:37 浏览: 127
这段代码是一段二叉搜索树的实现,可以进行插入、删除、查找操作。其中,insert函数实现了插入操作,search函数实现了查找操作,remove函数实现了删除操作。在主函数中,根据输入的操作符进行相应的操作。其中,'+'表示插入,'-'表示删除,'*'表示查找,并输入对应的值。程序输出查找操作的结果。
相关问题
#include <iostream> #include <queue> #include<string> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; // 递归实现先序遍历 void preorderTraversal(TreeNode* root) { if (root) { cout << root->val << " "; preorderTraversal(root->left); preorderTraversal(root->right); } } // 非递归实现层次遍历 void levelOrderTraversal(TreeNode* root) { if (!root) { return; } queue<TreeNode*> q; q.push(root); while (!q.empty()) { TreeNode* cur = q.front(); q.pop(); cout << cur->val << " "; if (cur->left) { q.push(cur->left); } if (cur->right) { q.push(cur->right); } } } int main() { // 建立二叉树 TreeNode* root = new TreeNode(65); root->left = new TreeNode(66); root->right = new TreeNode(67); root->left->left = new TreeNode(68); root->left->right = new TreeNode(69); root->left->left->left = new TreeNode(72); root->left->right->right = new TreeNode(73); root->right->left = new TreeNode(70); root->right->right = new TreeNode(71); root->right->left->left = new TreeNode(74); // 先序遍历 cout << "Preorder Traversal: "; preorderTraversal(root); cout << endl; // 层次遍历 cout << "Level Order Traversal: "; levelOrderTraversal(root); cout << endl; return 0; }怎么将代码内节点的数字改成字母
要将节点的数字改成字母,可以将 TreeNode 结构体中的 val 成员变量改为 string 类型,并将节点的值赋为相应的字母。例如,将节点的数字 65 改为字母 A,将节点的数字 66 改为字母 B,以此类推,可以将代码修改如下:
```
#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct TreeNode {
string val; // 将节点的值从 int 类型改为 string 类型
TreeNode* left;
TreeNode* right;
TreeNode(string s) : val(s), left(NULL), right(NULL) {} // 将节点的值初始化为字符串类型
};
// 递归实现先序遍历
void preorderTraversal(TreeNode* root) {
if (root) {
cout << root->val << " ";
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
// 非递归实现层次遍历
void levelOrderTraversal(TreeNode* root) {
if (!root) {
return;
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* cur = q.front();
q.pop();
cout << cur->val << " ";
if (cur->left) {
q.push(cur->left);
}
if (cur->right) {
q.push(cur->right);
}
}
}
int main() {
// 建立二叉树
TreeNode* root = new TreeNode("A"); // 将节点的值从数字改为字母
root->left = new TreeNode("B");
root->right = new TreeNode("C");
root->left->left = new TreeNode("D");
root->left->right = new TreeNode("E");
root->left->left->left = new TreeNode("H");
root->left->right->right = new TreeNode("I");
root->right->left = new TreeNode("F");
root->right->right = new TreeNode("G");
root->right->left->left = new TreeNode("J");
// 先序遍历
cout << "Preorder Traversal: ";
preorderTraversal(root);
cout << endl;
// 层次遍历
cout << "Level Order Traversal: ";
levelOrderTraversal(root);
cout << endl;
return 0;
}
```
这样就可以将节点的数字改成相应的字母了。
#include<iostream>using namespace std;struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int v): val(v), left(NULL), right(NULL) {}};TreeNode* buildTree(){ int val; cin >> val; if(val == -1) // 空节点 return NULL; TreeNode* root = new TreeNode(val); root->left = buildTree(); root->right = buildTree(); return root;}int main(){ cout << "请输入二叉树的先序遍历序列(空节点用-1表示):" << endl; TreeNode* root = buildTree(); return 0;}用层次遍历该二叉树
可以使用队列来实现二叉树的层次遍历。具体步骤如下:
1. 将根节点放入队列中。
2. 当队列不为空时,取出队首节点,输出其值,并将其左右子节点(如果存在)放入队列中。
3. 重复第2步,直到队列为空。
下面是代码实现:
```c++
#include<iostream>
#include<queue>
using namespace std;
struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int v): val(v), left(NULL), right(NULL) {}
};
TreeNode* buildTree(){
int val;
cin >> val;
if(val == -1) // 空节点
return NULL;
TreeNode* root = new TreeNode(val);
root->left = buildTree();
root->right = buildTree();
return root;
}
void levelOrder(TreeNode* root){
if(root == NULL)
return;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
TreeNode* cur = q.front();
q.pop();
cout << cur->val << " ";
if(cur->left)
q.push(cur->left);
if(cur->right)
q.push(cur->right);
}
}
int main(){
cout << "请输入二叉树的先序遍历序列(空节点用-1表示):" << endl;
TreeNode* root = buildTree();
cout << "层次遍历结果为:" << endl;
levelOrder(root);
return 0;
}
```
如果输入的二叉树是这样的:
```
1
2
-1
-1
3
4
-1
-1
5
-1
-1
```
则输出的层次遍历结果为:
```
1 2 3 4 5
```
阅读全文