主题:程序出错
编程高手进来,高分悬赏
在下面这个程序中如何修改以增加二叉树指定节点的删除操作?
#ifndef _LAB4_H
#define _LAB4_H
#include <queue>
#include <iostream>
#include <cstdio>
#include <cstring>
using std::printf;
using std::strlen;
using std::cout;
using std::queue;
template <class T> class BinaryTree;
template <class T>
class TreeNode{
friend class BinaryTree<T>;
T data;
TreeNode<T> *left;
TreeNode<T> *right;
};
template <class T>
class BinaryTree{
public:
BinaryTree(){
root=0;
numberOfNode=0;
}
~BinaryTree();
BinaryTree<T>& Insert(const T& x);
void PreOrderOutput();
void PostOrderOutput();
void InOrderOutput();
int height();
int totalNodes(){
return numberOfNode;
}
void LevelOrderOutput();
private:
TreeNode<T> *root;
int numberOfNode;
void Pre(TreeNode<T> *x);
void Post(TreeNode<T> *x);
void In(TreeNode<T> *x);
int h(TreeNode<T> *x);
};
template <class T>
BinaryTree<T>::~BinaryTree<T>(){
queue< TreeNode<T>* > buf;
buf.push(root);
TreeNode<T> *p;
while(!buf.empty()){
p=buf.front();
buf.pop();
if(p&&p->left){
buf.push(p->left);
}
if(p&&p->right){
buf.push(p->right);
}
if(p){
delete p;
}
}
};
template <class T>
BinaryTree<T>& BinaryTree<T>::Insert(const T& x){
++numberOfNode;
TreeNode<T>* newNode=new TreeNode<T>();
newNode->data=x;
newNode->left=0;
newNode->right=0;
TreeNode<T> *present=root;
TreeNode<T> *parent=0;
while(present){
if(x>=present->data){
parent=present;
present=present->right;
}
else{
parent=present;
present=present->left;
}
}
if(!root){
root=newNode;
}
else{
if(x>=parent->data){
parent->right=newNode;
}
else{
parent->left=newNode;
}
}
};
template <class T>
void BinaryTree<T>::InOrderOutput(){
In(root);
};
template <class T>
void BinaryTree<T>::PreOrderOutput(){
Pre(root);
};
template <class T>
void BinaryTree<T>::PostOrderOutput(){
Post(root);
};
template <class T>
void BinaryTree<T>::In(TreeNode<T> *x){
if(x){
In(x->left);
cout << x->data;
In(x->right);
}
};
template <class T>
void BinaryTree<T>::Pre(TreeNode<T> *x){
if(x){
cout << x->data;
Pre(x->left);
Pre(x->right);
}
};
template <class T>
void BinaryTree<T>::Post(TreeNode<T> *x){
if(x){
Post(x->left);
Post(x->right);
cout << x->data;
}
};
template <class T>
int BinaryTree<T>::height(){
return h(root)+1;
};
template <class T>
int BinaryTree<T>::h(TreeNode<T> *x){
if(!x||(!x->left)&&(!x->right)){
return 0;
}
int th=0;
int buf=0;
if(x->left){
th=1+h(x->left);
}
if(x->right){
buf=h(x->right);
}
if(buf+1>th){
th=buf+1;
}
return th;
};
template <class T>
void BinaryTree<T>::LevelOrderOutput(){
queue< TreeNode<T>* > buf;
buf.push(root);
TreeNode<T> *p;
while(!buf.empty()){
p=buf.front();
buf.pop();
cout << p->data;
if(p->left){
buf.push(p->left);
}
if(p->right){
buf.push(p->right);
}
}
}
char mid[27];
char pre[27];
int left[27];
int right[27];
int search(int beg,int end,char key){
for(int i = beg ; i <= end ; ++i ){
if(mid[i]==key){
return i;
}
}
return -1;
}
int build(int pbeg,int pend,int mbeg,int mend){
if(pend<pbeg){
return -1;
}
if(pend==pbeg){
left[pend]=right[pbeg]=-1;
return pend;
}
int mmid=search(mbeg,mend,pre[pbeg]);
int leftlength=mmid-mbeg;
int rightlength=mend-mmid;
left[pbeg]=build(pbeg+1,pbeg+leftlength,mbeg,mmid-1);
right[pbeg]=build(pbeg+leftlength+1,pend,mmid+1,mend);
return pbeg;
}
void print(int i){
if(left[i]!=-1){
print(left[i]);
}
if(right[i]!=-1){
print(right[i]);
}
printf("%c",pre[i]);
}
#endif /* _LAB4_H */
//main.cc
#include <stdlib.h>
#include <iostream>
//#include "lab4.h"
using namespace std;
//
//
//
int main(){
cout << "please input the total number of characters you want to input" << endl;
int n;
cin >> n;
cout<<"please input the characters"<<endl;
BinaryTree<char> tree;
char buf;
for(int i = 0 ; i < n ; ++i ){
cin >> buf;
tree.Insert(buf);
}
cout << "the information of the tree is listed as follows:" << endl;
cout << "the height is " << tree.height() << endl;
cout << "inorder output" << endl;
tree.InOrderOutput();
cout << endl;
cout << "preorder output" << endl;
tree.PreOrderOutput();
cout << endl;
cout << "postorder output" << endl;
tree.PostOrderOutput();
cout << endl;
cout << "levelorder output" << endl;
tree.LevelOrderOutput();
cout << endl;
cout << "the number of nodes is " << tree.totalNodes() << endl;
int length;
int root;
cout << "please input the inorder output" << endl;
scanf("%s", mid);
cout << "please input the preorder output" << endl;
scanf("%s", pre);
length=strlen(pre);
root=build(0, length-1, 0, length-1);
cout << "the postorder output is listed as follows" << endl;
print(root);
printf("\n");
int i;
cin>>i;
return 0;
}
在下面这个程序中如何修改以增加二叉树指定节点的删除操作?
#ifndef _LAB4_H
#define _LAB4_H
#include <queue>
#include <iostream>
#include <cstdio>
#include <cstring>
using std::printf;
using std::strlen;
using std::cout;
using std::queue;
template <class T> class BinaryTree;
template <class T>
class TreeNode{
friend class BinaryTree<T>;
T data;
TreeNode<T> *left;
TreeNode<T> *right;
};
template <class T>
class BinaryTree{
public:
BinaryTree(){
root=0;
numberOfNode=0;
}
~BinaryTree();
BinaryTree<T>& Insert(const T& x);
void PreOrderOutput();
void PostOrderOutput();
void InOrderOutput();
int height();
int totalNodes(){
return numberOfNode;
}
void LevelOrderOutput();
private:
TreeNode<T> *root;
int numberOfNode;
void Pre(TreeNode<T> *x);
void Post(TreeNode<T> *x);
void In(TreeNode<T> *x);
int h(TreeNode<T> *x);
};
template <class T>
BinaryTree<T>::~BinaryTree<T>(){
queue< TreeNode<T>* > buf;
buf.push(root);
TreeNode<T> *p;
while(!buf.empty()){
p=buf.front();
buf.pop();
if(p&&p->left){
buf.push(p->left);
}
if(p&&p->right){
buf.push(p->right);
}
if(p){
delete p;
}
}
};
template <class T>
BinaryTree<T>& BinaryTree<T>::Insert(const T& x){
++numberOfNode;
TreeNode<T>* newNode=new TreeNode<T>();
newNode->data=x;
newNode->left=0;
newNode->right=0;
TreeNode<T> *present=root;
TreeNode<T> *parent=0;
while(present){
if(x>=present->data){
parent=present;
present=present->right;
}
else{
parent=present;
present=present->left;
}
}
if(!root){
root=newNode;
}
else{
if(x>=parent->data){
parent->right=newNode;
}
else{
parent->left=newNode;
}
}
};
template <class T>
void BinaryTree<T>::InOrderOutput(){
In(root);
};
template <class T>
void BinaryTree<T>::PreOrderOutput(){
Pre(root);
};
template <class T>
void BinaryTree<T>::PostOrderOutput(){
Post(root);
};
template <class T>
void BinaryTree<T>::In(TreeNode<T> *x){
if(x){
In(x->left);
cout << x->data;
In(x->right);
}
};
template <class T>
void BinaryTree<T>::Pre(TreeNode<T> *x){
if(x){
cout << x->data;
Pre(x->left);
Pre(x->right);
}
};
template <class T>
void BinaryTree<T>::Post(TreeNode<T> *x){
if(x){
Post(x->left);
Post(x->right);
cout << x->data;
}
};
template <class T>
int BinaryTree<T>::height(){
return h(root)+1;
};
template <class T>
int BinaryTree<T>::h(TreeNode<T> *x){
if(!x||(!x->left)&&(!x->right)){
return 0;
}
int th=0;
int buf=0;
if(x->left){
th=1+h(x->left);
}
if(x->right){
buf=h(x->right);
}
if(buf+1>th){
th=buf+1;
}
return th;
};
template <class T>
void BinaryTree<T>::LevelOrderOutput(){
queue< TreeNode<T>* > buf;
buf.push(root);
TreeNode<T> *p;
while(!buf.empty()){
p=buf.front();
buf.pop();
cout << p->data;
if(p->left){
buf.push(p->left);
}
if(p->right){
buf.push(p->right);
}
}
}
char mid[27];
char pre[27];
int left[27];
int right[27];
int search(int beg,int end,char key){
for(int i = beg ; i <= end ; ++i ){
if(mid[i]==key){
return i;
}
}
return -1;
}
int build(int pbeg,int pend,int mbeg,int mend){
if(pend<pbeg){
return -1;
}
if(pend==pbeg){
left[pend]=right[pbeg]=-1;
return pend;
}
int mmid=search(mbeg,mend,pre[pbeg]);
int leftlength=mmid-mbeg;
int rightlength=mend-mmid;
left[pbeg]=build(pbeg+1,pbeg+leftlength,mbeg,mmid-1);
right[pbeg]=build(pbeg+leftlength+1,pend,mmid+1,mend);
return pbeg;
}
void print(int i){
if(left[i]!=-1){
print(left[i]);
}
if(right[i]!=-1){
print(right[i]);
}
printf("%c",pre[i]);
}
#endif /* _LAB4_H */
//main.cc
#include <stdlib.h>
#include <iostream>
//#include "lab4.h"
using namespace std;
//
//
//
int main(){
cout << "please input the total number of characters you want to input" << endl;
int n;
cin >> n;
cout<<"please input the characters"<<endl;
BinaryTree<char> tree;
char buf;
for(int i = 0 ; i < n ; ++i ){
cin >> buf;
tree.Insert(buf);
}
cout << "the information of the tree is listed as follows:" << endl;
cout << "the height is " << tree.height() << endl;
cout << "inorder output" << endl;
tree.InOrderOutput();
cout << endl;
cout << "preorder output" << endl;
tree.PreOrderOutput();
cout << endl;
cout << "postorder output" << endl;
tree.PostOrderOutput();
cout << endl;
cout << "levelorder output" << endl;
tree.LevelOrderOutput();
cout << endl;
cout << "the number of nodes is " << tree.totalNodes() << endl;
int length;
int root;
cout << "please input the inorder output" << endl;
scanf("%s", mid);
cout << "please input the preorder output" << endl;
scanf("%s", pre);
length=strlen(pre);
root=build(0, length-1, 0, length-1);
cout << "the postorder output is listed as follows" << endl;
print(root);
printf("\n");
int i;
cin>>i;
return 0;
}