【菜鸟】找了3个小时没发现哪出错了,虽然知道阅读代码很烦,但是恳请前辈们看看[数据结构]

zyz6991 2016-12-25 11:12:51
代码作用是输入"ab+cde+**",然后通过栈来控制树节点的组合(不是符号则字母入栈,是符号则pop两个字母出栈与符号组成新树在入栈),最后合成一整棵树(其实就是后序遍历了)
感觉写起来没什么问题,但是一运行就有错了,结果的最后一棵树上会多一个子节点,折磨了我好几个小时TAT
以下是代码:

// 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);
}
}


...全文
186 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
小灸舞 版主 2016-12-26
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
super_admi 2016-12-26
  • 打赏
  • 举报
回复
才几个小时……过3天再来。
ck2333 2016-12-25
  • 打赏
  • 举报
回复
仅供参考:
#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;
} 

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧