将二叉树转换为双向链表

[复制链接]
查看11 | 回复1 | 2010-12-5 01:24:12 | 显示全部楼层 |阅读模式
#include
#include
#include
typedef char elemtype;
struct bnode
{

elemtype data;

int top;

struct bnode *lchild,*rchild;

struct bnode *llink,*rlink;
};
const N=10;
void push(struct bnode *p)
{

struct bnode *q;

new(q);

q=p;

if(top!=0)

{
}
struct bnode *set_tree()
{

struct bnode *t,*p,*q;

int i;

char d;

t=new(bnode);

t->data=rand() % 26 + 'A';

t->lchild=NULL; t->rchild=NULL;

for (i=1;ip->data) {q=p; p=p->rchild;}

else { q=p; p=p->lchild;}

p=new(bnode);

p->data=d; p->lchild=NULL; p->rchild=NULL;

if (d>q->data)
q->rchild=p;

else q->lchild=p;

}

return t;
}
void pre_order(struct bnode *t)
{

if (t!=NULL)

{

coutdatalchild);

pre_order(t->rchild);

}
}
void mid_order(struct bnode *t)
{

if (t!=NULL)

{

mid_order(t->lchild);

coutdatarchild);

}
}
void write(struct bnode *h)
{

struct bnode *p;

p=h;

while(p->rlink!=h)

{

coutdata;

p=p->rchild;

}

coutdata;
}
struct bnode *Conver(struct bnode *t)
{

struct bnode *p,*q,top;
q=p=t;

top=0;

while(p!=NULL||top!=0)

{

if(p!=NULL)

{


q->rlink=p;

p->llink=q;

q=q->rlink;

push(p);

p=p->lchild;

}

else

{

pop(p);

p=p->rchild;

}

}

q->llink->rlink=t;

t->llink=q->llink;

return t;
}
void main()
{
struct bnode *t;

//srand((unsigned)time(NULL));

t=set_tree();

cout<<endl<<"preorder"<<endl;

pre_order(t);

cout<<endl<<"midorder"<<endl;

mid_order(t);

cout<<endl;
}
首先栈的定义不会,此外帮我看下COVER过程,能实现嘛?编不下去了啊,这块不是很懂,忘谁能给个正确的程序 谢谢 不盛感激
给个过程吧 队的定义真不会 麻烦了

回复

使用道具 举报

千问 | 2010-12-5 01:24:12 | 显示全部楼层
与队列配合操作: 对第 一 层进队; 1.把队头节点的左右节点分别进队,对头节点出队并储存当前数据进数组, 2.重复1,直到当前层均为NULL
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行