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

来源:互联网 发布:苏联暴行知乎 编辑:程序博客网 时间:2024/06/08 09:00
#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;}

阅读全文
1 0
原创粉丝点击