zoukankan      html  css  js  c++  java
  • 二叉排序树生成删除过程模拟

    利用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=='
    ')
     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("请输入要删除的节点:(按空格键结束输入)
    ");
    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("|-------------------------------------------------------|
    ");
    246         printf("|							|
    ");
    247         printf("|		  欢迎进入二叉排序树生成系统! 		|
    ");
    248         printf("|							|
    ");
    249         printf("|-------------------------------------------------------|
    ");
    250         printf("|							|
    ");
    251         printf("|		    1——开始            		|
    ");
    252         printf("|		    0——退出系统        		|
    ");
    253         printf("|							|
    ");
    254         printf("|-------------------------------------------------------|
    ");
    255         printf("请输入选项编号(0 ~ 1): (按回车键结束输入)
    ");
    256         cin>>a;
    257         if(a==0)
    258             break;
    259         if(a==1)
    260         {
    261             printf("请输入数字序列:(数字间用空格隔开,按回车键结束输入)
    ");
    262             input(T);
    263         }
    264         printf("按回车键继续。");
    265         getchar();
    266 
    267         while(true)
    268         {
    269             system("cls");
    270             printf("|-------------------------------------------------------|
    ");
    271             printf("|							|
    ");
    272             printf("|		    1——中序输出            		|
    ");
    273             printf("|		    2——后序输出            		|
    ");
    274             printf("|		    3——删除节点            		|
    ");
    275             printf("|		    4——重新输入            		|
    ");
    276             printf("|		    0——退出系统        		|
    ");
    277             printf("|							|
    ");
    278             printf("|-------------------------------------------------------|
    ");
    279             printf("请输入选项编号(0 ~ 4): (按回车键结束输入)
    ");
    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
  • 相关阅读:
    关于 IIS 上运行 ASP.NET Core 站点的“HTTP 错误 500.19”错误
    下单快发货慢:一个 JOIN SQL 引起 SqlClient 读取数据慢的奇特问题
    ASP.NET Core 2.2 项目升级至 3.0 备忘录
    corefx 源码学习:SqlClient 是如何同步建立 Socket 连接的
    Chimee
    electron-vue:Vue.js 开发 Electron 桌面应用
    H5网页适配 iPhoneX,就是这么简单
    经典文摘:饿了么的 PWA 升级实践(结合Vue.js)
    Table Dragger
    分享8个网站开发中最好用的打印页面插件
  • 原文地址:https://www.cnblogs.com/kingbk/p/6286909.html
Copyright © 2011-2022 走看看