上机4.1的题目

 

直接上代码:

  1 //============================================================================
  2 // Name        : 上机4.1  tree.cpp
  3 // Author      :
  4 // Version     :
  5 // Copyright   : Your copyright notice
  6 // Description : Hello World in C++, Ansi-style
  7 //============================================================================
  8 /***
  9  * 1、    定义链接存储的二叉树类。
 10 2、    实验验证如下算法的正确性、各种功能及指标:
 11 1)创建一棵二叉树,并对其初始化;
 12 2)先根、中根、后根遍历二叉树;
 13 3)在二叉树中搜索给定结点的父结点;
 14 4)搜索二叉树中符合数据域条件的结点;
 15 5)从二叉树中删除给定结点及其左右子树。
 16 3、    为便于观察程序的运行结果,
 17     设计的输出函数能在输出设备上以图形或表格或其它直观的形式展现、存储计算结果。
 18 4、    测试程序时,对所有输入变量取遍各种有代表性的值。
 19 5、    为了增强程序的可读性,程序中要有适当的注释。
 20  *
 21  */
 22 #include <iostream>
 23 
 24 using namespace std;
 25 
 26 typedef struct BinTreeNode{
 27     char data;
 28     struct BinTreeNode * left,*right;
 29 }BinTreeNode;
 30 
 31 class BinTree{
 32 private:
 33     BinTreeNode *root;
 34 public:
 35     BinTree(){root = NULL;}
 36     BinTree(BinTreeNode & temp){
 37         root = &temp;
 38         temp.left = NULL;
 39         temp.right = NULL;
 40     }
 41     BinTreeNode * creat(){ //递归创建树
 42         char t;
 43         cin>>t;
 44         if(t=='#'){// #  stand for null
 45             return NULL;
 46         }
 47         else{
 48             BinTreeNode *r = new BinTreeNode;
 49             r->data = t;
 50             r->left = creat();
 51             r->right = creat();
 52             return r;
 53         }
 54     }
 55 };
 56 void preOrderTra(BinTreeNode *t){ //先跟便利
 57         if(t){
 58             cout<<t->data<<" ";
 59             preOrderTra(t->left);
 60             preOrderTra(t->right);
 61         }
 62 }
 63 void midOrderTra(BinTreeNode *t){  //中跟遍历
 64     if(t){
 65         midOrderTra(t->left);
 66         cout<<t->data<<" ";
 67         midOrderTra(t->right);
 68     }
 69 }
 70 void bacOrderTra(BinTreeNode *t){  //后跟遍历
 71     if(t){
 72         bacOrderTra(t->left);
 73         bacOrderTra(t->right);
 74         cout<<t->data<<" ";
 75     }
 76 }
 77 BinTreeNode * findFather(BinTreeNode *t, const char &p){
 78     //在t为跟的树中查找节点data p的父节点
 79     BinTreeNode *q;
 80     if(t== NULL) {
 81         return NULL; //根为空
 82     }
 83     if(t->data==p) {
 84         //cout<<"根节点就是"<<endl;
 85         return t;
 86     }
 87     if(t->left==NULL||t->right==NULL){
 88         //cout<<"叶节点"<<endl;
 89         return NULL;
 90     }
 91     if(t->left->data == p||t->right->data == p){
 92         //cout<<"找到了,返回"<<endl;
 93         return t;// find it
 94     }
 95     if((q= findFather((t->left),p))!=NULL){
 96         //cout<<"继续在左节点寻找"<<endl;
 97         return q;   //在t的左子树继续递归寻找
 98     }
 99     else {
100         //cout<<"继续在右节点寻找"<<endl;
101         return findFather(t->right,p);  //否则在右子树寻找
102     }
103 }
104 BinTreeNode * find(BinTreeNode * q,const char &c){
105     BinTreeNode *p=NULL;
106     cout<<"find"<<endl;
107     if(q==NULL){
108         cout<<"root is null"<<endl;
109         return NULL;
110     }
111     if(q->data==c){   //要先判断值,再判断左右儿子是否为空!!!!!!!!!
112         cout<<"find it"<<endl;
113         return q; //find it
114     }
115 
116     if(q->left==NULL){
117         cout<<"left is null"<<endl;
118         return NULL;
119     }
120     if(q->right==NULL){
121         cout<<"right is null"<<endl;
122         return NULL;
123     }
124 
125     if((p=find(q->left,c))!=NULL){
126         cout<<"find left"<<endl;
127         return p;
128     }
129     else{
130         cout<<"find right"<<endl;
131         return find(q->right,c);
132     }
133 }
134 
135 void delTree(BinTreeNode *r ,const char &c){
136     BinTreeNode *temp;
137     temp = findFather(r,c);
138     if(temp==NULL){
139         cout<<"not find char c!!!"<<endl;
140         return ;
141     }
142     if(temp->left->data==c){
143         temp->left=NULL;
144     }
145     if(temp->right->data==c){
146         temp->right=NULL;
147     }
148     cout<<"deleted!"<<endl;
149     return ;
150 }
151 
152 int main() {
153     BinTree mb;
154     BinTreeNode *r;
155     cout<<"输入要创建的树,空节点用#代替\n";
156     r = mb.creat();
157     cout<<"先跟遍历"<<endl;
158     preOrderTra(r);
159     cout<<"\n中跟遍历"<<endl;
160     midOrderTra(r);
161     cout<<"\n后跟遍历"<<endl;
162     bacOrderTra(r);
163     cout<<"find"<<endl;
164     cout<<"A的父节点值为:"<<findFather(r,'A')->data<<endl;
165     cout<<"B的父节点值为:"<<findFather(r,'B')->data<<endl;
166     cout<<"C的父节点值为:"<<findFather(r,'C')->data<<endl;
167     cout<<"D的父节点值为:"<<findFather(r,'D')->data<<endl;
168     cout<<"E的父节点值为:"<<findFather(r,'E')->data<<endl;
169     cout<<"F的父节点值为:"<<findFather(r,'F')->data<<endl;
170     cout<<"-的父节点值为:"<<findFather(r,'-')->data<<endl;
171     BinTreeNode *temp;
172     temp = find(r,'A');
173     cout<<"find A:"<<temp->data;
174 
175     delTree(r,'-');
176     cout<<"bianli:"<<endl;
177     preOrderTra(r);
178 
179 
180     return 0;
181 }

 

测试数据:

 *+A##B##+*-C##D##E##F##

 

运行结果:eclipse下的

posted on 2012-11-16 11:51  _兜  阅读(787)  评论(1编辑  收藏  举报