后序遍历构建二叉树+查找打印路径+删除

给出后序遍历序列###ca##ji####spom, 构建二叉树。
上述二叉树实际是一颗二叉排序树,请实现程序
查找c节点,输出从树根到c节点的路径。
删除其中的m节点,使得删除后仍为二叉排序树。

头文件 Binary_node.h 构建二叉树节点

#include<iostream>
struct Binary_node 
{
 char ch;
 struct Binary_node* left; //指向左孩子的指针
 struct Binary_node* right; //指向右孩子的指针
 Binary_node(); //默认的不带参数的构造函数
 Binary_node(char& x); //带有一个参数的构造函数
};
typedef struct Binary_node* ptr;

Binary_node.cpp 节点的实现

#include "Binary_node.h"
Binary_node::Binary_node() 
{
 left = NULL;
 right = NULL;
}
Binary_node::Binary_node(char& x) 
{
 ch = x;
 left = NULL;
 right = NULL;
}

Binary_tree.h 定义二叉树的类

#include "Binary_node.h"
#include<iostream>
using namespace std;
typedef struct Binary_node* ptr;
class Binary_tree {
public:
 Binary_tree();
 void postorder(void (*visit)(char&));
 void BuildTree_Postorder(ptr& aroot, string& postorder, int& index);
 ptr root;
protected:
 void recursive_postorder(ptr& sub_root, void (*visit)(char&));
 int count;
};

Binary_tree.cpp 二叉树的实现

#include "Binary_tree.h"
typedef struct Binary_node* ptr;
Binary_tree::Binary_tree()
{
 root = NULL;
 count = 0;
}
void Binary_tree::postorder(void (*visit)(char&))
{
 recursive_postorder(root, visit);
}
void Binary_tree::recursive_postorder(ptr& sub_root, void (*visit)(char&))
{
 if (sub_root != NULL) 
 {
  recursive_postorder(sub_root->left, visit);
  recursive_postorder(sub_root->right, visit);
  (*visit)(sub_root->ch);
 }
}
void Binary_tree::BuildTree_Postorder(ptr& aroot, string& postorder, int& index)
{
 if (index>=0)
 {
  if (postorder[index] == '#')
   aroot = NULL;
  else 
  {
   aroot = new Binary_node;
   aroot->ch = postorder[index];
   BuildTree_Postorder(aroot->right, postorder, --index);
   BuildTree_Postorder(aroot->left, postorder, --index);
  }
 }
}
``
继承实现二分查找树 Search_tree.h

```cpp
#include "Binary_tree.h"
enum Error_code{success,not_present};
class Search_tree: public Binary_tree
{
public:
 Error_code insert(char &new_data);
 void remove(Binary_node*& root,char &target);
 void tree_search(Binary_node*& root,char &target);
 //ptr search_root;
private: 
    Binary_node* search_for_node(Binary_node*& sub_root, char& target); 
    Error_code remove_root(Binary_node*& sub_root);
    Error_code search_and_destroy(Binary_node*& sub_root,char& target);
};

Search_tree.cpp 二分查找树的实现

#include"Search_tree.h"
void Search_tree::tree_search(Binary_node*& root,char &target)
{
	Binary_node* found = search_for_node(root, target);
	if (found == NULL)
		cout<<"false";
	else
		cout<<"true";
}
Binary_node* Search_tree::search_for_node(Binary_node*& sub_root,char &target) 
{
	if (sub_root == NULL||sub_root->ch == target)
	{
		cout<<sub_root->ch;
		return sub_root;
	}
	else if (sub_root->ch < target)
	{
		cout<<sub_root->ch;
		return search_for_node(sub_root->right, target);
	}
	else 
	{
		cout<<sub_root->ch;
		return search_for_node(sub_root->left, target);
	}
}

void Search_tree::remove(Binary_node*& root,char& target)
{
	Error_code result=search_and_destroy(root,target);
	if(result==success)
	count--;
	else
	cout<<"false";
}
Error_code Search_tree::search_and_destroy(Binary_node*& sub_root,char &target)
{
	if (sub_root == NULL || sub_root->ch == target)
		return remove_root(sub_root);
	else if (target < sub_root->ch)
		return search_and_destroy(sub_root->left, target);
	else
		return search_and_destroy(sub_root->right, target);
}
Error_code Search_tree::remove_root(Binary_node*& sub_root)
{
	//删除左边子树最右边的孩子 
	//没有子树 
	if (sub_root == NULL) 
		return not_present;
	Binary_node* to_delete = sub_root;
	//只有一棵子树为空 
    if (sub_root->right == NULL) 
		sub_root = sub_root->left;
	else if (sub_root->left == NULL) 
		sub_root = sub_root->right;
	else 
	{ 
		//两棵子树都不是空的
		to_delete = sub_root->left;
		Binary_node* parent = sub_root;
		while (to_delete->right != NULL) { 
			parent = to_delete;
			to_delete = to_delete->right;
		} 
		sub_root->ch = to_delete->ch;
		if (parent == sub_root) 
			sub_root->left = to_delete->left;  //左孩子无右孩子
		else 
			parent->right = to_delete->left;  //左孩子有右孩子
	} 
	delete to_delete;
	return success;
}

主函数的实现 main

#include"Search_tree.h"
#include<iostream>
#include<string>
using namespace std;
void print(char& m)
{
	cout<<m;
}
int main()
{
	Binary_tree bt;
	string S;
	cin >> S;
	int length = S.size() - 1;
	bt.BuildTree_Postorder(bt.root,S,length);
	bt.postorder(print);
	cout<<endl;
	Search_tree st;
	char al='c';
	st.tree_search(bt.root,al);
	cout<<endl;
	al='m';
	st.remove(bt.root,al);
	bt.postorder(print);
	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是实现代码: ```c++ #include <iostream> #include <queue> using namespace std; struct TreeNode { int val; char letter; TreeNode* left; TreeNode* right; TreeNode(int x, char c) : val(x), letter(c), left(NULL), right(NULL) {} }; // 层手动输入构建叉树 TreeNode* buildTree() { int val; char letter; queue<TreeNode*> q; cout << "请输入根节点的值和字母:"; cin >> val >> letter; TreeNode* root = new TreeNode(val, letter); q.push(root); while (!q.empty()) { TreeNode* cur = q.front(); q.pop(); int leftVal, rightVal; char leftLetter, rightLetter; cout << "请输入节点" << cur->val << "的左右子节点的值和字母(如果没有子节点请输入0):"; cin >> leftVal >> leftLetter >> rightVal >> rightLetter; if (leftVal) { TreeNode* left = new TreeNode(leftVal, leftLetter); cur->left = left; q.push(left); } if (rightVal) { TreeNode* right = new TreeNode(rightVal, rightLetter); cur->right = right; q.push(right); } } return root; } // 层输出二叉树 void levelOrder(TreeNode* root) { if (!root) return; queue<TreeNode*> q; q.push(root); while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) { TreeNode* cur = q.front(); q.pop(); cout << cur->val << "(" << cur->letter << ") "; if (cur->left) q.push(cur->left); if (cur->right) q.push(cur->right); } cout << endl; } } // 前搜索节点并输出路径 bool findNodePreorder(TreeNode* root, int val, string& path) { if (!root) return false; if (root->val == val) { path += to_string(val); return true; } if (findNodePreorder(root->left, val, path)) { path = to_string(root->val) + " -> " + path; return true; } if (findNodePreorder(root->right, val, path)) { path = to_string(root->val) + " -> " + path; return true; } return false; } // 中搜索节点并输出路径 bool findNodeInorder(TreeNode* root, int val, string& path) { if (!root) return false; if (findNodeInorder(root->left, val, path)) { path = path.empty() ? to_string(root->val) : path + " -> " + to_string(root->val); return true; } if (root->val == val) { path = path.empty() ? to_string(val) : path + " -> " + to_string(val); return true; } if (findNodeInorder(root->right, val, path)) { path = path.empty() ? to_string(root->val) : path + " -> " + to_string(root->val); return true; } return false; } // 后搜索节点并输出路径 bool findNodePostorder(TreeNode* root, int val, string& path) { if (!root) return false; if (findNodePostorder(root->left, val, path)) { path = path.empty() ? to_string(root->val) : path + " -> " + to_string(root->val); return true; } if (findNodePostorder(root->right, val, path)) { path = path.empty() ? to_string(root->val) : path + " -> " + to_string(root->val); return true; } if (root->val == val) { path = path.empty() ? to_string(val) : path + " -> " + to_string(val); return true; } return false; } // 层搜索节点并输出路径 bool findNodeLevelorder(TreeNode* root, int val, string& path) { if (!root) return false; queue<pair<TreeNode*, string>> q; q.push(make_pair(root, to_string(root->val))); while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) { pair<TreeNode*, string> cur = q.front(); q.pop(); if (cur.first->val == val) { path = cur.second; return true; } if (cur.first->left) q.push(make_pair(cur.first->left, cur.second + " -> " + to_string(cur.first->left->val))); if (cur.first->right) q.push(make_pair(cur.first->right, cur.second + " -> " + to_string(cur.first->right->val))); } } return false; } int main() { TreeNode* root = buildTree(); cout << "层输出二叉树:" << endl; levelOrder(root); while (true) { int val; cout << "请输入要查找的节点的值(输入-1退出程):"; cin >> val; if (val == -1) break; string path = ""; if (findNodePreorder(root, val, path)) { cout << "前路径为:" << path << endl; } else { cout << "前中不存在该节点" << endl; } path = ""; if (findNodeInorder(root, val, path)) { cout << "中路径为:" << path << endl; } else { cout << "中中不存在该节点" << endl; } path = ""; if (findNodePostorder(root, val, path)) { cout << "后路径为:" << path << endl; } else { cout << "后中不存在该节点" << endl; } path = ""; if (findNodeLevelorder(root, val, path)) { cout << "层路径为:" << path << endl; } else { cout << "层中不存在该节点" << endl; } } return 0; } ``` 示例输入: ``` 请输入根节点的值和字母:1 A 请输入节点1的左右子节点的值和字母(如果没有子节点请输入0):2 B 3 C 请输入节点2的左右子节点的值和字母(如果没有子节点请输入0):4 D 5 E 请输入节点3的左右子节点的值和字母(如果没有子节点请输入0):6 F 0 请输入节点4的左右子节点的值和字母(如果没有子节点请输入0):0 请输入节点5的左右子节点的值和字母(如果没有子节点请输入0):7 G 8 H 请输入节点6的左右子节点的值和字母(如果没有子节点请输入0):0 请输入节点7的左右子节点的值和字母(如果没有子节点请输入0):0 请输入节点8的左右子节点的值和字母(如果没有子节点请输入0):0 请输入要查找的节点的值(输入-1退出程):4 前路径为:1 -> 2 -> 4 中路径为:4 -> 2 -> 1 后路径为:4 -> 2 -> 1 层路径为:1 -> 2 -> 3 -> 4 请输入要查找的节点的值(输入-1退出程):7 前中不存在该节点 中路径为:7 -> 5 -> 1 -> 3 后路径为:7 -> 5 -> 2 -> 8 -> 4 -> 6 -> 3 -> 1 层路径为:1 -> 3 -> 2 -> 6 -> 5 -> 4 -> 8 -> 7 请输入要查找的节点的值(输入-1退出程):-1 ``` 示例输出: ``` 层输出二叉树: 1(A) 2(B) 3(C) 4(D) 5(E) 6(F) 7(G) 8(H) 请输入要查找的节点的值(输入-1退出程):4 前路径为:1 -> 2 -> 4 中路径为:4 -> 2 -> 1 后路径为:4 -> 2 -> 1 层路径为:1 -> 2 -> 3 -> 4 请输入要查找的节点的值(输入-1退出程):7 前中不存在该节点 中路径为:7 -> 5 -> 1 -> 3 后路径为:7 -> 5 -> 2 -> 8 -> 4 -> 6 -> 3 -> 1 层路径为:1 -> 3 -> 2 -> 6 -> 5 -> 4 -> 8 -> 7 请输入要查找的节点的值(输入-1退出程):-1 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值