二叉树基本操作2

  1 //删除以cur为根结点的树
  2 void delete_tree(node *&cur){
  3     if(cur!=NULL){
  4         delete_tree(cur->lchild);
  5         delete_tree(cur->rchild);
  6         //delete只是释放内存,指针可能成为野指针
  7         delete cur;
  8         cur=NULL;
  9     }
 10 }
 11 
 12 
 13 //删除所有值为value的结点的子树
 14 //注意参数为指针的引用或指针的指针
 15 void delete_all(node* &cur, char value){
 16     if(cur->lchild!=NULL){
 17         delete_all(cur->lchild,value);
 18     }
 19     if(cur->rchild!=NULL){
 20         delete_all(cur->rchild,value);
 21     }
 22     if(cur->data==value){
 23         delete_tree(cur);
 24     }
 25 }
 26 
 27 
 28 
 29 //借助队列实现层序遍历
 30 void layorder(Node* root, int max_len){
 31     Node* Queue[max_len];
 32     Node* temp;
 33     int head=0,tail=-1;
 34     tail++;
 35     Queue[tail]=root;
 36     while(head<=tail){
 37         temp=Queue[head];
 38         if(temp!=root) cout<<" ";
 39         cout<<temp->data;
 40         if(temp->lchild!=NULL){
 41             tail++;
 42             Queue[tail]=temp->lchild;
 43         }
 44         if(temp->rchild!=NULL){
 45             tail++;
 46             Queue[tail]=temp->rchild;
 47         }
 48         head++;
 49     }
 50 }
 51 
 52 
 53 //以三元组形式(e.g.^AL,ABL,^^L)输入二叉树
 54 Node* build(){
 55     Node* Queue[100];
 56     Node *temp, *root=NULL;
 57     int head=0,tail=-1;
 58     string buffer;
 59     cin>>buffer;
 60     while(buffer[1]!='^'){
 61         temp=new Node;
 62         temp->data=buffer[1];
 63         if(buffer[0]=='^'){
 64             root=temp;
 65             tail++;
 66             Queue[tail]=temp;
 67         }
 68         else{
 69             while(Queue[head]->data!=buffer[0]){
 70                 head++;
 71             }
 72             if(buffer[2]=='L'){
 73                 Queue[head]->lchild=temp;
 74                 tail++;
 75                 Queue[tail]=temp;
 76             }
 77             else if(buffer[2]=='R'){
 78                 Queue[head]->rchild=temp;
 79                 tail++;
 80                 Queue[tail]=temp;
 81             }
 82         }
 83         cin>>buffer;
 84     }
 85     return root;
 86 }
 87 
 88 
 89 
 90 //返回data域为goal的结点指针
 91 Node *find_node(Node *cur, Type goal){
 92     Node *temp;
 93     if(cur==NULL){
 94         return NULL;
 95     }
 96     else if(cur->data==goal){
 97         return cur;
 98     }
 99     else{
100         temp=find_node(cur->lchild,goal);
101         if(temp!=NULL) return temp;
102         else return find_node(cur->rchild,goal);
103     }
104 }
105 
106 
107 
108 //递归寻找目标结点(same data)的父结点
109 //当然也可以不用递归,用队列进行层序遍历并比较
110 Node* find_parent(Node *&cur,Node *&goal){
111     if(cur->lchild!=NULL){
112         if(cur->lchild->data==goal->data){
113             return cur;
114         }
115         if(find_parent(cur->lchild,goal)!=NULL){
116             return find_parent(cur->lchild,goal);
117         }
118     }
119     if(cur->rchild!=NULL){
120         if(cur->rchild->data==goal->data){
121             return cur;
122         }
123         if(find_parent(cur->rchild,goal)!=NULL){
124             return find_parent(cur->rchild,goal);
125         }
126     }
127     return NULL;
128 }
129 
130 //递归求树的高度
131 //也可以不用递归,用栈求
132 int height(Node *cur){
133     int lmax,rmax;
134     if(cur==NULL) return 0;
135     lmax=height(cur->lchild);
136     rmax=height(cur->rchild);
137     return max(lmax,rmax)+1;
138 }
139 
140 
141 
142 //求二叉树的宽度(结点数最多的那一层的结点个数)
143 //在层序遍历的基础上,给队列元素加上一个变量记录层数
144 //最后从头到尾记录下每层结点数并找出最大值
145 int max_width(Node *root){
146     if(root==NULL) return 0;
147     struct{
148         int level_no;
149         Node *p;
150     }Queue[MAX_SIZE];
151     Node *temp;
152     int head=0,tail=-1,cur_level;
153     tail++;
154     Queue[tail].p=root;
155     Queue[tail].level_no=1;
156     while(tail>=head){
157         cur_level=Queue[head].level_no;
158         temp=Queue[head].p;
159         head++;
160         if(temp->lchild!=NULL){
161             tail++;
162             Queue[tail].p=temp->lchild;
163             Queue[tail].level_no=cur_level+1;
164         }
165         if(temp->rchild!=NULL){
166             tail++;
167             Queue[tail].p=temp->rchild;
168             Queue[tail].level_no=cur_level+1;
169         }
170     }
171     int i=0,max=0,cur_max;
172     cur_level=1;
173     while(i<=tail){
174         cur_max=0;
175         while(i<=tail&&Queue[i].level_no==cur_level){
176             cur_max++;
177             i++;
178         }
179         if(max<cur_max) max=cur_max;
180         //这一层结束了就进入下一层数结点
181         cur_level++;
182     }
183     return max;
184 }
185 
186 
187 //求二叉树叶子结点的个数
188 int leaf_nodes(Node *cur){
189     if(cur==NULL){
190         return 0;
191     }
192     else if(cur->lchild==NULL&&cur->rchild==NULL){
193         return 1;
194     }
195     else{
196         return leaf_nodes(cur->lchild)+leaf_nodes(cur->rchild);
197     }
198 }
199 
200 
201 //求结点data==goal的结点的深度(假设只有1个)
202 int level(Node *cur, Type goal, int cur_level){
203     int left,right;
204     if(cur==NULL){
205         return 0;
206     }
207     else if(cur->data==goal){
208         return cur_level;
209     }
210     else{
211         left=level(cur->lchild,goal,cur_level+1);
212         if(left!=0) return left;
213         else return right=level(cur->rchild,goal,cur_level+1);
214     }
215 }

 

posted @ 2017-06-17 20:39  NoviScl  阅读(241)  评论(0编辑  收藏  举报