69,382
社区成员
发帖
与我相关
我的任务
分享
// BinaryTree.cpp : Defines the entry point for the console application.
//ab+cde+**
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
一、实现栈
二、实现树
三、实现将树压入栈的方法
*/
struct MyStack;
struct MyTree;
typedef struct MyTree * Tree;
typedef struct MyStack * stack;
//树的函数
//初始化树
Tree Initialization(char x);
//销毁树(没用到)
void destroyTree(Tree t);
//插入,t1是父,t2是左节点,t3是右节点
void insertTree(Tree t1,Tree t2,Tree t3);
//判断是否为空
int isNULL(Tree t);
void visit(Tree t, int depth);
//栈的函数
//栈初始化
stack stackInitalization();
//进栈
void push(stack s,Tree t);
//出栈
Tree pop(stack s);
void visit(stack s);
//structure:
struct MyStack
{
stack top;
stack base;
Tree T;
};
struct MyTree
{
Tree left;
Tree right;
char element;
};
int main()
{
char * a = (char *)malloc(18*sizeof(char));
strcpy_s(a,18, "ab+cde+**");
stack s = stackInitalization();
//压栈
int j = 0;
for(int i = 0; i < strlen(a); i++)
{
char newC = *(a+i);
Tree t1 = Initialization(newC);
if (newC !='+' && newC !='-' && newC!='*' && newC!='/')
{
push(s, t1);
}
else
{
j++;
Tree t2 =(struct MyTree *)pop(s);
Tree t3 =(struct MyTree *)pop(s);
insertTree(t1, t3, t2);
push(s, t1);
if (j <= 4)
{
visit(s);
printf("--------------------\n");
}
}
printf("\n");
}
return 0;
}
//树的函数
Tree Initialization(char x)
{
Tree t = (Tree)malloc(sizeof(struct MyTree));
t->left = NULL;
t->right = NULL;
t->element = x;
return t;
}
void destroyTree(Tree t)
{
if(!isNULL(t))free(t);
}
void insertTree(Tree t1,Tree t2,Tree t3)
{
t1->left = t2;
t1->right = t3;
}
int isNULL(Tree t)
{
if (t == NULL)
{
return 1;
}
else
{
return -1;
}
}
//栈的函数
stack stackInitalization()
{
stack s = (stack)malloc(20*sizeof(struct MyStack));
if (s == NULL) printf("out of space!\n");
s->base = s->top = (stack)malloc(sizeof(struct MyStack));
if (s->base == NULL&&s->top==NULL) printf("out of space!\n");
return s;
}
void push(stack s , Tree t)
{
if (s->top - s->base >= 0)
{
s->top++;
s->top->T = t;
}
}
Tree pop(stack s)
{
stack s1 = s->top;
if (s->top - s->base > 0)
{
s->top--;
}
return s1->T;
}
void visit(stack s)
{
if (s != NULL)
{
stack q = s->top;
while (q - s->base > 0)
{
visit(q->T, 0);
printf("\n");
q--;
}
}
}
void visit(Tree t,int depth)
{
if (t != NULL)
{
printf("t->em:%c\t%d\n",t->element,depth);
depth++;
visit(t->left, depth);
visit(t->right, depth);
}
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *lchild,*rchild;
}BItree,*BtNode;
typedef struct{
BtNode s[100];
int top;
}Stack;
void creatree(BtNode &tree) //递归建立二叉树
{
char c;
c=getchar();
if(c=='#')
tree=NULL;
else {
tree=(BtNode)malloc(sizeof(BItree));
tree->data=c-'0';
creatree(tree->lchild);
creatree(tree->rchild);
}
}//前序遍历:
void Forder(BtNode root)
{
BtNode p;
Stack store;
store.top=0;
store.s[store.top++]=root;
while(store.top>0)
{
p=store.s[--store.top];
printf("%3d",p->data);
if(p->rchild)
store.s[store.top++]=p->rchild;
if(p->lchild)
store.s[store.top++]=p->lchild;
}
}
//中序遍历:
void Midorder(BtNode root)
{
BtNode p=root;
Stack store; //指针栈
store.top=0; //top表示下一个元素的位置
store.s[store.top++]=root;
while(p->lchild) //如果有左孩子,左孩子进栈
{
store.s[store.top++]=p->lchild;
p=p->lchild;
}
while(store.top>0) //栈不为空就继续访问
{
p=store.s[--store.top]; //p指向栈顶元素
printf("%3d",p->data);
if(p->rchild)
{
store.s[store.top++]=p->rchild; //右孩子进栈
p=p->rchild;
while(p->lchild) //如果有左孩子,左孩子进栈
{
store.s[store.top++]=p->lchild;
p=p->lchild;
}
}
}
}
//后序遍历:
void Lorder(BtNode root)
{
BtNode p=NULL,cur=root; //p为前一个访问的结点,cur为当前结点
Stack store;
store.top=0;
store.s[store.top++]=root;
while(store.top>0)
{
cur=store.s[store.top-1];
if(cur->lchild==NULL&&cur->lchild==NULL|| //判断左右孩子是否不存在
(p!=NULL&&(p==cur->lchild||p==cur->rchild))) //判断左右孩子是否被访问过了
{
printf("%3d",cur->data);
store.top--; //输出了结点的data后就在栈中把该结点去掉
p=cur;
}
else {
if(cur->rchild!=NULL) //有右孩子就让右孩子入栈(先右再左)
store.s[store.top++]=cur->rchild;
if(cur->lchild!=NULL) //有左孩子就让左孩子入栈
store.s[store.top++]=cur->lchild;
}
}
}
int main ()
{
BtNode root;
creatree(root);
printf("\n");
printf("*****************************************\n");
printf("\n");
printf("前序非递归遍历为:");
Forder(root);
printf("\n\n");
printf("中序非递归遍历为:");
Midorder(root);
printf("\n\n");
printf("后序非递归遍历为:");
Lorder(root);
printf("\n\n");
return 0;
}