标签:
#include <stdio.h> #include <stdlib.h> #define MAX 100 //树结点的设计 typedef struct node { //数字和运算符 union { char operator; int data; }; struct node *lchild; struct node *rchild; }TreeNode; //树栈的设计 typedef struct { TreeNode *buf[MAX]; int n; }TreeStack; //创建空栈 TreeStack *create_empty_stack() { TreeStack *pstack; pstack = (TreeStack *)malloc(sizeof(TreeStack)); pstack->n = -1; return pstack; } //入栈 int push_stack(TreeStack *p,TreeNode *data) { p->n ++; p->buf[p->n] = data; return 0; } //出栈 TreeNode *pop_stack(TreeStack *p) { TreeNode *data; data = p->buf[p->n]; p->n --; return data; } //判断空栈 int is_empty_stack(TreeStack *p) { if(p->n == -1) { return 1; }else{ return 0; } } //后缀表达式树的创建 TreeNode *create_express_tree(char *str,TreeStack *p) { int i = 0; TreeNode *current; TreeNode *left,*right; while(str[i]) { if(str[i] == ‘ ‘) { i ++; continue; } if(str[i] >= ‘0‘ && str[i] <= ‘9‘) { current = (TreeNode *)malloc(sizeof(TreeNode)); current->data = str[i] - ‘0‘; current->lchild = current->rchild = NULL; push_stack(p,current); }else{ current = (TreeNode *)malloc(sizeof(TreeNode)); current->operator = str[i]; right = pop_stack(p); left = pop_stack(p); current->lchild = left; current->rchild = right; push_stack(p,current); } i ++; } return p->buf[p->n]; } //打印结点 void print_node(TreeNode *p) { if(p->lchild == NULL && p->rchild == NULL) { printf("%d ",p->data); }else{ printf("%c ",p->operator); } return; } //访问结点 int vist_node(TreeNode *p) { print_node(p); return 0; } //树的后序遍历 int PostOrder(TreeNode *p) { if(p != NULL) { PostOrder(p->lchild);//左 PostOrder(p->rchild);//右 vist_node(p);//根 } return 0; } //树的中序遍历 int InOrder(TreeNode *p) { if(p != NULL) { InOrder(p->lchild);//左 vist_node(p);//根 InOrder(p->rchild);//右 } return 0; } //树的前序遍历 int PreOrder(TreeNode *p) { if(p != NULL) { vist_node(p);//根 PreOrder(p->lchild);//左 PreOrder(p->rchild);//右 } return 0; } /队列的设计 struct _node_ { TreeNode *data; struct _node_ *next; }; typedef struct { struct _node_ *front; struct _node_ *rear; }Queue; //创建空队列 Queue *create_empty_queue() { struct _node_ *pnode; Queue *qhead; qhead = (Queue *)malloc(sizeof(Queue)); pnode = (struct _node_ *)malloc(sizeof(struct _node_)); pnode->next = NULL; qhead->front = qhead->rear = pnode; return qhead; } //入队 int EnterQueue(Queue *qhead,TreeNode *data) { struct _node_ *temp; temp = (struct _node_ *)malloc(sizeof(struct _node_ *)); temp->data = data; temp->next = NULL; qhead->rear->next = temp; qhead->rear = temp; return 0; } //出队 TreeNode *DeleteQueue(Queue *qhead) { struct _node_ *temp; temp = qhead->front; qhead->front = temp->next; free(temp); temp = NULL; return qhead->front->data; } //队列为空 int is_empty_queue(Queue *qhead) { if(qhead->front == qhead->rear) return 1; else return 0; } //树的层次遍历 int NoOrder(TreeNode *p) { Queue *qhead; TreeNode *pdata; qhead = create_empty_queue(); EnterQueue(qhead, p); while(!is_empty_queue(qhead)) { pdata = DeleteQueue(qhead); vist_node(pdata); if(pdata->lchild)EnterQueue(qhead,pdata->lchild); if(pdata->rchild)EnterQueue(qhead,pdata->rchild); } return 0; } int main() { TreeNode *thead; TreeStack *pstack; int i = 0; char buf[100]; while((buf[i++] = getchar()) != ‘\n‘ && i < 100); buf[i-1] = 0; pstack = create_empty_stack(); thead = create_express_tree(buf,pstack); printf("PostOrder:: "); PostOrder(thead); printf("\n"); printf("InOrder:: "); InOrder(thead); printf("\n"); printf("PreOrder:: "); PreOrder(thead); printf("\n"); printf("NoOrder::"); NoOrder(thead); printf("\n"); return 0; }
标签:
原文地址:http://www.cnblogs.com/bixiongquan/p/4355647.html