數據結構C語言——用二叉鏈表示二叉樹

頭文件,定義棧和結構體的功能:

/* 二叉樹的鏈接表示*/
#include <stdio.h>
#include <stdlib.h>

typedef char DataType;

struct BinTreeNode;	 
typedef  struct BinTreeNode	 *PBinTreeNode;/* 二叉樹中結點 */
typedef struct BinTreeNode { 
	DataType  info;	                        /* 數據域 */
	PBinTreeNode llink;                     /* 指向左子女 */
	PBinTreeNode rlink;                     /* 指向右子女 */
}BTnode;
typedef struct BinTreeNode *PBinTree;

/*隊列的結構體定義*/
#define  queueSize 20
struct Sequeue{
	int r,f;
	PBinTree q[queueSize];
};
typedef struct Sequeue *Pqueue;
//聲明隊列函數
Pqueue creatEmptyQueue(void);
int isEmptySqueue(Pqueue qu);
void insertQueue(Pqueue qu,PBinTree x);
void deleteQueue(Pqueue qu);
PBinTree topQueueElement(Pqueue qu);

//創建空隊列
Pqueue creatEmptyQueue(void){
	Pqueue qu=(Pqueue)malloc(sizeof(struct Sequeue));
	if (qu==NULL)
	{
		printf("out of space");
	} 
	else
	{
		qu->f=qu->r=0;
		return qu;
	}
}


//判斷隊列是否爲空
int isEmptySqueue(Pqueue qu){
	return qu->f==qu->r;//隊尾=隊頭,則隊列爲空
}

//在隊列中插入某一個元素
void insertQueue(Pqueue qu,PBinTree x){
	//首先判斷隊列是否已滿
	if ((qu->r+1)%queueSize==qu->f)
	{
		printf("full queue");
	} 
	else
	{    //尾進頭出
		qu->q[qu->r]=x;//x插入當前隊尾所指的位置
		//qu->r=qu->r+1;
		qu->r=(qu->r+1)%queueSize;//循環隊列的長度加一
	}
}

//刪除隊列頭部元素
void deleteQueue(Pqueue qu){
	//首先判斷隊列是否已空
	if (isEmptySqueue(qu))
	{
		printf("queue is empty");
	} 
	else
	{
		qu->f=qu->f+1;//使隊頭的下一個結點成爲隊頭,那麼當前的隊頭就出棧了
	}
}

//對於非空隊列,求隊頭元素
PBinTree topQueueElement(Pqueue qu){
	return qu->q[qu->f];//返回隊頭指向的當前元素
}

//*******************************************************************/

//判斷隊列是否已滿
int isFullQueue(Pqueue qu){
	return (qu->r+1)%queueSize==qu->f;//隊尾加一等於隊頭,則滿
}


/* 結點的指針類型 */

#define  stackSize 100//定義常量不需加分號
 struct stack{
	int top;//指向棧頂元素的指針
	PBinTree s[stackSize];//棧的空間大小
}Seqstack;
typedef struct stack *Pstack;

/*聲明函數*/
Pstack createEmptyStack_ps();
int isEmptyStack_ps(Pstack s);
void pushStack(Pstack ps,PBinTree x);
void pop_stack(Pstack s);
PBinTree topStack(Pstack s);

//創建一個空棧並初始化

Pstack createEmptyStack_ps(){
	Pstack s=(Pstack)malloc(sizeof(struct stack));
	if (s==NULL)
	{
		printf("out of space!");
	}else{
		s->top=-1;//棧頂元素賦值爲-1
	}
	return s;
}

//判斷棧是否爲空
int isEmptyStack_ps(Pstack s){
	return s->top==-1;//空棧返回1,否則返回0
}

//進棧
void pushStack(Pstack ps,PBinTree x){
	if (ps->top>=stackSize-1)
	{
		printf("over flow");
	} 
		ps->top++;//棧頂元素指針加1
		ps->s[ps->top]=x;
}



//取棧頂元素
PBinTree topStack(Pstack s){
	return (s->s[s->top]);
}

//出棧
void pop_stack(Pstack s){
	if (isEmptyStack_ps(s))
	{
		printf("under flow");
	}
	else{
		s->top--;//棧頂指針減1
	}
}




源文件:

#include "PStack.h"

/* 遞歸創建從根開始的二叉樹 ,中序遍歷創建二叉樹*/
PBinTree createRest_BTree() { 
	PBinTree  pbnode;
	char ch;
	scanf("%c", &ch);
	if(ch == '@') {
		pbnode = NULL;
	}
	else { 
		pbnode = (PBinTree)malloc(sizeof(struct BinTreeNode));
		if( pbnode == NULL )         {
			printf("Out of space!\n");
			return pbnode;
		}
		pbnode->info = ch;
		pbnode->llink = createRest_BTree();	/* 構造左子樹 */
		pbnode->rlink = createRest_BTree();	/* 構造右子樹 */
	}
	return pbnode;
}


//用棧實現二叉樹的先序遍歷
/*void FristReadBinTree(PBinTree t){
	PBinTree p;
	Pstack s;
	if (t==NULL)
	{
		return;
	}
	 s=createEmptyStack_ps();
	pushStack(s,t);
	while (!isEmptyStack_ps(s))
	{
		p=topStack(s);
		pop_stack(s);
		if (p!=NULL)
		{
			printf("%c ",p->info);
			pushStack(s,p->rlink);
			pushStack(s,p->llink);
		}
	}
}
//中序遍歷二叉樹
void minReadBinTree(PBinTree t){
	PBinTree p=t;
	Pstack s=createEmptyStack_ps();
	if (p==NULL)
	{
		return;
	}
	do
	{
		while (p!=NULL)
		{
			
			pushStack(s,p);
			p=p->llink;
		}
			p=topStack(s);
			pop_stack(s);
			printf("%c ",p->info);
			p=p->rlink;
	}while (p!=NULL||!isEmptyStack_ps(s));

}*/

//層次遍歷
void chengciReadBinTree(PBinTree t){
	PBinTree c,cc;
	Pqueue q=creatEmptyQueue();
	if (t==NULL)
	{
		return;
	}
	c=t;insertQueue(q,c);
	while (!isEmptySqueue(q))
	{
		c=topQueueElement(q);
		deleteQueue(q);
		printf("%c ",c->info);

		cc=c->llink;if (cc!=NULL)
		{
			insertQueue(q,cc);
		}

		cc=c->rlink;if (cc!=NULL)
		{
			insertQueue(q,cc);
		}
	}
}

//中序遍歷統計葉子結點的個數
int countLea(PBinTree p){
	static int count;
	if (p!=NULL)
	{
	countLea(p->llink);
	if ((!p->llink)&&(!p->rlink))
	{
		count++;
	}
	countLea(p->rlink);
	}
	return count;
	printf("fuck");
}

void main(){
	int count;
	printf("輸入字符創建二叉樹:\n");
	PBinTree p=createRest_BTree();
	printf("層次遍歷二叉樹:\n");
	//FristReadBinTree(p);//先序遍歷
	//minReadBinTree(p);//中序遍歷
	chengciReadBinTree(p);//層次遍歷
	printf("葉子結點個數:\n");
	count=countLea(p);
	printf("%d\n",count);
}



發佈了31 篇原創文章 · 獲贊 44 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章