- 相關(guān)推薦
c語言版本二叉樹基本操作示例
在計(jì)算機(jī)科學(xué)中,二叉樹是每個(gè)節(jié)點(diǎn)最多有兩個(gè)子樹的樹結(jié)構(gòu)。通常子樹被稱作“左子樹”和“右子樹”。本文特意為大家收集整理了c語言版本二叉樹基本操作示例,希望大家喜歡!
復(fù)制代碼 代碼如下:
請(qǐng)按先序遍歷輸入二叉樹元素(每個(gè)結(jié)點(diǎn)一個(gè)字符,空結(jié)點(diǎn)為'='):
ABD==E==CF==G==
先序遞歸遍歷:
A B D E C F G
中序遞歸遍歷:
D B E A F C G
后序遞歸遍歷:
D E B F G C A
層序遞歸遍歷:
ABCDEFG
先序非遞歸遍歷:
A B D E C F G
中序非遞歸遍歷:
D B E A F C G
后序非遞歸遍歷:
D E B F G C A
深度:
請(qǐng)按任意鍵繼續(xù). . .
復(fù)制代碼 代碼如下:
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status;
typedef char ElemType;
typedef struct BTNode
{
ElemType data;
struct BTNode *leftChild;
struct BTNode *rightChild;
}BTNode, *BinTree;
typedef BinTree SElemType;
typedef struct{//棧結(jié)構(gòu)定義
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
BinTree CreateBinTree(BinTree T);
Status Visit(ElemType e);
Status Depth(BinTree T);
Status PreOrderRecursionTraverse(BinTree T, Status (*Visit)(ElemType e));
Status InOrderRecursionTraverse(BinTree T, Status (*Visit)(ElemType e));
Status PostOrderRecursionTraverse(BinTree T, Status (*Visit)(ElemType e));
Status LevelOrderRecursionTraverse(BinTree T, Status (*Visit)(ElemType e));
//定義棧的相關(guān)操作
Status InitStack(SqStack *S);
Status DestroyStack(SqStack *S);
Status ClearStack(SqStack *S);
Status StackEmpty(SqStack S);
int StackLength(SqStack S);
Status GetTop(SqStack S,SElemType *e);
Status Push(SqStack *S,SElemType e);
Status Pop(SqStack *S,SElemType *e);
Status StackTraverse(const SqStack *S);
Status PreOrderNoneRecursionTraverse(BinTree T, Status (*Visit)(ElemType e));
Status InOrderNoneRecursionTraverse(BinTree T, Status (*Visit)(ElemType e));
Status PostOrderNoneRecursionTraverse(BinTree T, Status (*Visit)(ElemType e));
int main()
{
int depth;
BinTree Tree = NULL;
Status(*visit)(ElemType e) = Visit;
printf_s("請(qǐng)按先序遍歷輸入二叉樹元素(每個(gè)結(jié)點(diǎn)一個(gè)字符,空結(jié)點(diǎn)為'='):n");
Tree = CreateBinTree(Tree);
printf_s("n先序遞歸遍歷:n");
PreOrderRecursionTraverse(Tree,visit);
printf_s("n中序遞歸遍歷:n");
InOrderRecursionTraverse(Tree,visit);
printf_s("n后序遞歸遍歷:n");
PostOrderRecursionTraverse(Tree,visit);
printf_s("n層序遞歸遍歷:n");
LevelOrderRecursionTraverse(Tree,visit);
printf_s("n先序非遞歸遍歷:n");
PreOrderNoneRecursionTraverse(Tree,visit);
printf_s("n中序非遞歸遍歷:n");
InOrderNoneRecursionTraverse(Tree,visit);
printf_s("n后序非遞歸遍歷:n");
PostOrderNoneRecursionTraverse(Tree,visit);
printf_s("n深度:n");
depth = Depth(Tree);
printf_s("%dn", depth);
system("pause");
return 0;
}
//創(chuàng)建二叉樹
BinTree CreateBinTree(BinTree T)
{
char ch;
scanf_s("%c", &ch);
if (ch == '=')
{
T = NULL;
}
else
{
if (!(T=(BTNode *) malloc(sizeof(BTNode))))
{
exit(OVERFLOW);
}
T->data = ch; //生成根結(jié)點(diǎn)
T->leftChild = CreateBinTree(T->leftChild);
T->rightChild = CreateBinTree(T->rightChild);
}
return T;
}
//訪問二叉樹
Status Visit(ElemType e)
{
if (e == '')
{
return ERROR;
}
else
{
printf_s("%c ", e);
}
return OK;
}
//先序遍歷遞歸算法
Status PreOrderRecursionTraverse(BinTree T, Status (*Visit)(ElemType e))
{
if (T)
{
if (!Visit(T->data))
{
return ERROR;
}
PreOrderRecursionTraverse(T->leftChild, Visit);
PreOrderRecursionTraverse(T->rightChild, Visit);
}
return OK;
}
//中序遍歷遞歸算法
Status InOrderRecursionTraverse(BinTree T, Status (*Visit)(ElemType e))
{
if (T)
{
InOrderRecursionTraverse(T->leftChild, Visit);
if (!Visit(T->data))
{
return ERROR;
}
InOrderRecursionTraverse(T->rightChild, Visit);
}
return OK;
}
//后序遍歷遞歸算法
Status PostOrderRecursionTraverse(BinTree T, Status (*Visit)(ElemType e))
{
if (T)
{
PostOrderRecursionTraverse(T->leftChild, Visit);
PostOrderRecursionTraverse(T->rightChild, Visit);
if (!Visit(T->data))
{
return ERROR;
}
}
return OK;
}
//層序遍歷遞歸算法
Status LevelOrderRecursionTraverse(BinTree T, Status (*Visit)(ElemType e))
{
if (T)
{
BTNode *Q[100];//假設(shè)不溢出
int front = -1,rear = -1;
if (T)
{
Q[++rear] = T;
printf_s("%c", T->data);
while (front != rear)
{
BTNode *p;
if (!(p = (BTNode *)malloc(sizeof(BTNode))))
{
exit(OVERFLOW);
}
p = Q[++front];
if (p->leftChild)
{
Q[++rear] = p->leftChild;
printf("%c",p->leftChild->data);
}
if (p->rightChild)
{
Q[++rear] = p->rightChild;
printf("%c",p->rightChild->data);
}
}
}
}
return OK;
}
Status Depth(BinTree T)
{
int a,b;
if (!T)
{
return ERROR;
}
else
{
a = Depth(T->leftChild) + 1;
b = Depth(T->rightChild) + 1;
return a > b ? a : b;
}
}
//先序遍歷非遞歸算法
Status PreOrderNoneRecursionTraverse(BinTree T, Status (*Visit)(ElemType e))
{
SqStack S;
SElemType p;
InitStack(&S);
Push(&S, T);
while (!StackEmpty(S))
{
Pop(&S, &p);
if (!Visit(p->data))
{
return ERROR;
}
if (p->leftChild)
{
Push(&S, p->rightChild);
}
if (p->rightChild)
{
Push(&S, p->leftChild);
}
}
DestroyStack(&S);
return OK;
}
//中序遍歷非遞歸算法
Status InOrderNoneRecursionTraverse(BinTree T, Status (*Visit)(ElemType e))
{
SqStack S;
SElemType p;
InitStack(&S);
Push(&S, T);
while (!StackEmpty(S))
{
while (GetTop(S,&p) && p)
{
Push(&S, p->leftChild);
}
Pop(&S, &p);
if (!StackEmpty(S))
{
Pop(&S, &p);
if (!Visit(p->data))
{
return ERROR;
}
Push(&S, p->rightChild);
}
}
DestroyStack(&S);
return OK;
}
//后序便利非遞歸算法
Status PostOrderNoneRecursionTraverse(BinTree T, Status (*Visit)(ElemType e))
{
SqStack S;
SElemType p, q;
InitStack(&S);
Push(&S,T);
while(!StackEmpty(S))
{
while(GetTop(S,&p)&&p&&(p->leftChild||p->rightChild))
{
Push(&S,p->rightChild);
Push(&S,p->leftChild);
}
if(!StackEmpty(S)){
Pop(&S,&p);
if (p)
{
if(!Visit(p->data))
{
return ERROR;
}
}
else
{
Pop(&S,&p);
if(!Visit(p->data))
{
return ERROR;
}
}
while (GetTop(S,&q)&&q&&p==q->rightChild)
{
Pop(&S,&p);
if(!Visit(p->data))
{
return ERROR;
}
GetTop(S,&q);
}
}
}
DestroyStack(&S);
return OK;
}
//-----------棧的相關(guān)操作--------------//
Status InitStack(SqStack *S){
S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if(!S->base)
{
exit(0);
}
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
return OK;
}
Status DestroyStack(SqStack *S){
if(!S)
{
exit(0);
}
free(S->base);
return OK;
}
Status ClearStack(SqStack *S){
if(!S)
{
return FALSE;
}
S->top = S->base;
return OK;
}
Status StackEmpty(SqStack S){
if(S.top==S.base)
{
return TRUE;
}
else
{
return FALSE;
}
}
int StackLength(SqStack S){
return S.stacksize;
}
Status GetTop(SqStack S,SElemType *e){
if(S.top == S.base)
{
return FALSE;
}
else
{
*e = *(S.top-1);
return OK;
}
}
Status Push(SqStack *S,SElemType e){
if(S->top-S->base>=S->stacksize)
{
S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType));
if(!S->base)
{
exit(0);
}
S->top = S->base+S->stacksize;
S->stacksize += STACKINCREMENT;
}
*S->top++ = e;
return OK;
}
Status Pop(SqStack *S,SElemType *e){
if(S->top==S->base)
{
return ERROR;
}
*e = *(--S->top);
return OK;
}
【c語言版本二叉樹基本操作示例】相關(guān)文章:
C語言對(duì)棧的實(shí)現(xiàn)基本操作介紹10-29
c語言操作文本基本使用方法09-16
C語言位操作是08-17
C語言的底層操作08-23
C語言的特點(diǎn)及版本有哪些08-17
C語言的基本要點(diǎn)08-19
c語言的基本特性07-19
C語言的基本構(gòu)成10-19
C#檢測(cè)操作系統(tǒng)版本的方法匯總07-15
C語言位操作教程08-07