📄 print.h
字号:
#include<iostream>
#include "treeabc.h"
#include<cmath>//运用POW函数所需
#define SIZE 50//定义一个存储节点的数组
//用于打印所需打印的空格数
typedef struct AVLTree AVLTree;
void IndentBlanks(int num1,int num2){
for(int in=1;in<=(int)pow(2,num1+2-num2)-1;in++)//根据层数打印空格
std::cout<<" ";
}
//用于打印所许打印的标志
void Indentflags(int num1,int num2){
for(int in=1;in<=(int)pow(2,num1-num2)-1;in++)//根据层数打印
std::cout<<"-";
}
//按层数搜索整个树,并且将其存储与变量 nodeVector中
void floor_search(AVLTree* ptr, int* nodeVector, int temp, int node, int* ph)
{
nodeVector[node]=ptr->nData;
if(temp>*ph)//放置到起点
*ph=temp;
if(ptr->pLeft)//运用递归的方法
floor_search(ptr->pLeft, nodeVector, temp+1, node*2, ph);
if(ptr->pRight)
floor_search(ptr->pRight, nodeVector, temp+1, node*2+1, ph);
}
void printBTree(AVLTree* ptr)
{
int nodeVector[SIZE]={0};//初始
int height=0;
int n,floor,temp1,temp2;
floor_search(ptr, nodeVector, 1, 1, &height);
n=(int)pow(2,height)-1;//n存储的是此高度下饱和节点
floor=1;
for(temp1=1;temp1<=n;temp1++)
{
if(nodeVector[temp1]==0)
IndentBlanks(height,floor); //如果节点为空 调用打印空格的子函数
else
{
if(temp1*2<=n&&nodeVector[temp1*2]!=0)
{
IndentBlanks(height-2,floor);
std::cout<<"|";//左子数边界标志
Indentflags(height,floor);
}
else
IndentBlanks(height-1,floor);
std::cout<<nodeVector[temp1];
if(temp1*2<=n&&nodeVector[temp1*2+1]!=0)
{
Indentflags(height,floor);
std::cout<<"|";//右子数边界标志
IndentBlanks(height-2,floor);//子函数调用
}
else
IndentBlanks(height-1,floor);
}
std::cout<<" ";
if(temp1==(int)pow(2,floor)-1)
{
std::cout<<std::endl;
floor++;
}
}
std::cout<<std::endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -