逆時針旋轉90度輸出二叉樹(資料結構實驗二),90度二叉樹

來源:互聯網
上載者:User

逆時針旋轉90度輸出二叉樹(資料結構實驗二),90度二叉樹





逆時針旋轉90度列印二叉樹是一種特殊的中序遍曆演算法

圖解逆時針旋轉90度操作


實現也特別簡單,跟中序遍曆演算法差不多,在輸出節點值前,用個特殊標記記錄層數並輸出適當的空格就可以了。

代碼:

void prtbtree(BiTNode *p,int cur)//逆時針旋轉90度輸出二叉樹{    if(p)    {        prtbtree(p->rch,cur+1);        for(int i=0;i<=cur;i++)        printf("  ");        printf("%3c",p->data);        printf("\n");        prtbtree(p->lch,cur+1);    }}



二叉樹,資料結構

我剛剛寫的作業 呵呵

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

#define OVERFLOW 0

//二叉樹的二叉鏈表格儲存體定義
struct Node
{
char data;
struct Node * lchild, * rchild;
};
typedef struct Node BiTNode;
typedef BiTNode * BiTree;

/*************************************************************************\
*
* 按先序次序輸入二叉樹中的結點的值(一個字元)構造二叉鏈表表示的二叉樹,
* 字元'#'表示空樹。
*
* 例如,一棵二叉樹的三種遍曆次序為:
* 先序:-+a*b-cd/ef 中序:a+b*c-d-e/f 後序:abcd-*+ef/
* 程式中應該輸入:-+a##*b##-c##d##/e##f##
*
* 又如,一棵二叉樹的三種遍曆次序為:
* 先序:ABDFGCEH 中序:BFDGACHE 後序:FGDBHECA
* 程式中應該輸入:AB#DF##G##C#EH###
*
\*************************************************************************/
void CreateBiTree(BiTree &T)
{
char ch;
scanf("%c", &ch); // * 按先序次序輸入二叉樹中的結點的值(一個字元)構造二叉鏈表表示的二叉樹,
if (ch=='#') T=NULL; // * 字元'#'表示空樹
else{
if (!(T = (BiTNode *)malloc(sizeof(BiTNode)))) exit(OVERFLOW);
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return;
}//CreateBiTree

void PreOrderTraverse(BiTree T)
{
//按先序遍曆次序輸出二叉樹T中的結點的值(一個字元),二叉樹T用二叉鏈表格儲存體。

if (T)
{
cout<<T->data;
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
return;
}//PreOrderTraverse

void InOrderTraverse(BiTree T)
{
//按中序遍曆次序輸出二叉樹T中的結點的值(一個字元),二叉樹T用二叉鏈表格儲存體。

if (T)
{
InOrderTraverse(T->lchild);
cout<<T->data;
InOrderTraverse(T->rchild)......餘下全文>>
 
二叉樹 資料結構

#include<iostream>
#include<cmath>//提供pow()函數
using namespace std;

#define OK 1
#define ERROR 0

typedef int Status;
typedef struct BiTNode
{
int data;
struct BiTNode *lChild,*rChild;
}BiTNode,*BiTree;

//先序輸出二叉樹
void PreOrderTraverse(BiTree T)
{
if(T)
{
cout<<T->data<<' ';
PreOrderTraverse(T->lChild);
PreOrderTraverse(T->rChild);
}
}

//中序輸出二叉樹
void InOrderTraverse(BiTree T)
{
if(T)
{
InOrderTraverse(T->lChild);
cout<<T->data<<' ';
InOrderTraverse(T->rChild);
}
}

//後序輸出二叉樹
void PostOrderTraverse(BiTree T)
{
if(T)
{
PostOrderTraverse(T->lChild);
PostOrderTraverse(T->rChild);
cout<<T->data<<' ';
}
}

//建立二叉樹
Status CreateBiTree(BiTree& T)
{
int num;
cin>>num;
if(num==0)
T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
T->data=num;
CreateBiTree(T->lChild);
CreateBiTree(T->rChild);
}
return OK;
}

//計算二叉樹的深度
int BiTreeDepth(BiTree T)
{
int depth1,depth2;
if(!T)
return 0;
else{
depth1=BiTreeDepth(T->lChild);
depth2=BiTreeDepth(T->rChild);
if(depth1>depth2)
return depth1+1;
else
return depth2+1;
}
}

static int n=0;
int CountNode(BiTree T)
{
if(T)
{
n++;
CountNode(T->lChild);
CountNode(T->rChild);
}
return n;
}

//計算深度為depth的滿二叉樹的結點數
inline int Count(int depth)
{
return pow(2,depth)-1;//滿二叉樹的結點計演算法
}

int ma......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.