資料結構-樹的筆記
阿新 • • 發佈:2018-11-24
#include <stdio.h> #include <stdlib.h> //節點宣告,資料域、左指標、右指標 typedef struct BiTNode { int data; struct BiTNode *Left,*Right; }BiTNode,*BiTree; typedef struct SeqStack{ BiTNode* num[100]; int top; }SeqStack; void InitStack(SeqStack *S){ S->top = -1; } int push(SqStack* S,BitNode* p){ if(S->top==-1) { return 0; } S->top++; S->num[S->top] = *p; return 1; } int pop(SqStack* S,BitNode* p){ if(S->top ==-1) return 0; else{ p = &(S->num[S->top]); S->top--; return 1; } } BiTree CreateBiTree(); void PreOrderTraverse(BiTree root); void PostOrder(BiTNode *root); int main(void) { BiTree T; int d; T = CreateBiTree();//建立 //PreOrderTraverse(T); PostOrder(T); return 0; }
先序建立二叉樹
BiTree CreateBiTree()
{
char a;
BiTree T;
scanf("%c",&a);
if(a=='#')
T=NULL;
else
{
T = (BiTree)malloc(sizeof(BiTNode));
T->data = a;
T->Left = CreateBiTree();
T->Right = CreateBiTree();
}
return T;//返回根節點
}
先序遞迴遍歷一個二叉樹
void PreOrderTraverse(BiTree root){
if(root!=NULL){
printf("%c",root->data);
PreOrderTraverse(root->Left);
PreOrderTraverse(root->Right);
}
}
中序遞迴遍歷一個二叉樹
void InOrderTraverse(BiTree root){ if(root!=NULL){ InOrderTraverse(root->Left); printf("%c",root->data); InOrderTraverse(root->Right); } }
後序遞迴遍歷一個二叉樹
void PostOrderTraverse(BiTree root){
if(root!=NULL){
PostOrderTraverse(root->Left);
PostOrderTraverse(root->Right);
printf("%c",root->data);
}
}
先序非遞迴遍歷一個二叉樹
void PreOrder(BiTree root){
SeqStack* S = (SeqStack*)malloc(sizeof(SeqStack));
InitStack(S);
BiTNode* p;
p = root;
while(p||!StackEmpty(S)){
if(p){//當p不為空
printf("%c",p->data);
//入棧
push(S,p);
p = p->Left;
}else{//當p為空
p = S->num[S->top];
pop(S,p);
p = p->Right;
//訪問右節點
}
}
}
中序非遞迴遍歷一個二叉樹
void InOrder(BiTree root){
SeqStack* S = (SeqStack*)malloc(sizeof(SeqStack));
InitStack(S);
BiTNode* p;
p = root;
while(p||!StackEmpty(S)){
if(p){//當p不為空
//入棧
push(S,p);
p = p->Left;
}else{//當p為空
p = S->num[S->top];
pop(S,p);
printf("%c",p->data);
p = p->Right;
//訪問右節點
}
}
}
後序非遞迴遍歷一個二叉樹
void PostOrder(BiTNode *root)
{
SeqStack *S = (SeqStack *)malloc(sizeof(SeqStack));
S->top = -1 ;
BiTNode *p,*q;
p = root;
q = NULL;
while(p||S->top!=-1) {
if(p){
push(S,p);
p = p->Left;
}
else{
p = S->num[S->top];
if(p->Right==NULL || p->Right == q){
printf("%c",p->data);
S->top--;
q = p;
p = NULL;
}else{
p = p->Right;
}
}
}
}
層次遍歷
void LevelOrder(BiTree root){
SqQueue *Q = (SqQueue*)malloc(sizeof(SqQueue));
BiTree p = (BiTree)malloc(sizeof(BiTNode));
Q->front = -1;
Q->rear = -1;
Q->rear++;
Q->num[Q->rear] = root;
while(Q->rear-Q->front!=0){
Q->front++;
p = Q->num[Q->front];
printf("%c",p->data);
if(p->Left!=NULL){
Q->rear++;
Q->num[Q->rear] = p->Left;
}
if(p->Right!=NULL){
Q->rear++;
Q->num[Q->rear] = p->Right;
}
}
}
葉子結點個數/度為0的結點個數
int leaveSum(BiTree root){
int leaveCount;
if(root==NULL){
leaveCount = 0;
}else if((root->Left==NULL&&root->Right==NULL)){
leaveCount = 1;
}else{
leaveCount = leave(root->Left)+leave(root->Right);
}
return leaveCount;
}
度為1的節點個數
int leaveOne(BiTree root){
int leaveCount;
if(root!=NULL){
if((root->Left!=NULL&&root->Right==NULL)||(root->Left==NULL&&root->Right!=NULL)){
leaveCount = 1;
}else{
leaveCount = 0;
}
return leaveCount+leaveOne(root->Left)+leaveOne(root->Right);
}
return 0;
}
度為2的節點個數
int leaveTwo(BiTree root){
int leaveCount;
if(root!=NULL){
if(root->Left!=NULL&&root->Right!=NULL){
leaveCount = 1;
}
else{
leaveCount = 0;
}
return leaveCount+leaveTwo(root->Left)+leaveTwo(root->Right);
}
}