1 给出后序遍历序列###ca##ji####spom, 构建二叉树
void buildtree(string &str);//在buildtree中调用build,将root作为参数传给build
void build(string &str,int &index,node* &root );//递归
2一棵二叉排序树,查找c节点,输出从树根到c节点的路径。
法一循环+stack
void tree::searchtree(char c)//在searchtree里创建一个栈,调用search,将root,stack作为search的参数,若之后stack为空说明没找到,不然stack里存的就是其路径
void search(node* &root,char c,stack<node*> &mystack);//后序遍历(最后再push),找到该节点就push,然后栈不为空,就把父节点push。
法二循环+stack
void tree::search(char c)//stack里存的是路径的倒序
3
一棵二叉排序树,删除其中的m节点,使得删除后仍为二叉排序树
void remove(char m);remove调用search——and——destory,将root作为参数传
int search_destory(node* &root,char c);//return removenode
int removenode(node* &root,char c);//若root是null说明没找到,返回-1.不然就进行删除,最后返回0
若待删除节点左子树或右子树为空,则直接删。不然找到其中序遍历的前驱w,用w节点的值代替要删除的m节点,然后删除w节点原来所在的位置
struct node
{
node *leftchild;
node *rightchild;
char value;
node()
{
leftchild=rightchild=NULL;
value=' ';
}
node(char x)
{
leftchild=rightchild=NULL;
value=x;
}
};
class tree {
public:
tree();
~tree();
void buildtree(string &str);
void postordertree();
void searchtree(char c);
void remove(char m);
void destory(node* &n);
private:
void build(string &str,int &index,node* &root );
void postorder(node* root);
void search(node* &root,char c,stack<node*> &mystack);
int removenode(node* &root,char c);
int search_destory(node* &root,char c);
node *root;
};
tree::tree()
{
root=NULL;
}
void tree::build(string &str,int &index,node* &root )
{
if(index>=0)//index>=0
{
if(str[index]=='#')
root=NULL;
else{
node *p=new node(str[index]);
root=p;
build(str,--index,root->rightchild);
build(str,--index,root->leftchild);
}
}
}
tree::~tree()
{
destory(root);
}
void tree::destory(node* & root)
{
if(root==NULL)
return;
node *left=root->leftchild;
node *right=root->rightchild;
delete root;
destory(left);
destory(right);
}
void tree::postordertree()
{
postorder(root);
}
void tree::postorder(node* root)
{
if(root==NULL)
{
cout<<'#';
return;
}
postorder(root->leftchild);
postorder(root->rightchild);
cout<<root->value;
}
void tree::buildtree(string &str)
{
int len=str.length()-1;
build(str,len,root);
}
void tree::searchtree(char c)
{
stack<node*> mystack;
stack<node*> mystack2;
search(root,c,mystack);
if(mystack.empty())
{
cout<<"not found";
}
else
{
while(!mystack.empty())
{
node *p=mystack.top();
mystack2.push(p);
mystack.pop();
}
cout<<endl;
while(!mystack2.empty())
{
cout<<mystack2.top()->value;
mystack2.pop();
}
}
}
void tree::search(node* &root,char c,stack<node*> &mystack)//add &before mystack
{
if(root==NULL)
return;
if(root->value==c)
{
mystack.push(root);
return ;
}
else {
mystack.push(root);
if (root->value > c) {
search(root->leftchild, c, mystack);
} else {
search(root->rightchild, c, mystack);
}
if(mystack.top()->value!=c)//both its right and left child don't contain c
{
mystack.pop();
}
}
}
void tree:: remove(char c)
{
if(search_destory(root,c)==-1)
{
cout<<endl;
cout<<"not present";
}
}
int tree:: removenode(node* &root,char c)
{
if(root==NULL)
return -1;
node *to_delete=root;//should be put here to save the pointer to delete
if(root->leftchild==NULL)
{
root=root->rightchild;
}
else if(root->rightchild==NULL)
root=root->leftchild;
else
{
node*parent=root;
to_delete=root->leftchild;//find the last before root in midorder
while(to_delete->rightchild!=NULL)
{
parent=to_delete;
to_delete=to_delete->rightchild;
}
root->value=to_delete->value;//just move value
if(parent==root) root->leftchild=to_delete->leftchild;//the left child of root don't have any right child,so its left child should be deleted
//to_delete=to_delete->right is wrong,because we will delete to_delete at last
else parent->rightchild=to_delete->leftchild;
delete to_delete;
}
return 0;
}
int tree::search_destory(node* &root,char c)
{
if(root==NULL ||root->value==c)
return removenode(root,c);
else if(c>root->value)
return removenode(root->rightchild,c);
else
return removenode(root->leftchild,c);
}
void tree::searchtree(char c)
{
stack<node*> mystack;
search(root,c,mystack);
if(mystack.empty())
{
cout<<"not found";
}
else
{
cout<<endl;
while(!mystack.empty())
{
cout<<mystack.top()->value;
mystack.pop();
}
}
}
void tree::search(node* &root,char c,stack<node*> &mystack)//add &before mystack
{
if(root==NULL)
return;
if(root->value==c)
{
mystack.push(root);
return ;
}
else {
if (root->value > c) {
search(root->leftchild, c, mystack);
} else {
search(root->rightchild, c, mystack);
}
if(!mystack.empty())//both its right and left child don't contain c
{
mystack.push(root);
}
}
}
void tree::search(char c)
{
stack<node*> mystack;
node* p=root;
mystack.push(p);
while(p!=NULL)
{
if(p->value==c)
{
break;
}
else if(p->value<c)
{
p=p->rightchild;
}
else
p=p->leftchild;
mystack.push(p);
}
stack<node*> mystack2;
if(mystack.top()->value==c)
{
while(!mystack.empty())
{
node *p=mystack.top();
mystack2.push(p);
mystack.pop();
}
cout<<endl;
while(!mystack2.empty())
{
cout<<mystack2.top()->value;
mystack2.pop();
}
}
else
{
cout<<"not found";
}
}