算术表达式二叉树表示

输入的中缀表达式为:  

1+2*(3-4)-5/6#

 1)以二叉链表的存储结构创建一棵。

 2)先根遍历为:此中缀表达式的前缀表达式  

 3)后根遍历为:此中缀表达式的后缀表达式 

栈的操作参考:  

栈、循环队列的基本操作

BiTree.h

#define TRUE	1
#define FALSE	0

typedef int status;
typedef struct BiTNode
{
	int data;
	struct BiTNode *lchild;
	struct BiTNode *rchild;
}BiTree;

BiTree.c

#include "BiTree.h"

//算术符优先级
			//+ - * / ( ) #
int a[7][7] = { 
	/* + */{1, 1, -1, -1, -1, 1, 1},
	/* - */{1, 1, -1, -1, -1, 1, 1},
	/* * */{1, 1, 1, 1, -1, 1, 1},
	/* / */{1, 1, 1, 1, -1, 1, 1},
	/* ( */{-1, -1, -1, -1, -1, 0, 100},
	/* ) */{1, 1, 1, 1, 100, 1, 1},
	/* # */{-1, -1, -1, -1, -1, 100, 0}};

//获得当前算术符在优先级中的坐标
int getIndex(char x)
{
	int ret;
	switch(x)
	{
	case '+':
		ret = 0;
		break;
	case '-':
		ret = 1;
		break;
	case '*':
		ret = 2;
		break;
	case '/':
		ret = 3;
		break;
	case '(':
		ret = 4;
		break;
	case ')':
		ret = 5;
		break;
	case '#':
		ret = 6;
		break;
	default:
		ret = 100;
		break;
	}
	return ret;
}

//比较优先级大小
int cmp(char x, char y)
{
	int xa;
	int ya;
	xa = getIndex(x);
	ya = getIndex(y);
	if(xa == 100 || ya == 100)
	{
		return -100;
	}
	return a[xa][ya];
}

//创建一个数值节点
status CreateNode(BiTree **T, int num)
{
	*T = (BiTree *)malloc(sizeof(BiTree));

	if(*T == NULL)
	{
		return FALSE;
	}
	(*T)->data = num;
	(*T)->lchild = NULL;
	(*T)->rchild = NULL;
	return TRUE;
}

//创建一个根节点
status CreateRoot(BiTree **T, BiTree *opand1, int optr, BiTree *pand2)
{
	*T = (BiTree *)malloc(sizeof(BiTree));

	if(*T == NULL)
	{
		return FALSE;
	}
	(*T)->data = optr;
	(*T)->lchild = opand1;
	(*T)->rchild = pand2;
	return TRUE;
}

//创建表达式二叉树
status CreateBiTree(BiTree **T)
{
	char c = 0;
	int x = 0;
	int r = 0;
	BiTree *pd1;
	BiTree *pd2;
	BiTree *root;
	stack opnd;
	stack optr;
	initStack(&opnd);
	initStack(&optr);

	Push(&optr, (BiTree *)'#');
	scanf("%c", &c);
	while(!IsStackEmpty(&optr))
	{
		if(c >= '0' && c <= '9')
		{
			CreateNode(&pd1, c);
			Push(&opnd, pd1);
			scanf("%c", &c);
		}
		else
		{
			GetTop(&optr, (BiTree **)&x);
			r = cmp(x, c);
			switch(r)
			{
			case 1://>
				Pop(&opnd, &pd2);
				Pop(&opnd, &pd1);
				Pop(&optr, (BiTree **)&x);
				CreateRoot(&root, pd1, x, pd2);
				Push(&opnd, root);
				break;
			case -1://<
				Push(&optr, (BiTree *)c);
				scanf("%c", &c);
				break;
			case 0://==
				Pop(&optr, (BiTree **)&x);
				scanf("%c", &c);
				break;
			default:
				break;
			}
		}
	}
	Pop(&opnd, T);
	return TRUE;
}

void Visit(BiTree *T)
{
	printf("%c ", T->data);
}

status PreOrder(BiTree *T)
{
	if(T != NULL)
	{
		Visit(T);
		PreOrder(T->lchild);
		PreOrder(T->rchild);
	}
	return TRUE;
}

status InOrder(BiTree *T)
{
	if(T != NULL)
	{
		InOrder(T->lchild);
		Visit(T);
		InOrder(T->rchild);
	}
	return TRUE;
}

status PostOrder(BiTree *T)
{
	if(T != NULL)
	{
		PostOrder(T->lchild);
		PostOrder(T->rchild);
		Visit(T);
	}
	return TRUE;
}

status ClearBiTree(BiTree *root)
{
	if(root == NULL)
	{
		return TRUE;
	}
	ClearBiTree(root->lchild);
	ClearBiTree(root->rchild);
	free(root);
}

int main()
{
	BiTree *root;
	CreateBiTree(&root);

	printf("先序遍历:");
	PreOrder(root);
	printf("\n");

	printf("中序遍历:");
	InOrder(root);
	printf("\n");

	printf("后序遍历:");
	PostOrder(root);
	printf("\n");

	ClearBiTree(root);
	return 0;
}
演示范例:   
        输入:1+2*(3-4)-5/6#   
        输出:

先序遍历:- + 1 * 2 - 3 4 / 5 6
中序遍历:1 + 2 * 3 - 4 - 5 / 6
后序遍历:1 2 3 4 - * + 5 6 / -
        输入串以#结尾,只能输入一位整数。  

你可能感兴趣的:(数据结构)