二叉树可视化代码

文章详细描述了一个C++程序,用于操作二叉树,包括初始化数据、生成随机二叉树、宽度计算、可视化以及各种遍历方法。主要关注了二叉树的结构和基本操作。
摘要由CSDN通过智能技术生成

代码解释

只实现到了第五个功能

样例

在这里插入图片描述
如果需要实现这样一个二叉树输入应该是 AB##CD###

得到的结果

在这里插入图片描述

代码主体

/*
   源文件名:P3.cpp
   功能:二叉树操作
*/

#include <conio.h>
#include <stdio.h>
#include <process.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <fstream>
#define max 100
#include <iostream>
using namespace std;

struct BiTNode
{                  // 二叉树结点类型
   char data;      // 数据
   int tem1, tem2; // 辅助数据(实习题不用)
   BiTNode *left;  // 左子树指针
   BiTNode *right; // 右子树指针
};

BiTNode *Tree; // 本程序操作的二叉树根指针

// 屏幕提示后,从键盘输入个数和随机数种子,在数组elem中生成指定个数的数据,供其他程序使用,0表示数据结束
void init0(int list[])
{
   int i, n;
   while (1)
   {
      printf("输入数据个数(0--%d):", max - 1); //
      scanf("%d", &n);
      if (n >= 0 && n <= max - 1)
         break;
      printf("\n");
   }
   while (1)
   {
      printf("输入随机数种子(0-32767):");
      scanf("%d", &i);
      if (i >= 0 && i <= 32767)
         break;
      printf("\n");
   }
   srand(i); // 指定随机数种子,相同的种子将产生相同的数据序列
   rand();

   for (i = 0; i < n; i++)
   {
      list[i] = rand() % 999 + 1;
   }
   list[n] = 0;
}

void getWidth(BiTNode *Tree, int depth, int shift, char map[max * 2][max * 2]) // 生成可视化视图
{
   int i;

   if (Tree->left != NULL)
   {
      getWidth(Tree->left, depth + 1, shift, map);
      Tree->tem1 = Tree->left->tem1 + Tree->left->tem2 + 1;
      for (i = (Tree->left->tem1 + shift) * 2; i < shift * 2; i = i + 2)
      {
         map[depth * 2 + 1][i] = '-';
         map[depth * 2 + 1][i + 1] = '-';
      }
   }
   else
      Tree->tem1 = 0;
   if (Tree->right != NULL)
   {
      getWidth(Tree->right, depth + 1, shift + Tree->tem1 + 1, map);
      Tree->tem2 = Tree->right->tem1 + Tree->right->tem2 + 1;
   }
   else
      Tree->tem2 = 0;

   for (i = shift * 2; i < (Tree->tem1 + shift) * 2; i++)
      map[depth * 2][i] = ' ';

   map[depth * 2][(Tree->tem1 + shift) * 2] = Tree->data; // 直接存储字符数据

   // ... 其他代码 ...
}


// 生成文件Map.txt,显示以Tree为根指针的二叉树
void showBinTree(BiTNode *Tree)
{
    char map[max * 2][max * 2]; // 创建足够空间的数组
    ofstream out;
    int i, j, k;

    out.open("Map.txt");
    if (Tree == NULL) // 判断是否为空树
    {
        out << "空树";
        out.close();
        return;
    }
    for (i = 0; i < max * 2; i++)
        for (j = 0; j < max * 2; j++)
            map[i][j] = ' ';
    getWidth(Tree, 0, 0, map);
    for (i = 0; i < max * 2; i++)
    {
        k = max * 2 - 1;
        while (k >= 0 && map[i][k] == ' ')
            k--;

        for (j = 0; j <= k; j++)
            out << map[i][j];
        out << "\n";
    }
    out.close();
}


// 供init1O调用的释放函数
void release(BiTNode *Tree)
{
   if (Tree == NULL)
      return;
   release(Tree->left);
   release(Tree->right);
   free(Tree);
}

// 供init1调用的生成函数
BiTNode *Generate(int n)
{
   int nl;
   if (n == 0)
      return NULL;

   BiTNode *P = new BiTNode;
   P->data = rand() % 999 + 1;
   nl = rand() % (n);
   P->left = Generate(nl);
   P->right = Generate(n - 1 - nl);
   return P;
}

// 屏幕提示后,从键盘输入个数和随机数种子,以Tree为根指针,生成指定结点个数的二叉树,供其他程序使用,同时生成文件Map.txt,显示这课二叉树
BiTNode *init1()
{
   int i, n;

   while (1)
   {
      printf("输入数据个数(0-%d):", max - 1);
      scanf("%d", &n);
      if (n >= 0 && n <= max - 1)
         break;
      printf("\n");
   }
   while (1)
   {
      printf("输入随机数种子(0-32767):");
      scanf("%d", &i);
      if (i >= 0 && i <= 32767)
         break;
      printf("\n");
   }
   srand(i); // 指定随机数种子,相同的种子将产生相同的数据序列
   rand();

   release(Tree);
   return Generate(n);
}

int elem[max]; // 存放原始数据

// 从键盘输入个数和随机数种子,在数组elem中生成指定个数的数据,供其他程序使用,0表示数据结束
void init0(int list[]);

// 在本程序所在的位置生成文本文件Map.txt,其中显示以Tree为根指针的二叉树
void showBinTree(BiTNode *Tree);

// 从键盘输入个数和随机数种子,以Tree为根指针,生成指定结点个数的二叉树,供其他程序使用
BiTNode *init1();

void CreateBiTree(BiTNode *&Tree)
{
   char ch;
   cin >> ch;
   if (ch == '#')
   {
      Tree = NULL;
   }
   else
   {
      Tree = (BiTNode *)malloc(sizeof(BiTNode));
      Tree->data = ch;
      CreateBiTree(Tree->left);
      CreateBiTree(Tree->right);
   }
}

/*先序遍历*/
void PreOrderTraverse(BiTNode *T)
{

   if (T)
   {
      cout << T->data;
      PreOrderTraverse(T->left);
      PreOrderTraverse(T->right);
   }
}

/*中序遍历*/
void InOrderTraverse(BiTNode *T)
{

   if (T)
   {
      InOrderTraverse(T->left);
      cout << T->data;
      InOrderTraverse(T->right);
   }
}

/*后序遍历*/
void PostOrderTraverse(BiTNode *T)
{

   if (T)
   {
      PostOrderTraverse(T->left);
      PostOrderTraverse(T->right);
      cout << T->data;
   }
}

// 主函数,显示功能菜单(包括生成二叉树、显示二叉树),键盘选择后执行对应功能
int main()
{
   int ch;
   while (1)
   {
      printf("     学号:      姓名:        \n");
      printf("\n\n\n\n");
      printf("\t\t              二叉树操作  \n");
      printf("\t\t======================================");
      printf("\n\n");
      printf("\t\t             1:创建二叉树     \n");
      printf("\t\t             2:先序遍历      \n");
      printf("\t\t             3:中序遍历    \n");
      printf("\t\t             4:后序遍历        \n");
      printf("\t\t             5:二叉树可视化        \n");
      printf("\t\t             6:逆置       \n");
      printf("\t\t             7:remove_6       \n");
      printf("\t\t             8:level_1       \n");
      printf("\t\t             9:high_level_5       \n");
      printf("\t\t             0:退出        \n");
      printf("\n");
      printf("\t\t请选择:");
      cin >> ch; // cin>>ch;
      switch (ch)
      {
      case 1:
         cout << "先序遍历输入(以#结束):";
         CreateBiTree(Tree);
         break;
      case 2:
         cout << endl
              << "先序遍历输出:";
         PreOrderTraverse(Tree);
         break;

      case 3:
         cout << "中序遍历输出:";
         InOrderTraverse(Tree);
         break;
      case 4:
         cout << "\n"
              << "后序遍历输出:";
         PostOrderTraverse(Tree);
         break;
      case 5:
         showBinTree(Tree);
         break;

      case 0:
         exit(0);
         break;
      }

      // Tree=init1();
   }
   showBinTree(Tree);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值