您的位置:首页 > 其它

输入广义表形式的树(字符串),删除指定值的节点(节点值为单个字母)及其子树,并且输出此时树的广义表形式

2017-08-08 22:31 435 查看
#include<iostream>
#include<string>
#include<stack>
using namespace std;
class Node{
public:
char data;
Node *lchild, *rchild;

Node(char _data){
data = _data;
lchild = NULL;
rchild = NULL;
}
~Node(){
if (lchild != NULL){
delete lchild;
lchild = NULL;
}
if (rchild != NULL){
delete rchild;
rchild = NULL;
}
}
void build(const string& str){//由广义表建树,其实就是先序遍历
int k = -1;
stack<Node*> Stack;
Stack.push(this);
for (int i = 1; i < str.length(); i++){
if (str[i] == '('){
k = 0;//表示进入下一层
Node* p = new Node(str[i]);//标记节点,区分左右
Stack.push(p);
}
else if (str[i] == ','){
k = 1;//表示转向右子树
}
else if (str[i] == ')'){
Stack.pop();//表示返回上一层
}
else {
Node *temp = new Node(str[i]);
if (k == -1){
Stack.push(temp);//作为根节点直接入栈
}
else if (k == 0){//这种形式a,(b,c)
if (Stack.top()->data == '('){
Stack.pop();//也就把上次的左括号出栈
}
Stack.top()->lchild = temp;//作为栈顶的左孩子
Stack.push(temp);
}
else if (k == 1){ //这种形式a(,c)
Stack.pop();
Stack.top()->rchild = temp;
Stack.push(temp);
}
}
}
}
void output(){//输出广义表形式
cout << data;
if (lchild != NULL){
cout << '(';
lchild->output();
}
if (rchild != NULL){
if (lchild == NULL){
cout << "(";
}
cout << ',';
rchild->output();
cout << ')';
}
if(lchild!=NULL&&rchild==NULL){//防止右子树为空时缺少右括号
cout << ')';
}
}
};
class BinaryTree{
private:
Node *root;
public:
BinaryTree(){
root = NULL;
}
BinaryTree(Node*& del){
root = del;
}
BinaryTree(const string& str){//由广义表建树
root = new Node(str[0]);
root->build(str);
}
~BinaryTree(){
if (root != NULL){
delete root;
root = NULL;
}
}
void output(){
root->output();
}
void find_remove(char delete_data){//查找值为delete_data的节点,并删除它及其子树。  前提:该值不能为 整棵树的根节点
stack<Node*> s;
s.push(root);
Node* p = this->root;
while (!s.empty()){
/*
栈的特点:先进后出
先被访问的根节点的右子树后被访问
*/
if (p->lchild != NULL){ //找到父节点
if (p->lchild->data == delete_data){
BinaryTree deleteSubTree(p->lchild);//构建待删除的子树,利用析构函数删除
deleteSubTree.~BinaryTree();
p->lchild = NULL;  //切断子树
return;
}
}
if (p->rchild != NULL){
if (p->rchild->data == delete_data){
BinaryTree delete_tree(p->rchild);//构建待删除的子树,利用析构函数删除
delete_tree.~BinaryTree();
p->rchild = NULL;//切断子树
return;
}
}

if (p->rchild){
s.push(p->rchild);
}
if (p->lchild){
p = p->lchild;
}
else{
//左子树访问完了,访问右子树
p = s.top();
s.pop();
}
}
}
};

int main(){
string str;
char search;
cin >> str>>search;//7(4(3,6),1(,5))
BinaryTree binarytree(str);
if (str.find(search) == str.npos){//如果没找到这个数值
binarytree.output();//直接输出
}
else{
if (search != str[0]){//不删除头结点时
binarytree.find_remove(search);//删除该值的节点及其子树
binarytree.output();
}
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐