码迷,mamicode.com
首页 > 编程语言 > 详细

二叉排序树生成删除过程模拟

时间:2017-01-15 12:57:00      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:先序遍历   getchar   amp   view   blocks   pen   main   退出   nbsp   

利用gotoxy语句进行光标移动,利用快速生成二叉树及清屏操作动态显示二叉树生成过程。

界面如下:

技术分享

技术分享

 

 

代码如下:

技术分享
  1 #include <iostream>
  2 #include <stdlib.h>
  3 #include <stdio.h>
  4 #include <windows.h>
  5 using namespace std;
  6 typedef int ElemType;//定义二叉树结点值的类型为字符型
  7 const int MaxLength=10;//结点个数不超过10个
  8 int   Win_x,y=1;//坐标
  9 int  now_y,max_y;
 10 typedef struct BTNode{
 11  ElemType data;
 12  struct BTNode *lchild,*rchild;
 13 }BTNode,* BiTree;
 14 
 15 void gotoxy(int x, int y) //goto语句
 16 {
 17 COORD pos;
 18 pos.X = x - 1;
 19 pos.Y = y - 1;
 20 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
 21 }
 22 void   inorder(BiTree T,int   y)  //以二叉树形状显示
 23 {
 24  BiTree p=T;
 25  if(p!=NULL)
 26  {
 27   inorder(p->lchild,y+2);
 28   gotoxy(Win_x-1,y);
 29   cout<<p->data;
 30   if(y>max_y)
 31     max_y=y;
 32   now_y=y;
 33   Win_x=Win_x+1;
 34   inorder(p->rchild,y+2);
 35  }
 36  else
 37  {
 38   gotoxy(Win_x,y);
 39   Win_x=Win_x+2;
 40  }
 41 }
 42 
 43 void Insert(BiTree &T,int key)//二插排序树插入节点
 44 {
 45 
 46     if(T==NULL)
 47     {
 48         BiTree p=new BTNode;
 49         p->data=key;
 50         p->rchild=NULL;
 51         p->lchild=NULL;
 52         T=p;
 53         return ;
 54     }
 55 
 56     if(T->data>key)
 57     {
 58         Insert(T->lchild,key);
 59     }
 60     else if(T->data<key)
 61     {
 62         Insert(T->rchild,key);
 63     }
 64 }
 65 
 66 void input(BiTree &T)//输入函数
 67 {
 68     int n=0,a[100];
 69     char ch;
 70     max_y=1;
 71     while(true)
 72     {
 73         cin>>a[n++];
 74         ch=getchar();
 75         if(ch==\n)
 76             break;
 77     }
 78     system("cls");
 79     cout<<"二叉排序树的建立过程为:";
 80     for(int i=0;i<n;i++)
 81     {
 82         Sleep(2000);
 83         system("cls");
 84         Insert(T,a[i]);
 85         Win_x=2;
 86         inorder(T,y);
 87     }
 88     for(int i=now_y;i<=max_y;i++)
 89         cout<<endl;
 90 }
 91 
 92 void InOrderTraverse(BiTree T){//中序遍历
 93  if(T){
 94   InOrderTraverse(T->lchild);
 95   cout<<T->data<< ;
 96   InOrderTraverse(T->rchild);
 97  }
 98 }
 99 void FirstOrderTraverse(BiTree T){//先序遍历
100  if(T){
101   cout<<T->data<< ;
102   FirstOrderTraverse(T->lchild);
103   FirstOrderTraverse(T->rchild);
104 
105  }
106 }
107 BiTree binary_search1(BiTree point,int node,int *postion)//找到节点位置 并返还父节点位置
108 {
109  BiTree parent;
110 
111  parent=point;
112  *postion=0;
113 
114  while(point!=NULL)
115  {
116   if(point->data==node)
117    return parent;
118   else
119   {
120    parent=point;
121    if(point->data>node)
122    {
123     point=point->lchild;
124     *postion=-1;
125 
126    }
127    else
128    {
129     point=point->rchild;
130     *postion=1;
131    }
132   }
133  }
134 }
135 
136 BiTree Deletenode(BiTree root,int node)//删除节点
137 {
138  BiTree parent;
139  BiTree point;
140  BiTree child;
141  int postion;
142 
143 
144  parent=binary_search1(root,node,&postion);
145  //二叉树为空的情况
146    if(parent==NULL)
147          return root;
148    else
149    {
150     switch(postion)
151    {
152        case -1:point=parent->lchild;break;
153        case 1 :point=parent->rchild;break;
154        case  0 :point=parent;break;
155    }
156 
157  if(point->lchild==NULL&&point->rchild==NULL)
158  {
159   switch(postion)
160   {
161          case -1:parent->lchild=NULL;break;
162          case 1:parent->rchild=NULL;break;
163          case 0:parent=NULL;break;
164   }
165 
166     free(point);
167    return root;
168     }
169 
170  if(point->lchild==NULL&&point->rchild!=NULL)
171   {
172    if(postion==-1)
173     parent->lchild=point->rchild;
174    else
175     if(postion==1)
176     parent->rchild=point->rchild;
177     else
178      root=root->rchild;
179 
180    free(point);
181    return root;
182   }
183 
184  if(point->lchild!=NULL&&point->rchild==NULL)
185  {
186   if(postion==-1)
187    parent->lchild=point->lchild;
188   else
189    if(postion==1)
190     parent->rchild=point->lchild;
191    else
192     root=root->lchild;
193   return root;
194  }
195 
196  if(point->lchild!=NULL&& point->rchild!=NULL)
197  {
198   parent=point;
199   child=point->lchild;
200   while(child->rchild!=NULL)
201   {
202    parent=child;
203    child=child->rchild;
204   }
205   point->data=child->data;
206   if(parent->lchild==child)
207    parent->lchild=child->lchild;
208   else
209    parent->rchild=child->lchild;
210 
211   free(child);
212   return root;
213     }
214   }
215 }
216 
217 void delete_node(BiTree &T)
218 {
219     int node;
220     printf("请输入要删除的节点:(按空格键结束输入)\n");
221     cin>>node;
222     system("cls");
223     cout<<"二叉排序树的删除过程为:";
224     Sleep(2000);
225     system("cls");
226     max_y=1;
227     Win_x=2;
228     inorder(T,y);
229     Sleep(2000);
230     system("cls");
231     Win_x=2;
232     inorder(Deletenode(T,node),y);
233     Sleep(2000);
234     cout<<endl;
235 }
236 int main()
237 {
238 
239     BiTree T;
240     int a;//坐标
241     T=NULL;
242     while(true)
243     {
244         system("cls");
245         printf("|-------------------------------------------------------|\n");
246         printf("|\t\t\t\t\t\t\t|\n");
247         printf("|\t\t  欢迎进入二叉排序树生成系统! \t\t|\n");
248         printf("|\t\t\t\t\t\t\t|\n");
249         printf("|-------------------------------------------------------|\n");
250         printf("|\t\t\t\t\t\t\t|\n");
251         printf("|\t\t    1——开始            \t\t|\n");
252         printf("|\t\t    0——退出系统        \t\t|\n");
253         printf("|\t\t\t\t\t\t\t|\n");
254         printf("|-------------------------------------------------------|\n");
255         printf("请输入选项编号(0 ~ 1): (按回车键结束输入)\n");
256         cin>>a;
257         if(a==0)
258             break;
259         if(a==1)
260         {
261             printf("请输入数字序列:(数字间用空格隔开,按回车键结束输入)\n");
262             input(T);
263         }
264         printf("按回车键继续。");
265         getchar();
266 
267         while(true)
268         {
269             system("cls");
270             printf("|-------------------------------------------------------|\n");
271             printf("|\t\t\t\t\t\t\t|\n");
272             printf("|\t\t    1——中序输出            \t\t|\n");
273             printf("|\t\t    2——后序输出            \t\t|\n");
274             printf("|\t\t    3——删除节点            \t\t|\n");
275             printf("|\t\t    4——重新输入            \t\t|\n");
276             printf("|\t\t    0——退出系统        \t\t|\n");
277             printf("|\t\t\t\t\t\t\t|\n");
278             printf("|-------------------------------------------------------|\n");
279             printf("请输入选项编号(0 ~ 4): (按回车键结束输入)\n");
280             cin>>a;
281             switch(a)
282             {
283                 case 1:InOrderTraverse(T);break;
284                 case 2:FirstOrderTraverse(T);break;
285                 case 3:delete_node(T);break;
286                 case 4:
287                 case 0:break;
288             }
289             if(a==4||a==0)
290                 break;
291             printf("按回车键继续。");
292             getchar();
293             getchar();
294         }
295         if(a==4)
296             continue;
297         if(a==0)
298             break;
299     }
300 
301 return 0;
302 }
View Code

 

二叉排序树生成删除过程模拟

标签:先序遍历   getchar   amp   view   blocks   pen   main   退出   nbsp   

原文地址:http://www.cnblogs.com/kingbk/p/6286909.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!