主题:四层二叉树的前序遍历
高手过来看看吧 能不能帮我说说编写这个程序的步骤和思想啊,是不是先要用数据结构编 写 然后再转为汇编语言啊我把我写的二叉数程序(包括建立二叉数,用栈实现中序遍历)发给你啊.你对照一下我想你可以把你的程序给改出来啊.
// 二叉树的实现.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
//二叉树的结构
typedef struct bitnode{
char data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
//二叉数的二叉线索存储
typedef enum pointertag{link,thread};
typedef struct bithrnode{
char data;
struct bithrnode *lchild,*rchild;
pointertag ltag,rtag;
}bithrnode,*bithrtree;
//定义结点结构
typedef struct snode{
bitree tnode;
struct snode *link;
}snode;
//建立二叉树
char createbitree(bitree &T){
char ch;
scanf("%c",&ch);
if(ch==' ') T=NULL;
else{
if(!(T=(bitnode *)malloc(sizeof(bitnode)))) exit(0);
T->data=ch;
createbitree(T->lchild);
createbitree(T->rchild);
}
return 1;
}
//用栈实现遍历
char inorder(bitree T){
snode *top=NULL,*p;
while(T!=NULL||top!=NULL){
while(T!=NULL){
p=(snode*)malloc(sizeof(snode));
p->tnode=T;
p->link=top;
top=p;
T=T->lchild;}//走完左子树
if(top!=NULL){
T=top->tnode;
p=top;
top=top->link;
free(p);
printf("%c",T->data);
T=T->rchild;}//走完右子树
}
return 1;
}
//中索线索化链表
char inthreading(bithrtree p){
bithrnode *pre=NULL;
if(p){
inthreading(p->lchild);
if(!p->lchild){
p->ltag=thread;
p->lchild=pre;
}
if(!pre->rchild){
pre->ltag=thread;
pre->rchild=p;
}
pre=p;
inthreading(p->rchild);
}
return 1;
}
char inorderthreading(bithrtree &thrt,bithrtree t){
bithrnode *pre;
if(!(thrt=(bithrtree)malloc(sizeof(bithrnode)))) exit(0);
thrt->ltag=link;
thrt->rtag=thread;
thrt->rchild=thrt;
if(!t)thrt->lchild=thrt;
else{
thrt->lchild=t;
pre=thrt;
inthreading(t);
pre->rchild=thrt;
pre->rtag=thread;
thrt->rchild=pre;
}
return 1;
}
//主函数
void main(){
bitree T;
bithrtree thrt,t=NULL;
createbitree(T);
inorder(T);
inorderthreading(thrt,t);
}
能不能帮我看看这个程序 是否符合数据结构的条件?
如果会用汇编语言编写更好,指点指点我吧。 我的QQ:277355453
// 二叉树的实现.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
//二叉树的结构
typedef struct bitnode{
char data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
//二叉数的二叉线索存储
typedef enum pointertag{link,thread};
typedef struct bithrnode{
char data;
struct bithrnode *lchild,*rchild;
pointertag ltag,rtag;
}bithrnode,*bithrtree;
//定义结点结构
typedef struct snode{
bitree tnode;
struct snode *link;
}snode;
//建立二叉树
char createbitree(bitree &T){
char ch;
scanf("%c",&ch);
if(ch==' ') T=NULL;
else{
if(!(T=(bitnode *)malloc(sizeof(bitnode)))) exit(0);
T->data=ch;
createbitree(T->lchild);
createbitree(T->rchild);
}
return 1;
}
//用栈实现遍历
char inorder(bitree T){
snode *top=NULL,*p;
while(T!=NULL||top!=NULL){
while(T!=NULL){
p=(snode*)malloc(sizeof(snode));
p->tnode=T;
p->link=top;
top=p;
T=T->lchild;}//走完左子树
if(top!=NULL){
T=top->tnode;
p=top;
top=top->link;
free(p);
printf("%c",T->data);
T=T->rchild;}//走完右子树
}
return 1;
}
//中索线索化链表
char inthreading(bithrtree p){
bithrnode *pre=NULL;
if(p){
inthreading(p->lchild);
if(!p->lchild){
p->ltag=thread;
p->lchild=pre;
}
if(!pre->rchild){
pre->ltag=thread;
pre->rchild=p;
}
pre=p;
inthreading(p->rchild);
}
return 1;
}
char inorderthreading(bithrtree &thrt,bithrtree t){
bithrnode *pre;
if(!(thrt=(bithrtree)malloc(sizeof(bithrnode)))) exit(0);
thrt->ltag=link;
thrt->rtag=thread;
thrt->rchild=thrt;
if(!t)thrt->lchild=thrt;
else{
thrt->lchild=t;
pre=thrt;
inthreading(t);
pre->rchild=thrt;
pre->rtag=thread;
thrt->rchild=pre;
}
return 1;
}
//主函数
void main(){
bitree T;
bithrtree thrt,t=NULL;
createbitree(T);
inorder(T);
inorderthreading(thrt,t);
}
能不能帮我看看这个程序 是否符合数据结构的条件?
如果会用汇编语言编写更好,指点指点我吧。 我的QQ:277355453