逆時針旋轉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......餘下全文>>