二叉树创建、销毁、按照分层在控制台显示、计算二叉树深度等等



// BinaryTree.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <iomanip>
#include<queue>
#include<math.h>

using namespace std;

int calculateLayer(int num);


//二叉树结点结构
typedef struct Node
{
 int data;
 struct Node *Left;
 struct Node *Right;
}ListNode;

//创建二叉树结点
ListNode* createNode(int data,ListNode* left=NULL,ListNode* right=NULL)
{
 ListNode *p=(ListNode *)malloc(sizeof(ListNode));
 p->data=data;
 p->Left=left;
 p->Right=right;
 return p;
}


int calculateLen(ListNode* head);


//创建二叉树
ListNode* createBinTree()
{
 int data;
 cin>>data;
 if(data=='#')
 {
  return NULL;
 }
 ListNode *p=createNode(data);

 cout<<"左子树:"<<endl;
 p->Left=createBinTree();
 cout<<"右子树:"<<endl;
 p->Right=createBinTree();
 
 return p;
}


//析构二叉树
bool DistroyBinTree(ListNode* head)
{
 if(head!=NULL)
 {
  //析构左子树
  DistroyBinTree(head->Left);

  //析构右子树
  DistroyBinTree(head->Right);
  
  free(head);
 }
 return true;
}


//创建二叉树
ListNode* createBinTree(int a[],int len)
{
 if(len<1)
 {
  return NULL;
 }
 else
 {

  ListNode* head=createNode(a[0]);
  ListNode * p=head;
  queue<ListNode*> que;
  que.push(head);

  for(int i=1;;)
  {
   ListNode *temp=que.front();
    que.pop();

   if(i<len)
   {
    ListNode *left=createNode(a[i++]);     
    temp->Left=left;
    que.push(left);
    if(i<len)
    {
     ListNode *right=createNode(a[i++]);
     temp->Right=right;
     que.push(right);
    }

   }
   if(i>=len)
   {
    break;
   }
  }

  return head;
 }
}




//打印二叉树

void PrintTreeByLevel(ListNode *pHead)
{
    if (NULL == pHead)
    {
        return;
    }

 int len=calculateLen(pHead);
    vector<ListNode*> vec;
    vec.push_back(pHead);int cur = 0;
    int last = 0;
 int layer=1;

 bool hasNode=true;
    while((cur < vec.size())&&hasNode)
    {
        last = vec.size();
  hasNode=false;
  cout<<endl;

  int end=(int)(((pow((double)2,(double)(len+1))+1)*2-pow((double)2,(double)(layer))*2)/(pow((double)2,(double)(layer+1))));

  int i=0;
  for(i=0;i<end;i++)
  {
   cout<<" ";
  }

  layer++;

        while (cur < last)
        {
   if(vec[cur]==NULL)
   {
    //cout<<setw(2)<<"    ";


    for(i=0;i<end*2*2+2;i++)
    {
     cout<<" ";
    }

    vec.push_back(NULL);
    vec.push_back(NULL);
   }
   else
   {

    cout<<setw(2)<<vec[cur]->data;
    for(i=0;i<end*2;i++)
    {
     cout<<" ";
    }

    
    vec.push_back(vec[cur]->Left);
    vec.push_back(vec[cur]->Right);
    hasNode=true;
   }
            cur++;
        }
        cout<<endl;
    }
}

//计算二叉树的深度
int calculateLen(ListNode* head)
{
 int result=0;
 if(head==NULL)
 {
  result= 0;
 }
 else
 {
  int leftLen=calculateLen(head->Left);
  int rightLen=calculateLen(head->Right);
  result=(leftLen>rightLen) ? (leftLen+1):(rightLen+1);
 }
 return result;
}


int calculateLayer(int num)
{
 double layer=floor((double)log10((double)num)/log10((double)2));
 return (int)layer;
}


int main()
{
 
 //cout<<endl<<"根近点:"<<endl;
 //ListNode *head=createBinTree();

 int n=20;
 int *a=new int[n];
 for(int i=0;i<n;i++)
 {
  a[i]=i;
 }

 for(int i=0;i<n;i++)
 {
   cout<<a[i]<<" ";
 }
 cout<<endl;

 ListNode *  head=createBinTree(a,n);
 PrintTreeByLevel(head);

 DistroyBinTree(head);


 return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值