二叉树及其应用

设计并验证如下算法:按中序建立两棵二叉树的二叉链表结构,判断两棵二叉树是否相等。

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#define OK		1
#define ERROR	0
#define LIST_INIT_SIZE 50
#define LISTINCREMENT  50
typedef int Status;
typedef char TElemType;
typedef struct BiTNode 
{
	TElemType data;
	struct BiTNode *lchild, *rchild;
	int position;
}BiTNode, *BiTree;
typedef char SElemType;
#include"StackForBiTNode.h"		//储存结点指针的栈 
Stack S;
struct Data
{
	char data;
	bool IsData;
};
typedef struct Data ElemType; 
struct LIST
{
	ElemType *elem;
	int length;
	int listsize;
};
typedef struct LIST SqList;
Status InitList_SqList(SqList *L) 
{
	L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
	L->length = 0;
	L->listsize = LIST_INIT_SIZE;
	return OK;
}
void InputList_SqList(SqList *L) 
{
	ElemType e;
	ElemType *newbase, *p;
	char a;
	while(1) 
	{
		scanf("%c", &a);
		if(a == '\n')
			break;
		e.data = a;
		e.IsData = false;
		if(a != '(' && a != ')' && a != '#')
			e.IsData = true;
		if(L->length >= L->listsize)
		{
			newbase = (ElemType *)realloc(L->elem, (L->listsize+LISTINCREMENT)*sizeof(ElemType));
			L->elem = newbase;
			L->listsize += LISTINCREMENT;
		}
		
		p=&L->elem[L->length];
		*p = e;
		L->length++;
	}
}
void printlist_SqList(SqList L) 
{
	printf("data:\t");
	for(int i=0; i<L.length; i++)
	printf("%c  ",L.elem[i].data);
	printf("\n");
}
Status InitBiTree(BiTree *);
BiTree CreateBiTree(BiTree *);	
Status PreOrder(BiTree );
bool Equal(BiTree , BiTree );
bool ExitLeft(SqList , int); 	 
bool ExitRight(SqList , int);
int LeftData(SqList , int );	
int RightData(SqList , int );
Status BiTreeInsLeft(BiTree *, char );		 
Status BiTreeInsRight(BiTree *, char );		 
int main() 
{	
	BiTree BT1, BT2;
	InitStack(&S);
	printf("\n创建第一棵树1\n以先序输入 :");
	InitBiTree(&BT1);
	BT1 = CreateBiTree(&BT1);
	printf("创建成功!\n");
	printf("\n创建第二棵树2\n以先序输入 :");
	InitBiTree(&BT2);
	BT2 = CreateBiTree(&BT2);
	printf("创建成功!\n");
	printf("先序输出树1\n");
	PreOrder(BT1);
	printf("先序输出树2\n");
	PreOrder(BT2);
	printf("判断是否相等\n");
	if(Equal(BT1, BT2))
		printf("\n两树相等!\n");
	else
		printf("\n 两树不相等!\n");
	return 0;
}
 
Status InitBiTree(BiTree *BT) 
{
	*BT = NULL;
	return 0;
}
 
BiTree CreateBiTree(BiTree *BT) 
{
	TElemType ch;
	SqList L;
	InitList_SqList(&L);
	InputList_SqList(&L);
	printlist_SqList(L);
	BiTNode *node, *root;
	int index = 0, count = 0;	
	for(int i = 1; i < L.length; i++) 
	{	 
		if(L.elem[i].data == '(')
			count ++;
		if(L.elem[i].data == ')')
			count --;
		if(count == 0)
		{
			index = i+1;break;
		}
	}
	while(L.elem[index].IsData == false) //如果该位置不是data,则往后一个位置	
		index++;
	*BT = (BiTNode *)malloc(sizeof(BiTNode));
	(*BT)->data = L.elem[index].data;
	(*BT)->position = index;
	(*BT)->lchild = NULL;
	(*BT)->rchild = NULL;
	push(&S, *BT);
	root = *BT;		
	while(!emptyStack(&S)) 
	{
		node = getTop(&S);
		index = node->position;
		if(ExitLeft(L, index) && node->lchild == NULL) 
		{
			index = LeftData(L, index-2);	//找到左子树结点并插入
			ch = L.elem[index].data;
			BiTreeInsLeft(&(*BT),ch);
			node->lchild->position = index;
			push(&S,node->lchild); 
		}
		
		else if(ExitRight(L, index) && node->rchild == NULL) 
		{
			node = pop(&S);
			index = RightData(L, index+2);//找到右子树结点并插入 
			ch = L.elem[index].data;
			*BT = node;
			BiTreeInsRight(&(*BT), ch);
			
			node->rchild->position = index;
			push(&S,node->rchild);
		}
		else {
			node = pop(&S);
			index = node->position;
		}	
	}
	return root;
}
bool ExitLeft(SqList L, int index) 
{
	while(index >= 0) {
		index--;
		if(L.elem[index].data == ')')
			return true;
		if(L.elem[index].data == '(')
			return false;	
	}
	return false;
}
 

bool ExitRight(SqList L, int index)  
{
	while(index < L.length) {
		index++; 
		if(L.elem[index].data == '(')
			return true;
		if(L.elem[index].data == ')')
			return false;	
	}
	return false;
}
int LeftData(SqList L, int index)//找左子树结点,返回在顺序表的位置
 {
	int ind, count = 0;
	for(int i = index; i >= 0; i--)//从index位置开始,当左右括号抵消时,找到左子树结点 
	{ 
		if(L.elem[i].data == ')')
			count ++;
		if(L.elem[i].data == '(')
			count --;
		if(count == 0) {
			ind = i-1; break;
		}
	}
	while(L.elem[ind].IsData == false)	//如果该位置不是data,则往后一个位置
		ind++;
	return ind;
}
int RightData(SqList L, int index) //找右子树结点,返回在顺序表的位置
{
	int ind, count = 0;
	for(int i = index; i < L.length; i++) //从index位置开始,当左右括号抵消时,找到右子树结点
	{	
		if(L.elem[i].data == '(')
			count ++;
		if(L.elem[i].data == ')')
			count --;
		if(count == 0) {
			ind = i+1; break;
		}
	}
	while(L.elem[ind].IsData == false)	//如果该位置不是data,则往前一个位置
		ind--;
	return ind;
}
Status BiTreeInsLeft(BiTree *BT, char data) 
{
	BiTNode *new_node;
	new_node = (BiTNode *)malloc(sizeof(BiTNode));
	if(BT == NULL)
		return -1;
	else {
		if((*BT)->lchild != NULL)
			return -1;
		else
			(*BT)->lchild = new_node;
	}
	new_node->data = data;
	new_node->lchild = NULL;
	new_node->rchild = NULL;
	*BT = new_node;
	return 0;
}
Status BiTreeInsRight(BiTree *BT, char data) 
{
	BiTNode *new_node;
	new_node = (BiTNode *)malloc(sizeof(BiTNode));
	if(BT == NULL)
		return -1;
	else {
		if((*BT)->rchild != NULL)
			return -1;
		else
			(*BT)->rchild = new_node;
	}
	new_node->data = data;
	new_node->lchild = NULL;
	new_node->rchild = NULL;
	*BT = new_node;
	return 0;
}
Status PreOrder(BiTree BT) {
	if(BT) {
		//(!(BT->data))
		//eturn ERROR;
		printf("%c ", BT->data);
		PreOrder(BT->lchild);
		PreOrder(BT->rchild);
		return OK; 
	}
	return 0;
}
bool Equal(BiTree BT1, BiTree BT2) {
	if((!BT1) && (!BT2))	
		return true;
	if(BT1->data == BT2->data)
		if(Equal(BT1->lchild, BT2->lchild) && (Equal(BT1->rchild, BT2->rchild)))
			return true;
	return false;
}

设计并验证如下算法:输入一棵二叉树的广义表形式,建立该二叉树的二叉链表结构,并求其总结点数目。例如,对“12.7.4 参考源程序”所示二叉树,按下列形式读入字符:C(E(I,J),F(,G(K,H))#。

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define Max_Tree_SIZE 20
#define STACK_INIT_SIZE 100
#define STACKINCRENT 10
#define MAXQSIZE 100
typedef int Status;
typedef char TElemType;
typedef struct BiTNode
{
	TElemType data;
	struct BiTNode *lchild,*rchild;
}BiNode,*BiTree;
typedef BiTree SElemType;
typedef struct Inode
{
	SElemType *base;
	SElemType *top;
	int stacksize;
}SqStack;
Status InitStack(SqStack *S)
{
	S->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
	if(!S->base)
		exit(OVERFLOW);
	S->top=S->base;
	S->stacksize=STACK_INIT_SIZE;
	return OK;
}
Status GetTop(SqStack S,SElemType *e)
{
	if(S.top==S.base)
		return ERROR;
	*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+STACKINCRENT)*sizeof(SElemType));
		if(!S->base)
			exit(OVERFLOW);
		S->top=S->base+S->stacksize;
		S->stacksize+=STACKINCRENT;
	}
	*S->top++=*e;
	return OK;
}
Status Pop(SqStack *S,SElemType *e)
{
	if(S->top==S->base)
		return ERROR;
	*e=*S->top;
	return OK;
}
Status InitBiTree(BiTree *BT);
Status CreateBiTree(BiTree *BT);
Status PreOrder(BiTree BT);
Status InOrder(BiTree BT);
Status PostOrder(BiTree BT);
int NodeNumber(BiTree BT);
void main()
{
	BiTree BT;
	int flag=1,select;
	CreateBiTree(&BT);
	while(flag)
	{
		printf("请选择\n");
		printf("1.先序\n");
		printf("2.中序\n");
		printf("3.后序\n");
		printf("4.求结点数\n");
		printf("5.退出\n");
		scanf("%d",&select);
		switch(select)
		{
		case 1:PreOrder(BT);break;
		case 2:InOrder(BT);break;
		case 3:PostOrder(BT);break;
		case 4:printf("%d",NodeNumber(BT));break;
		default:flag=0;
			printf("no");
			break;
		}
		printf("\n");
	}
}
Status InitBiTree(BiTree *BT)
{
	*BT=NULL;
	return OK;
}
Status CreateBiTree(BiTree *BT)
{
	SqStack S;
	InitStack(&S);
	*BT=NULL;
	char ch;
	int flag;
	SElemType p,e;
	scanf("%c",&ch);
	while(ch!='#')
	{
		switch(ch)
		{
		case'(':Push(&S,&p);flag=1;break;
		case',':flag=0;break;
		case')':Pop(&S,&p);break;
		default:p=(BiNode*)malloc(sizeof(BiNode));
			p->lchild=p->rchild=NULL;
			p->data=ch;
			if(!(*BT))
				*BT=p;
			else
			{
				GetTop(S,&e);
				if(flag)
					e->lchild=p;
				else
					e->rchild=p;
			}
			break;
		}
		scanf("%c",&ch);
	}
	return OK;
}
Status PreOrder(BiTree BT)
{
	if(BT)
	{
		if(!(BT->data))
			return ERROR;
		printf("%c",BT->data);
		PreOrder(BT->lchild);
		PreOrder(BT->rchild);
		return OK;
	}return OK;
}
Status InOrder(BiTree BT)
{
	if(BT)
	{
		if(!(BT->data))
			return ERROR;
		InOrder(BT->lchild);
		printf("%c",BT->data);
		InOrder(BT->rchild);
		return OK;
	}return OK;
}
Status PostOrder(BiTree BT)
{
	if(BT)
	{
		if(!(BT->data))
			return ERROR;
		PostOrder(BT->lchild);
		PostOrder(BT->rchild);
		printf("%c",BT->data);
		return OK;
	}return OK;
}

int NodeNumber(BiTree BT)
{
	if(!BT)
		return 0;
	else
		return NodeNumber(BT->lchild)+NodeNumber(BT->rchild)+1;

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值